程序媛詹妮弗
终身学习

 

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return an empty list if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: []

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

 

 

代码

 1 class Solution {
 2     public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
 3         // bfs
 4         HashSet<String> dict = new HashSet<>(wordList);
 5         dict.add(beginWord);
 6         HashMap<String, Integer> distance = new HashMap<>();
 7         HashMap<String, List<String>> adj = new HashMap<>();
 8         bfs(beginWord, endWord, dict, adj, distance);   
 9         //dfs
10         List<List<String>> result = new ArrayList<>();
11         List<String> path = new ArrayList<>();
12         path.add(beginWord);
13         dfs(beginWord, endWord, result, path, dict, adj, distance);
14         return result;
15     }
16     
17     public List<String> getNeighbor(String s, HashSet<String> dict) {
18         List<String> list = new ArrayList<>(); 
19         for (int i = 0; i < s.length(); i++) {
20             char[] charArray = s.toCharArray();
21             for (char c = 'a'; c <= 'z'; c++) { 
22                 if (charArray[i] == c)  continue;
23                 charArray[i] = c;
24                 String temp = new String(charArray);
25                 if (dict.contains(temp)) {
26                     list.add(temp);
27                 }       
28             }
29         }
30         return list;
31     }
32     
33     public void bfs(String beginWord, 
34                     String endWord, 
35                     HashSet<String> dict, 
36                     HashMap<String, List<String>> adj, 
37                     HashMap<String, Integer> distance) {
38         
39         for (String word : dict) {
40             adj.put(word, new ArrayList<String>());
41         }  
42         Queue<String> queue = new LinkedList<>();
43         queue.add(beginWord);
44         distance.put(beginWord, 0);
45         while (!queue.isEmpty()) {
46             String curr = queue.remove();
47             int level = distance.get(curr);
48             List<String> neighbor = getNeighbor(curr, dict);
49             for (String nei : neighbor) {
50                 adj.get(curr).add(nei);
51                 if (!distance.containsKey(nei)) {
52                     distance.put(nei, level + 1);
53                     if (!endWord.equals(nei)) {
54                         queue.add(nei);
55                     }
56                 }
57             }
58         }
59     }
60     
61     public void dfs(String curr,
62                     String end, 
63                     List<List<String>> result, 
64                     List<String> path, 
65                     HashSet<String> dict, 
66                     HashMap<String, List<String>> adj, 
67                     HashMap<String, Integer> distance) {
68         
69         if (curr.equals(end)) {
70             result.add(new ArrayList<>(path));
71             return;
72         }
73         
74         for (String nei : adj.get(curr)) {
75             path.add(nei);
76             if (distance.containsKey(nei) && distance.get(nei) == distance.get(curr) + 1) {
77                 dfs(nei, end, result, path, dict, adj, distance);
78             }
79             path.remove(path.size() - 1);
80         }
81     }   
82 }

 

posted on 2019-05-16 02:47  程序媛詹妮弗  阅读(209)  评论(0编辑  收藏  举报