Last active
October 13, 2019 15:09
-
-
Save wangchauyan/23edbcc32099ba6ced75 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
/* | |
Implement an algorithm to detemine if a string has all unique characters. | |
What if you cannot use additional data structure. | |
*/ | |
// solution 1, with an array, time complexity = O(n), space complexity = O(1) | |
public class solution { | |
public boolean isUniqueChar(String str) { | |
if(str == null || str.length() == 0) | |
return false; | |
if(str.length() > 128) return false; | |
boolean[] char_set = new boolean[256]; | |
for(int i = 0; i < str.length(); i++) { | |
int val = str.charAt(i); | |
if(char_set[val]) | |
return false; | |
char_set[val] = true; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment