package leetcode;

import java.util.List;

/**
 * https://leetcode.com/problems/word-ladder-ii/
 */
public class Problem126 {
    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem126 prob = new Problem126();
    }
}

@@ -29,19 +29,147 @@
 va.util.
-Lis
+ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -360,132 +360,2247 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem126 prob = new Problem126();
+Set%3CString%3E words = new HashSet%3C%3E();%0A        for (String word : wordList) %7B%0A            words.add(word);%0A        %7D%0A        List%3CList%3CString%3E%3E result = new ArrayList%3C%3E();%0A        Min min = new Min();%0A        findLadders(beginWord, endWord, words, new ArrayList%3C%3E(), min, result);%0A        Iterator%3CList%3CString%3E%3E iter = result.iterator();%0A        while (iter.hasNext()) %7B%0A            List%3CString%3E list = iter.next();%0A            if (list.size() != min.val) %7B%0A                iter.remove();%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static class Min %7B%0A        private int val = Integer.MAX_VALUE;%0A    %7D%0A%0A    private static void findLadders(String startWord,%0A                                    String endWord,%0A                                    Set%3CString%3E words,%0A                                    List%3CString%3E accu,%0A                                    Min min,%0A                                    List%3CList%3CString%3E%3E result) %7B%0A        accu.add(startWord);%0A        for (int i = 0; i %3C startWord.length(); i++) %7B%0A            char%5B%5D chars = startWord.toCharArray();%0A            for (char c = 'a'; c %3C= 'z'; c++) %7B%0A                chars%5Bi%5D = c;%0A                String newString = new String(chars);%0A                if (newString.equals(endWord)) %7B%0A                    accu.add(endWord);%0A                    result.add(accu);%0A                    min.val = Math.min(min.val, accu.size());%0A                    return;%0A                %7D%0A                if (words.contains(newString)) %7B%0A                    List%3CString%3E newAccu = new ArrayList%3C%3E(accu);%0A                    words.remove(newString);%0A                    findLadders(newString, endWord, words, newAccu, min, result);%0A                    words.add(newString);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem126 prob = new Problem126();%0A%0A        System.out.println(prob.findLadders(%22foo%22, %22baz%22, Arrays.asList(%22buz%22, %22bug%22, %22bud%22, %22bar%22, %22fuz%22, %22foz%22,%0A            %22faz%22, %22gaz%22, %22boz%22))); // %5B%5Bfoo, foz, boz, baz%5D, %5Bfoo, foz, faz, baz%5D%5D%0A%0A        System.out.println(prob.findLadders(%22bar%22, %22baz%22, Arrays.asList(%22buz%22, %22bug%22, %22bud%22, %22bar%22, %22fuz%22, %22foz%22,%0A            %22faz%22, %22gaz%22, %22boz%22))); // %5B%5Bbar, baz%5D%5D
 %0A   

Update problem 126