Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: OT: How to find anagrams?

by hv (Prior)
on Apr 28, 2004 at 00:35 UTC ( [id://348676]=note: print w/replies, xml ) Need Help??


in reply to OT: How to find anagrams?

One interesting approach I've considered, but never implemented, is to assign a prime number to each letter, and then hash the target word to the product of the values of its letters. The hash function for any word then divides the target product if and only if the letters of the word are a partial anagram - tricksy things like repeating letters are taken care of automatically. Then you just search for combinations of words whose hashes multiply together to give the target value; and if you calculate the hashes over the whole dictionary in advance and store in sorted order I think this could give pretty good performance.

For the standard 26 letter alphabet you'll need primes 2 .. 101, and you're going to need BigInts which will slow things down a bit (but not too badly if you use it with a fast maths package such as Math::Pari or Math::GMP). You can reduce the size of the numbers a bit further by assigning the lowest primes to the most frequently occurring letters.

Hugo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://348676]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found