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 {

    private static final int MINIMUM_VALID_WORD_SIZE = 2;

    @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;

        //generate all possible permutations for prefix as well, if it has 2 or more characters
        if(prefix.length() >= MINIMUM_VALID_WORD_SIZE)
            permute("", prefix, permutations);

        //recursion stopping condition - if remaining is single character, add to permutation set
        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);
        }
    }


}

@@ -418,755 +418,2068 @@
     
-@Override%0A    public Set%3CString%3E compute(String inputString) %7B%0A        Set%3CString%3E wordSet = generateAllPermutations(inputString);%0A        removeInvalidWordsFromSet(wordSet);%0A        return wordSet;%0A    %7D%0A%0A    private void removeInvalidWordsFromSet(Set%3CString%3E wordSet) %7B%0A        if(wordSet == null) return;%0A        Iterator%3CString%3E wordSetIterator = wordSet.iterator();%0A        while(wordSetIterator.hasNext()) %7B%0A            if(!DictionaryUtils.isValidWord(wordSetIterator.next()))%0A                wordSetIterator.remove();%0A        %7D%0A    %7D%0A%0A    private Set%3CString%3E generateAllPermutations(String inputString) %7B%0A        Set%3CString%3E permutations = new HashSet%3C%3E();%0A        permute(%22%22, inputString, permutations);%0A        return permutations;%0A    %7D%0A
+/**%0A     * Generates a set of valid words that can be formed from the characters in the input string%0A     * @param inputString Input string%0A     * @return The set of valid words%0A     *%0A     * If inputString is null or empty, returns empty set%0A     */%0A    @Override%0A    public Set%3CString%3E compute(String inputString) %7B%0A        if(inputString == null %7C%7C inputString.isEmpty())%0A            return new HashSet%3C%3E();%0A        Set%3CString%3E wordSet = generateAllPermutations(inputString);%0A        removeInvalidWordsFromSet(wordSet);%0A        return wordSet;%0A    %7D%0A%0A    /**%0A     * Removes all invalid words from wordSet. Word validity is based on call to DictionaryUtils.isValidWord%0A     * @param wordSet Set of strings. If wordSet is empty or null, returns with no changes.%0A     */%0A    private void removeInvalidWordsFromSet(Set%3CString%3E wordSet) %7B%0A        if(wordSet == null) return;%0A        Iterator%3CString%3E wordSetIterator = wordSet.iterator();%0A        while(wordSetIterator.hasNext()) %7B%0A            if(!DictionaryUtils.isValidWord(wordSetIterator.next()))%0A                wordSetIterator.remove();%0A        %7D%0A    %7D%0A%0A    /**%0A     * Generate all possible permutations of characters in inputString, of length%3E=2%0A     * @param inputString String containing characters for which permutations need to be computed.%0A     * @return  A Set of String objects containing all possible permutations for characters in inputString%0A     *          If inputString is empty or null, returns empty set%0A     */%0A    private Set%3CString%3E generateAllPermutations(String inputString) %7B%0A        Set%3CString%3E permutations = new HashSet%3C%3E();%0A        permute(%22%22, inputString, permutations);%0A        return permutations;%0A    %7D%0A%0A    /**%0A     * Generates all possible permutations of characters(of length%3E=2) in remaining, appends them to prefix and stores the strings in permutations%0A     * @param prefix Prefix string for permutation%0A     * @param remaining  Remaining characters for which permutations need to be generated%0A     * @param permutations   Set in which permutations must be stored%0A     */
 %0A   

Add javadoc for all methods. Also add string null/empty check for compute()