DEV Community

Discussion on: Learning Algorithms with JS, Python and Java 7: Anagrams

Collapse
 
kepta profile image
Kushan Joshi • Edited

Great article Tommy!

I love finding smaller solutions to problems, here is a similar attempt to your question on anagrams.

function anagrams(a, b) {
  a = a.replace(/[^\w]/g, "").toLowerCase();
  b = b.replace(/[^\w]/g, "").toLowerCase();

  if (a.length !== b.length) return false;

  return [...a].sort().join() ===  [...b].sort().join()
}

Enter fullscreen mode Exit fullscreen mode