Created
February 26, 2017 11:58
-
-
Save vitkarpov/0035a4e97e7b77802876da8256c7004b to your computer and use it in GitHub Desktop.
"Cracking the coding interview", strings 1.2.2
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
/** | |
* Определяет является ли одна строка перестановкой другой. | |
* Используются счетчики символов. Сложность 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