Java anagram recursion List<List<String>> only storing empty lists<Strings>

Posted by Riff Rafffer on Stack Overflow See other posts from Stack Overflow or by Riff Rafffer
Published on 2012-11-02T22:51:09Z Indexed on 2012/11/02 23:00 UTC
Read the original article Hit count: 345

Filed under:
|

Hi In this recursion method i am trying to find all anagrams and add it to a List> but what happens when i run this code is it just returns alot of empty Lists.

      private List<List<String>> findAnagrams(LetterInventory words,
        ArrayList<String> anagram, int max,
        Map<String, LetterInventory> smallDict, int level, List<List<String>> result) {

    ArrayList<String> solvedWord = new ArrayList<String>();
    LetterInventory shell;
    LetterInventory shell2;
    if (level < max || max == 0) {
        Iterator<String> it = smallDict.keySet().iterator();
        while (it.hasNext()) {
            String k = it.next();
            shell = new LetterInventory(k);
            shell2 = words;
            if (shell2.subtract(shell) != null) {
                anagram.add(k);
                shell2 = words.subtract(shell);
                if (shell2.isEmpty()) {
                    //System.out.println(anagram.toString()); it prints off fine here
                                            result.add(anagram); // but doesnt add here

                }
                else 

                    findAnagrams(shell2, anagram, max, smallDict, level + 1, result);
                anagram.remove(anagram.size()-1);
            }
        }
    }
    return results;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about recursion