Click to See Complete Forum and Search --> : Need Help with a word generator (anagram solver)


GI.Joe
December 13th, 2009, 08:56 PM
Hello everyone. I've been trying to find a way to solve this particular problem. I need to create a program where a word is entered by user and then the application will generate all the possible words from the letters of the word keyed in by the user. The words that can be possibly generated will be looked up from the following text file: http://java.sun.com/docs/books/tutorial/collections/interfaces/examples/dictionary.txt

So if I type in "team", the application should return the words "meat, mat, at, eat, tame, meat, mate,....etc."

I've looked at many examples of anagram solvers, but they only typically generate words of the same length of characters from the keyed in word: using "team" again I would get "team, tame, meat, mate" but not the shorter words, which I want.

If anyone could provide me with an algorithm of point me in the right direction, I would greatly appreciate it.

ProgramThis
December 14th, 2009, 08:54 AM
Well, one algorithm is to go letter by letter in your word, performing a lookup in your dictionary for each letter combination.

Example
t -> perform lookup
te -> perform lookup
ta -> perform lookup
tm -> perform lookup
tea -> perform lookup
tem -> perform lookup
team -> perform lookup
tema -> perform lookup

Switch first letter
e -> perform lookup
ea
em
et
eam
eat
eamt
eatm

Basically you are performing a lookup on all combinations of the given word. You should start by having a method that performs all letter combinations of the word, then simple loop through that list and perform checks against your dictionary.

While this isn't the fastest, most efficient algorithm for performing this operation, it certainly is one of the easiest to implement.

guyafe
December 14th, 2009, 10:38 AM
Consider the following pseudo code algorithm that creates all the permutations of all subsets of letters from a given word.
createPermutations(String word){
permutations <- empty set
for length = 1 to length of word{
createPermutations(permutations, word, length, empty string)
}
return permutations
}

createPermutations(permutations, restOfWord, length, wordToEnter){
if length = 0{
insert wordToEnter to permutations (if it is in the dictionary)
}
else{
for each letter in restOfWord{
wordToEnter <- concatanate letter to end of wordToEnter
restOdWord <- remove letter from restOfWord
createPermutatins(permutations, restOfWord, length - 1, wordToEnter)
}
}
}