题目:
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Return
[ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
Note:
- All words have the same length.
- All words contain only lowercase alphabetic characters.
链接: https//leetcode.com/problems/word-ladder-ii/
5/17/2017
算法班,抄答案
注意第12行,leetcode的要求是endWord需要在wordList当中。
做法是从头到位BFS算最小距离,从尾到头DFS求所有路径。
1 public class Solution { 2 public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { 3 List<List<String>> ret = new ArrayList<>(); 4 Set<String> wordSet = new HashSet(); 5 for (String word: wordList) { 6 wordSet.add(word); 7 } 8 if (!wordSet.contains(endWord)) { 9 return ret; 10 } 11 wordSet.add(beginWord); 12 // wordSet.add(endWord); 13 Map<String, List<String>> wordGraph = buildAdjacencyWords(wordSet); 14 Map<String, Integer> distance = new HashMap<String, Integer>(); 15 bfs(wordSet, wordGraph, distance, beginWord); 16 17 List<String> list = new ArrayList<String>(); 18 dfs(distance, beginWord, endWord, wordGraph, ret, list); 19 return ret; 20 } 21 22 private void dfs(Map<String, Integer> distance, String start, String end, Map<String, List<String>> wordGraph, List<List<String>> ret, List<String> list) { 23 list.add(end); 24 if (end.equals(start)) { 25 Collections.reverse(list); 26 ret.add(new ArrayList<String>(list)); 27 Collections.reverse(list); 28 } else { 29 for (String neighbor: wordGraph.get(end)) { 30 if (distance.containsKey(neighbor) && distance.get(end) == distance.get(neighbor) + 1) { 31 dfs(distance, start, neighbor, wordGraph, ret, list); 32 } 33 } 34 } 35 list.remove(list.size() - 1); 36 } 37 38 private void bfs(Set<String> wordSet, Map<String, List<String>> wordGraph, Map<String, Integer> distance, String start) { 39 Queue<String> queue = new LinkedList<String>(); 40 queue.offer(start); 41 distance.put(start, 0); 42 43 while (!queue.isEmpty()) { 44 String s = queue.poll(); 45 for (String neighbor: wordGraph.get(s)) { 46 if (!distance.containsKey(neighbor)) { 47 queue.offer(neighbor); 48 distance.put(neighbor, distance.get(s) + 1); 49 } 50 } 51 } 52 } 53 54 private Map<String, List<String>> buildAdjacencyWords(Set<String> wordSet) { 55 Map<String, List<String>> map = new HashMap<String, List<String>>(); 56 57 for (String word: wordSet) { 58 map.put(word, new ArrayList<String>()); 59 for (int i = 0; i < word.length(); i++) { 60 char c = word.charAt(i); 61 for (char l = 'a'; l <= 'z'; l++) { 62 String other = word.substring(0, i) + l + word.substring(i + 1, word.length()); 63 if (wordSet.contains(other)) { 64 map.get(word).add(other); 65 } 66 } 67 } 68 } 69 return map; 70 } 71 }
别人的答案:
一种BFS有向图+DFS,一种是用2个set???
https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj
https://discuss.leetcode.com/topic/27504/my-concise-java-solution-based-on-bfs-and-dfs
two end BFS
https://discuss.leetcode.com/topic/17975/super-fast-java-solution-two-end-bfs
更多讨论