I am trying to create this app on the iphone that given 6 letters, it would output all the possible 3-6 letter english words. I already have a dictionary, I just want to know how to do it.
I searched around and only found those scrabble solvers in python or those word search grid solutions.
I think a brute force search would do, but I'm concerned about the performance. Code is not necessary, a link to an algorithm or the algorithm itself would be fine, I think I'll be able to manage once I get that.
Thanks!
If you're concerned about performance, this method might do the trick. It involves some preprocessing but would allow for nearly-instantaneous lookup for anagrams.
Create a data structure that maps a String key to a List of Strings (I'm more familiar with Java, so in that case it would be a Map<String,List<String>>
) This will store your dictionary.
Define a function that takes a string and outputs the same letters arranged alphabetically. For example, hello
would become ehllo
; kitchen
would become cehiknt
. I'll refer to this function as keyify(word)
Here's the preprocessing part: for each item in your dictionary, find the list for that item's key (keyify(item)
) and add that item to the list.
When it comes time to look up anagrams of a given word, just look up the list at they keyify
of that word. For example, if the input was kitchen
, the keyify
would be cehiknt
, and looking that up in your map should result in a list containing kitchen
, chicken
and whatever other anagrams of kitchen that I forgot :P