[tabby title=”Task”]

Implement an algorithm to determine if a string (of characters from ‘a’ to ‘z’) has all unique characters or not.

**Example 1:**

var s = "abcde"; returns true;

**Example 2:**

var s = "abcade"; returns false;

[tabby title=”Solution”]

**Solution 1:** The brute force solution will be to iterate through all characters and compare with all other characters.

function areCharactersUnique(s) { for(let i=0; i < s.length; i++) { for(let j=0; j < s.length; j++) { if(i == j) continue; if(s[i] == s[j]) { return false; } } } return true; } var s = "abcade"; console.log(areCharactersUnique(s));

**Solution 2:** Using an array (or hashMap table) with key equal to the ASCII character code.

function areCharactersUnique(s) { var checker = new Array(26); for(let i=0; i < s.length; i++) { var pos = s[i].charCodeAt(0) - 'a'.charCodeAt(0); if(typeof checker[pos] != 'undefined') { return false; } checker[pos] = 1; } return true; } var s = "abcde"; console.log(areCharactersUnique(s));

**Let’s make it more challenging and prohibit the use of additional data structures like count array, hash, etc.**

**Solution 3: **Using bitwise operations to store into 32 bit if one of all 26 characters is presented or not.

We have 26 letters (from a to z). Let’s imagine that we could have 26 empty slots that we could set up to true if the character exists, pretty much as if we have a hashTable.

for simplicity I will use only 6 slots (from a to G) instead of all 26 that represent the whole alphabet.

In addition we have to mention that the slots are actually 32 (this is usually the length of an integer in JavaScript but we need only 26)

6 | 5 | 4 | 3 | 2 | 1 | 0 |

G | F | E | D | C | B | A |

false | false | false | false | false | false | false |

Given the string: ‘ABCG’ for example we will end up with this matrix.

6 | 5 | 4 | 3 | 2 | 1 | 0 |

G | F | E | D | C | B | A |

true | false | false | false | true | true | true |

But this could be stored into 32bit value using bitwise operations. The binary representation of the matrix above will be: 1000111

The solution:

function areCharactersUnique(s) { var checker = 0; for(let i=0; i < s.length; i++) { // Charcode of a is 97 but we want to start with 0 var val = s[i].charCodeAt(0) - 'a'.charCodeAt(0); // & - Sets each bit to 1 if both bits are 1 // examples: // 1 & 10 = 0 // 1 & 101 = 1 if(checker & ( 1 << val)) { return false; } // | - Sets each bit to 1 if one of two bits is 1 // examples: // 1 | 10 = 11 // 1 | 1 = 1 checker = checker | ( 1 << val); } return true; }

*what we just did:*

*– we started with creating a loop to go through all characters*

*– we set up an empty value checker to store if the character is used or not (this is the binary representation of the matrix above)*

*– (line 6) grabbing the value for each letter in the string but removing ‘a’ = 97 so ‘a’ character will be equal to 0 and z = 26*

*– (line 19) we are setting the position of the character into the*

`checker`

to true using bitwise shift left `(1 << a)`

and preserving other already set positions using bitwise `|`

‘or’*– (line 11) using the same technique but with bitwise*

`&`

‘and’ we check if the character position is set to true or not.Here is a step by step example for ‘ABCG’ character:

– initially `checker = 0 // or the binary representation is '00000000...0'`

– we are going to insert `a`

using **Zero fill left shift**. This is basically going to add ‘1’ followed by as many ‘0’ to the right of the checker as the value of ‘a’ is. In this case 0, so `schecker = 1 // binary '00000000...1`

– next step inserting ‘b’ follows the same procedure: b = 1, `(1 << b) = 2 '000000...10'`

but we also want to preserve whatever was already inserted so we use bitwise ‘|’ ‘or’ which sets each bit to 1 if one of two bits is 1.

so

`checker = checker | (1 << b) = 2`

or the binary representation will be:

`checker = '00000000...1' | (1 << b) = '000000000...11'`

and the same for the rest of the characters

var a = 0; var b = 1; var c = 2; var d = 3; var e = 4; var f = 5; var g = 6; var s = "abcg"; var checker = 0; checker = checker | (1 << a); // 1 checker = checker | (1 << b); // 11 checker = checker | (1 << c); // 111 checker = checker | (1 << g); // 1000111

**Let’s modify the problem, and ask to return the index of the first unique character in the string.
For example for string ‘abcac’ the return will be the index of b – ‘1’
This problem is asked in Leetcode**

/** * @param {string} s * @return {number} */ var firstUniqChar = function(s) { let lastSingle = null; let hashMap = {}; for(var i = 0;i < s.length; i ++) { var val = s[i]; hashMap[val] = hashMap[val] == undefined ? i: 'not-unique'; } for(let i=0; i < s.length; i++) { var key = s[i]; if(hashMap[key] != 'not-unique') { return hashMap[key]; } } return -1; }

[tabbyending]