Implement an algorithm to determine if a string has all unique characters.


Solution1:

bool isUniqueChars2(string str) {
 bool[] char_set = new bool[256];
 for (int i = 0; i < str.length(); i++) {
 int val = str[i];
 if (char_set[val]) return false;
 char_set[val] = true;
 }
 return true;
 }

Time complexity is O(n), where n is the length of the string, and space complexity is O(n).

Solution 2:

public static boolean isUniqueChars(String str) {
int checker = 0;
 for (int i = 0; i < str.length(); ++i) {
 int val = str.charAt(i) - ‘a’;
 if ((checker & (1 << val)) > 0) return false;
 checker |= (1 << val);
 }
 return true;
}