CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Posts
    2

    Need Help with a word generator (anagram solver)

    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/tutor...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.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Need Help with a word generator (anagram solver)

    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.

  3. #3
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Need Help with a word generator (anagram solver)

    Consider the following pseudo code algorithm that creates all the permutations of all subsets of letters from a given word.
    Code:
    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)
    		}
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured