Last active
December 8, 2020 16:50
-
-
Save BretCameron/e364b64792a06d3295830cdad56704ad to your computer and use it in GitHub Desktop.
Return true if two strings are anagrams of one another
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
const isAnagram = (str1, str2) => { | |
if (str1.length !== str2.length) return false; | |
const map = new Map(); | |
for (let char of str1) { | |
const count = map.has(char) ? map.get(char) + 1 : 1; | |
map.set(char, count); | |
}; | |
for (let char of str2) { | |
if (!map.has(char)) return false; | |
const count = map.get(char) - 1; | |
if (count === 0) { | |
map.delete(char); | |
continue; | |
}; | |
map.set(char, count); | |
}; | |
return map.size === 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment