Last active
August 29, 2015 14:18
-
-
Save jiaodong/0d6309d1694f6e08ad83 to your computer and use it in GitHub Desktop.
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 cannot use additional data structures. | |
public boolean isUnique(String s){ | |
int length = s.length(); | |
// ASCII table only has 128 unique characters, incluing commands | |
if(length > 256) return false; | |
boolean[] table = new boolean[256]; | |
for(int i = 0; i < length; i++){ | |
// Read ASCII int value of character | |
int code = (int) s.charAt(i); | |
if(!table[code]){ | |
table[code] = true; | |
} else { | |
return false; | |
} | |
} | |
// After iterating entire string there's no duplicate characters | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment