Skip to content

Instantly share code, notes, and snippets.

@JamesJi9277
Created March 30, 2015 12:49
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 JamesJi9277/8ca1fc9a81316f40bd65 to your computer and use it in GitHub Desktop.
Save JamesJi9277/8ca1fc9a81316f40bd65 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 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;
}
@xiaotdl
Copy link

xiaotdl commented Mar 31, 2015

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.

@xiaotdl
Copy link

xiaotdl commented Mar 31, 2015

Is there a chance that str.length()<0?

@xiaotdl
Copy link

xiaotdl commented Mar 31, 2015

Unicode case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment