Created
February 25, 2017 13:40
-
-
Save vitkarpov/3eff09ffe50a4712f96dd9dee6358870 to your computer and use it in GitHub Desktop.
"Cracking the coding interview", strings 1.1.2
This file contains 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
/** | |
* Определяет все ли символы в строке | |
* встречаются по одному разу. | |
* Подходит только для алфавита с количеством | |
* символов не более 32. В данном случае, a-z в нижнем регистре. | |
* | |
* @param {string} str | |
* @returns {boolean} | |
*/ | |
function isUniqueChars(str) { | |
// Вектор из 32 бит | |
let vector = 0; | |
const start = 'a'.charCodeAt(0); | |
for (let i = 0; i < str.length; i++) { | |
const delta = str.charCodeAt(i) - start; | |
if (vector & (1 << delta) > 0) { | |
return false; | |
} | |
vector |= 1 << delta; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Priority of binary and "&" is 8 and priority of compare operation "<" is 10. So you need to cover left side of condition in round brackets:
if ((vector & (1 << delta)) > 0)