Skip to content

Instantly share code, notes, and snippets.

@XinboChen
Last active August 29, 2015 14:05
Show Gist options
  • Save XinboChen/5d655fb0b2999924d61e to your computer and use it in GitHub Desktop.
Save XinboChen/5d655fb0b2999924d61e to your computer and use it in GitHub Desktop.
/*
Implement an algorithm to determine if a string has all unique characters what if you can't use additional data structure
Date: 08/26/2014
*/
class solution{
// Time complexity O(n^2), Space complexity O(1)
public boolean check( String s ){
boolean result = true;
if( s.length() < 2 )// include string is empty
result = true;
int i, j;
for( i = 0; i < s.length() - 1; i ++){
for(j = i + 1; j < s.length(); j ++){
if(s.charAt( i ) == s.charAt( j ) // charAt(int index) return a character of a string in position index.
result = false;
break;
}
return true;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment