View Sidebar
Fast Anagrammer (A Magma Transfer)

Fast Anagrammer (A Magma Transfer)

November 12, 2012 3:56 pmComments are Disabled

This was a fun coding project. Using Bootstrap.js and some simple PHP, I coded an anagrammer that performs much, much faster than existing anagram sites. I don’t know what they are using to anagram (or de-anagram), but execution is taking forever with sites like wordsmith.org and anagrammer.com; in addition, sites like wineverygame.com have a 12-character limit on their anagrammer. Mine has a 60-character limit.

Basically, the way it works is, after accessing the dictionary which is stored on the server, the anagrammer immediately eliminates all words that are too long (which could be 50% or more of the words). Then it starts a while loop and runs through each word, which is then broken down using str_split into its component letters. The magic is in a two-step elimination process:

if(strpos($input,$letter) === false){
 return false;
}

if(substr_count($word,$letter)>substr_count($input,$letter)){
 return false;
}

The first, checks to see if the component letter is even part of the input string; if not, it is thrown out. The second checks to see if the component letter appears more in the dictionary word than it does in the input. If so, it is thrown out.

After that, it’s just a question of stacking an array and sorting by string length.

This uses a standard TWL dictionary which has 178,691 words.

Comments are closed