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?
checker
. – Cymbiformint
, aBitSet
will be handy for larger data sets – CalefacientBitSet
probably violates the condition you can't use additional data structures. But then again, since the author is using anint
as a set of 26 bits, you could argue that theint
is really being used as a data structure and thus she's violated her own condition. – Gerrardint
bit vector she used). – Senatorial