package org.sanpra.jumble.WordSetComputers;

import org.sanpra.jumble.utils.DictionaryUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Simple brute force implementation - generate all possible combinations and test for validity against dictionary
 */
public class SimpleWordSetComputer implements WordSetComputer {
    @Override
    public Set<String> compute(String inputString) {
        Set<String> wordSet = generateAllPermutations(inputString);
        removeInvalidWordsFromSet(wordSet);
        return wordSet;
    }

    private void removeInvalidWordsFromSet(Set<String> wordSet) {
        if(wordSet == null) return;
        Iterator<String> wordSetIterator = wordSet.iterator();
        while(wordSetIterator.hasNext()) {
            if(!DictionaryUtils.isValidWord(wordSetIterator.next()))
                wordSetIterator.remove();
        }
    }

    private Set<String> generateAllPermutations(String inputString) {
        Set<String> permutations = new HashSet<>();
        permute("", inputString, permutations);
        return permutations;
    }

    private static void permute(String prefix, String remaining, Set<String> permutations) {
        if(permutations == null || prefix == null || remaining == null) return;
        if(remaining.isEmpty()) return;
        //recursion stopping condition - if remaining is single character, check
        if(remaining.length() == 1) {
            permutations.add(prefix + remaining);
            return;
        }

        //for each character in String remaining, append to prefix and generate permutations of remaining characters
        for(int counter = 0; counter < remaining.length(); counter++) {
            String newPrefix = prefix + remaining.charAt(counter);
            String newRemaining = remaining.substring(0, counter) + remaining.substring(counter+1, remaining.length());
            permute(newPrefix, newRemaining, permutations);
        }
    }


}

@@ -350,16 +350,76 @@
 puter %7B%0A
+%0A    private static final int MINIMUM_VALID_WORD_SIZE = 2;%0A%0A
     @Ove
@@ -1367,32 +1367,232 @@
 mpty()) return;%0A
+%0A        //generate all possible permutations for prefix as well, if it has 2 or more characters%0A        if(prefix.length() %3E= MINIMUM_VALID_WORD_SIZE)%0A            permute(%22%22, prefix, permutations);%0A%0A
         //recurs
@@ -1654,13 +1654,30 @@
 er, 
-check
+add to permutation set
 %0A   

Add code to generate permutations of strings with length>=2 and length<=inputString.length