Finding Anagrams

SolveX is an app on my site which can be used to search for words where some of the letters are known. The user enters the word with spaces or ‘.’ indicating the unknown letters. The app will display matching words.

It can also be used to search for anagams. To find anagrams the user can enter some text and the app will display anagrams for that text.

Earlier today I was stuck on the final clue in a crossword. The clue was “Dark hen’s dicky bird (8)”. I already had some of the letters from the clues down ‘R_D_H_N_’. I could have entered this in the app and retrieved matching words, but I suspected the answer was an anagram of the first part of the clue ‘Dark hen’s’. Racking my brain I was unable to come up with a solution, so finally entered it in the app, and clicked on the Anagrams button. The answer came back almost instantaneously; I won’t reveal it here in case you want to solve it yourself.

I have been contacted by users in the past complaining that the app has not been able to find solutions for words or anagrams they had tried. I have explained to them that the app is limited by the size of it’s database. As at today it holds 95,134 words but obviously that is only a subset of English words. Incidentally, the same database is used for the myWord app on the site.

So, how does SolveX find matching words? It uses SQL (SQL stands for Structured Query Language). SQL is used for managing data stored in relational databases. One of the many ficilities available in SQL is searching for text in data fields using the wildcards ‘%’ and ‘_’, where the ‘%’ means any number of characters and the ‘_’ means one character. So, in the above case the app would run the SQL statement “‘SELECT Word FROM Words WHERE WORD LIKE ‘R_D_H_N'” (the database table holding the words is called Words, with each individual Word held in the Word field).

What about anagrams? STOP is an anagram of POST, because all the letters of STOP are also in POST. But STOOP would not be a valid anagram, so as a first step we could refine our algorithm to say both words must have the same number of characters. But then, how do we deal with the words STOOP and POSTS. Both words are the same length and each word has all the letters from the other word, but obviously they are not anagrams, because STOOP contains 2 occurences of O and 1 of S, whereas POSTS has 2 of S and only 1 of O.

My app solves this problem by implementing something I’ve called the baseString function. This takes a piece of text and sorts it by it’s letters. So, in this case we would have the following baseStrings: OOPST and OPSST. These are difference so the original words are not anagrams.

For my crossword problem it takes the characters “Dark hen’s’ and generates a baseString as ‘ADEHKNRS’ (ignoring any non-alphabetic characters) . It then look through the database for any words having the same baseString. It finds and outputs one word with this baseString.

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *