0

I am currently working on a programming assignment where the user inputs a word

i.e. "that"

and the program should return all valid words that can be made from the given string

i.e. [that, hat, at]

The issue I am having is that the resulting words should be created using a recursive method that checks if the prefix is valid.

i.e. if the given word is "kevin" once the program tries the combination "kv" it should know that no words start with kv and try the next combination in order to save time.

Currently my code just creates ALL permutations which takes a relatively large amount of time when the input is larger than 8 letter.

protected static String wordCreator(String prefix, String letters) {
    int length = letters.length();

        //if each character has been used, return the current permutation of the letters
        if (length == 0) {
            return prefix;
        }
        //else recursively call on itself to permute possible combinations by incrementing the letters
        else {
            for (int i = 0; i < length; i++) {
                words.add(wordCreator(prefix + letters.charAt(i), letters.substring(0, i) + letters.substring(i+1, length)));
            }
        }
        return prefix;
    }

If anyone could help me figure this out I'd be much appreciated. I am also using an AVL tree to store the dictionary words for validation incase that is needed.

  • From what you give, there is no reason to compute *permutations* (check on wikipedia the definition of this term, if necessary). Unless your example is incorrect, of course ... – M. Page Dec 02 '14 at 19:04
  • I am not sure if I understand your question correctly. Just to clarify, do you want to generate all possible words of any length that can be generated using characters from the given word with given multiplicity?? – Ashu Pachauri Dec 02 '14 at 22:42
  • @AshuPachauri Yes I am looking to generate all possible words of any length using the string provided by the user. I'm not exactly sure what you mean by "with the given multiplicity" – EndlessProgression Dec 03 '14 at 00:11
  • How can the program know that if "kv" fails then "kvn" also will fail? Is your dictionary some kind of trie? – Jim Mischel Apr 06 '15 at 14:44

0 Answers0