Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures?

My initial thoughts:

If additional data structures are allowed, then apparently HashTable comes to mind.

There are 128(256) ASCII characters, so we can use an auxiliary array of size 256, each cell stores the count of that particular character. If the count is greater than 1, then it’s the sign of repetition of characters.

Which assumes that the characters are from ‘a’ to ‘z’. Let me explain what this is:

‘checker’ is the bit vector. checker[i] = 0 if ASCII code ‘a’ + i has not been presented; 1 otherwise. That is what line 6 is doing. | is inclusive OR (i.e., the general sense OR).

Within loop, compare wether a bit-position has already been filled (i.e., value 1). This is done by logical and. That is what line 5 is doing. & is bitwise AND. << shifts the left-hand operand to right-hand bits to the left.

Other thoughts:

Check every char of the string with every other char of the string for duplicate occurrences. This will take O(n^2) time and no space.

If we are allowed to destroy the input string, we could sort the string in O(n log n) time and then linearly check the string for neighboring characters that are identical. Careful, though – many sorting algorithms take up extra space.