Skip to content

Instantly share code, notes, and snippets.

@nickjacob
Created June 24, 2013 23:27
Show Gist options
  • Save nickjacob/5854664 to your computer and use it in GitHub Desktop.
Save nickjacob/5854664 to your computer and use it in GitHub Desktop.
just simple how 2 find anagrams
// space complexity: O(n) -- the hashmap
// time complexity: ~O(2n); iterate both strings once
// worse case: O(2n)
function areAnagrams(a, b) {
var len = a.length, chars = {}, i = 0;
if (len !== b.length) return false;
for(; i < len; i++) {
chars[a[i].toLowerCase()] = true;
}
for(i = 0; i<len; i++) {
if (!chars[b[i]].toLowerCase()) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment