This is one of the questions in the Cracking the Coding Interview book by Gayle Laakmann McDowell:
Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
The author wrote:
We can reduce our space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case
'a'
through'z'
. This will allow us to use just a single int.
The author has this implementation:
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;
}
Let's say we get rid of the assumption that "the string is only lower case 'a'
through 'z'
". Instead, the string can contain any kind of character—like ASCII characters or Unicode characters.
Is there a solution as efficient as the author's (or a solution that comes close to being as efficient as the author's)?
Related questions:
- Detecting if a string has unique characters: comparing my solution to "Cracking the Coding Interview?"
- Explain the use of a bit vector for determining if all characters are unique
- String unique characters
- Implementing an algorithm to determine if a string has all unique characters
- determine if a string has all unique characters?
for the asccii character set you can represent the 256bits in 4 longs: you basically hand code an array.
You can use the following code to generate the body of a similar method for unicode characters: