Created
March 30, 2015 12:49
-
-
Save JamesJi9277/8ca1fc9a81316f40bd65 to your computer and use it in GitHub Desktop.
1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Implement an algorithm to determine if a string has all unique characters. | |
//What if you cannot use additional data structure | |
//first ask the string is Unicode string or ASCII string | |
// Unicode is a superset of ASCII, ASCII only defines 128 characters, which mappes 0-127 | |
// but Unicode defines 2^21(maybe less than), and mappes to 0-2^21, | |
// but the ASCII can be the same meaning in the Unicode | |
//the length of string is str.length() | |
//the length of array is array.length | |
//array[i], str.charAt(i). array=>[], string => () | |
//time complexity is O(n) | |
public Solution{ | |
public boolean isUnique(String str){ | |
if(str == null || str.length() > 128 || str.length()< 0) | |
return false; | |
boolean check = new boolean[256]; | |
for(i = 0;i<str.length();i++) | |
{ | |
int val = str.charAt(i); | |
if(check[val]) | |
return false; | |
else check[val] = true; | |
} | |
}return true; | |
} |
Is there a chance that str.length()<0?
Unicode case?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you instantiate a boolean array with size of 256 instead of 128?
Note that ascii has 128 chars and ISO-8859-1(Latin 1) has 256 chars.