Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Last active October 13, 2019 15:09
Show Gist options
  • Save wangchauyan/23edbcc32099ba6ced75 to your computer and use it in GitHub Desktop.
Save wangchauyan/23edbcc32099ba6ced75 to your computer and use it in GitHub Desktop.
/*
Implement an algorithm to detemine if a string has all unique characters.
What if you cannot use additional data structure.
*/
// solution 1, with an array, time complexity = O(n), space complexity = O(1)
public class solution {
public boolean isUniqueChar(String str) {
if(str == null || str.length() == 0)
return false;
if(str.length() > 128) return false;
boolean[] char_set = new boolean[256];
for(int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if(char_set[val])
return false;
char_set[val] = true;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment