Last active
August 29, 2015 14:18
-
-
Save michaelniu/2b74fae8c0025a9a3122 to your computer and use it in GitHub Desktop.
CC150 Practice
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
/* | |
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