Last active
August 28, 2018 07:43
-
-
Save lienista/fdc36b327c4829ff592c7092873471c2 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const stringHasUniqueChars = (str) => { | |
| let checker = 0; //integer used to represent 32-bit operator | |
| for (let i = 0; i < str.length; i++) { | |
| let bitAtIndex = str[i] - 'a'; | |
| if ((checker & (1 << bitAtIndex)) > 0) { | |
| return false; | |
| } | |
| checker = checker | (1<<bitAtIndex); | |
| } | |
| return true; | |
| } | |
| let s = 'Geeks for Geeks'; | |
| let y = stringHasUniqueChars(s); | |
| let x = 'abc'; | |
| let z = stringHasUniqueChars(x); | |
| console.log(s + ': ' + y); | |
| console.log(x + ': ' + z); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment