Skip to content

Instantly share code, notes, and snippets.

@michaelniu
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 michaelniu/2b74fae8c0025a9a3122 to your computer and use it in GitHub Desktop.
Save michaelniu/2b74fae8c0025a9a3122 to your computer and use it in GitHub Desktop.
CC150 Practice
/*
1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
* 1.is it only ascII charactors
* 2.use an char Array with total 256 readable charactors
* check if any of element in the array contains value
* like boolean [] exsiting
* if(existing[char] == true)
* return false;
* else
* existing[char] == true;
*
Time Complexity O(n) Space O(1)
*/
public class UniqueCharInString_1_1 {
public static void main(String[] args){
String testStr ="fdsfah";
System.out.println(checkStringAllUniqu(testStr));
}
private static boolean checkStringAllUniqu(String str){
boolean[] allCharArray = new boolean[256];
if(str == null )
return true;
if(str.length()>256)
return false;
for(int i=0; i<str.length();i++){
int curchar = str.charAt(i);
if(allCharArray[curchar] )
return false;
allCharArray[curchar]=true;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment