Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Created February 26, 2017 11:58
Show Gist options
  • Save vitkarpov/0035a4e97e7b77802876da8256c7004b to your computer and use it in GitHub Desktop.
Save vitkarpov/0035a4e97e7b77802876da8256c7004b to your computer and use it in GitHub Desktop.
"Cracking the coding interview", strings 1.2.2
/**
* Определяет является ли одна строка перестановкой другой.
* Используются счетчики символов. Сложность O(n).
*
* @param {string} a
* @param {string} b
* @returns {boolean}
*/
function isPermutation(a, b) {
// ASCII
const letters = new Array(128).fill(0);
for (let i = 0; i < a.length; i++) {
letters[a.charCodeAt(i)]++;
}
for (let i = 0; i < b.length; i++) {
if (--letters[b.charCodeAt(i)] < 0) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment