)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Validate Anagram

  1. function normalizeString(word) {
  2.   word = word.toLowerCase()
  3.   const result = normalizeString.memo[word]
  4.   if (result) return result
  5.   return normalizeString.memo[word] =
  6.     [...word.replace(/\s/g, '')]
  7.       .sort() + ''
  8. }
  9. normalizeString.memo = {}
  10.  
  11. const checkAnagram = (word, source) =>
  12.   normalizeString(word) === normalizeString(source)
  13.  
  14.  
  15. // try it out 
  16.  
  17. const target = 'adorn hair'
  18. const anagrams = [
  19.   'hoard rain',
  20.   'hair radon',
  21.   'hadron air',
  22.   'hairdo ran',
  23.   'radian rho',
  24.   'au revoir fail'
  25. ];
  26.  
  27. anagrams.forEach(anigram => { 
  28.   console.log(anigram, '=', checkAnagram(anigram, target))
  29. })

Check if two strings are anagrams of one another.

snippet.zone ~ 2021-24 /// {s/z}