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


In reply to Re: OT: How to find anagrams? by hv
in thread OT: How to find anagrams? by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.