Validate Anagram
function normalizeString(word) {
word = word.toLowerCase()
const result = normalizeString.memo[word]
if (result) return result
return normalizeString.memo[word] =
[...word.replace(/\s/g, '')]
.sort() + ''
}
normalizeString.memo = {}
const checkAnagram = (word, source) =>
normalizeString(word) === normalizeString(source)
// try it out
const target = 'adorn hair'
const anagrams = [
'hoard rain',
'hair radon',
'hadron air',
'hairdo ran',
'radian rho',
'au revoir fail'
];
anagrams.forEach(anigram => {
console.log(anigram, '=', checkAnagram(anigram, target))
})
Check if two strings are anagrams of one another.