Skip to content

Instantly share code, notes, and snippets.

@jiaodong
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiaodong/0d6309d1694f6e08ad83 to your computer and use it in GitHub Desktop.
Save jiaodong/0d6309d1694f6e08ad83 to your computer and use it in GitHub Desktop.
// 1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
public boolean isUnique(String s){
int length = s.length();
// ASCII table only has 128 unique characters, incluing commands
if(length > 256) return false;
boolean[] table = new boolean[256];
for(int i = 0; i < length; i++){
// Read ASCII int value of character
int code = (int) s.charAt(i);
if(!table[code]){
table[code] = true;
} else {
return false;
}
}
// After iterating entire string there's no duplicate characters
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment