@Test public void onlyReturnShortest2() throws Exception { Solution solution = new Solution(); List<List<String>> ladders = solution.findLadders("cat", "fin", new HashSet<>(Arrays.asList("cat", "fat", "fit", "fin", "fan", "can", "mat", "mit"))); assertThat(ladders.get(0), hasItems("cat", "can", "fan", "fin")); assertThat(ladders.get(1), hasItems("cat", "fat", "fan", "fin")); assertThat(ladders.get(2), hasItems("cat", "fat", "fit", "fin")); }
public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) { wordList.remove(endWord); if (isOneLetterDiff(beginWord, endWord)) { return singletonList(Arrays.asList(beginWord, endWord)); } else { Node<String> root = new Node<>(endWord, null); List<Node<String>> leaves = singletonList(root); while (!isFound) { leaves = goOneLevelDeeperForEach(leaves, beginWord, wordList); if (leaves.size() == 0) { break; } } return leaves.stream() .map(Solution::allWords) .collect(Collectors.toList()); } }
Solution { public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) { wordList.remove(endWord); if (isOneLetterDiff(beginWord, endWord)) { return singletonList(Arrays.asList(beginWord, endWord)); } else { Node<String> root = new Node<>(endWord, null); List<Node<String>> leaves = singletonList(root); while (!isFound) { leaves = goOneLevelDeeperForEach(leaves, beginWord, wordList); if (leaves.size() == 0) { break; } } return leaves.stream() .map(Solution::allWords) .collect(Collectors.toList()); } } }
Solution { public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) { wordList.remove(endWord); if (isOneLetterDiff(beginWord, endWord)) { return singletonList(Arrays.asList(beginWord, endWord)); } else { Node<String> root = new Node<>(endWord, null); List<Node<String>> leaves = singletonList(root); while (!isFound) { leaves = goOneLevelDeeperForEach(leaves, beginWord, wordList); if (leaves.size() == 0) { break; } } return leaves.stream() .map(Solution::allWords) .collect(Collectors.toList()); } }  }
Solution { public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) { wordList.remove(endWord); if (isOneLetterDiff(beginWord, endWord)) { return singletonList(Arrays.asList(beginWord, endWord)); } else { Node<String> root = new Node<>(endWord, null); List<Node<String>> leaves = singletonList(root); while (!isFound) { leaves = goOneLevelDeeperForEach(leaves, beginWord, wordList); if (leaves.size() == 0) { break; } } return leaves.stream() .map(Solution::allWords) .collect(Collectors.toList()); } }  List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList); static boolean isOneLetterDiff(String a, String b); }
Solution { public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) { wordList.remove(endWord); if (isOneLetterDiff(beginWord, endWord)) { return singletonList(Arrays.asList(beginWord, endWord)); } else { Node<String> root = new Node<>(endWord, null); List<Node<String>> leaves = singletonList(root); while (!isFound) { leaves = goOneLevelDeeperForEach(leaves, beginWord, wordList); if (leaves.size() == 0) { break; } } return leaves.stream() .map(Solution::allWords) .collect(Collectors.toList()); } }  List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList); static boolean isOneLetterDiff(String a, String b);  }