DEV Community

Discussion on: Anagrams Checker - Three JavaScript Solutions

 
codemouse92 profile image
Jason C. McDonald

Hmm, hadn't considered that angle....

Thread Thread
 
evanoman profile image
Evan Oman

Prime factorizations are unique so if you map each letter to a prime and then take the product of those primes, you'd have a unique representation of the characters in the word. See this classic tweet for a better explanation.

Thread Thread
 
aadibajpai profile image
Aadi Bajpai

Not very efficient or fast, however.

Thread Thread
 
evanoman profile image
Evan Oman

I mean its time complexity is O(n)-ish but yeah the large number multiplication would get you for longer words.

Thread Thread
 
aadibajpai profile image
Aadi Bajpai

26th prime is 101 so I imagine zzzzzzzzzzzzz et all would take more time

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

It's not efficient, but I wonder if a Cantor pairing of the ord() values would work. It would certainly be optimal if all the strings were two letters. The trouble is, those numbers get very big, very fast.