## Determine if a string has all unique characters

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

My initial thoughts:

1. If additional data structures are allowed, then apparently HashTable comes to mind.
2. 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.

My initial codes:

```public static boolean doesStringHasAllUniqueChars(String s) {
int[] table = new int[256];
for(char c : s) {
int key = c % 256;
if(table[key] == 0)
table[key] = 1;
else
return false;
return true;
}

Compiling error at highlighted line. String s: Can only iterate over an array or an instance of java.lang.Iterable. Change to s.toCharArray().
Everything else looks just fine. Test for empty string, string with unique chars and string with repeated chars.

Solution:
public static boolean isUniqueChars2(String str) {
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;
}

Using boolean table is much better than my solution. It saves spaces.
You don’t need to mod 256 if you assume there are <256 chars.
Time complexity O(n); space complexity O(n).

Yet another crazy solution is given by:
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;
}

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.

__ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5bcfbdca6428c', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942-5bcfbdca642b1',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5bcfbdca642b3',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});

Related
```