@Test public void shouldAvoidBackLooping() throws Exception { Solution solution = new Solution(); List<List<String>> ladders = solution.findLadders("leet", "code", new HashSet<>(Arrays.asList("lest", "leet", "lose", "code", "lode", "robe", "lost"))); assertThat(ladders.get(0), hasItems("leet", "lest", "lost", "lose", "lode", "code")); }
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);  }