Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Last active February 25, 2017 13:02
Show Gist options
  • Save vitkarpov/cdaa756f5406922f1431576812e4f044 to your computer and use it in GitHub Desktop.
Save vitkarpov/cdaa756f5406922f1431576812e4f044 to your computer and use it in GitHub Desktop.
"Cracking the coding interview", strings 1.1
/**
* Определяет все ли символы в строке
* встречаются по одному разу.
*
* @param {string} str
* @returns {boolean}
*/
function isUniqueChars(str) {
// Строка из набора символов ASCII,
// поэтому максимальная длина строки 128
const set = new Array(128);
if (str.length > 128) {
return false;
}
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (set[code]) {
return false;
}
set[code] = true;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment