Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Created February 25, 2017 13:40
Show Gist options
  • Save vitkarpov/3eff09ffe50a4712f96dd9dee6358870 to your computer and use it in GitHub Desktop.
Save vitkarpov/3eff09ffe50a4712f96dd9dee6358870 to your computer and use it in GitHub Desktop.
"Cracking the coding interview", strings 1.1.2
/**
* Определяет все ли символы в строке
* встречаются по одному разу.
* Подходит только для алфавита с количеством
* символов не более 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;
}
@ilowen
Copy link

ilowen commented Nov 30, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment