Last active
March 27, 2019 06:38
Check given word is Anagram or not
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
function validAnagram(first, second) { | |
if (first.length !== second.length) { | |
return false; | |
} | |
const lookup = {}; | |
for (let letter of first) { | |
// if letter exists, increment, otherwise set to 1 | |
lookup[letter] ? lookup[letter] += 1 : lookup[letter] = 1; | |
} | |
console.log(lookup) | |
for (let letter of second) { | |
// can't find letter or letter is zero then it's not an anagram | |
if (!lookup[letter]) { | |
return false; | |
} else { | |
lookup[letter] -= 1; | |
} | |
} | |
return true; | |
} | |
// {a: 0, n: 0, g: 0, r: 0, m: 0,s:1} | |
validAnagram('anagrams', 'nagarams') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment