Adam Howitt's Blog

Dec 01
2003

Howitt's Anagram Theory

I am reading Dan Brown's "The Da Vinci Code" and in addition to the fact that it is a great read chock full of geek codebreaking goodness, it renewed my interest in anagrams.

Employing my best Java to write an anagram generator I am trying to work out the quickest way to generate all the possible anagrams of a sentence in as little time as possible.

During my search I came across what appears to be a solid fact about anagrams (please post a comment if you find it to be inaccurate): the total number of unique anagrams generated from an input string n characters long will be n! (factorial) divided by the the product of all the u! (factorial) where u is a unique character in the input string e.g. aaabb will be 5!/(3!*2!) = 10

I haven't found a case where this doesn't work but similarly don't have a full mathematical proof either.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. Java! Bah! Do it in CF! It'd be a piece of cake. About 5 years ago, I wrote a search engine that was basically an anagram generator, except with words. Basically you type in a word and it would perform searches on every combination of the words you enter. like: "hammer nails wood" would search for products with: "hammer and nails and wood", then "hammer and nails", then "hammer and wood", then "nails and wood, then hammer, then nails, then wood. It was sweet cause you got more results with the more words you typed in, instead of less. The most relavent results at the top.

    btw, find a database of english words and have your anagram generator search for each word in the dictionary and filter out garbage words. That'll make it more interesting. :-)

  2. I have this written as a C program for VMS, but it should compile anywhere if you are interested. Of course it requires a Unix dictionary file.

[Add Comment]