-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path126.word-ladder-ii.java
131 lines (120 loc) · 3.9 KB
/
126.word-ladder-ii.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* [126] Word Ladder II
*
* https://leetcode.com/problems/word-ladder-ii/description/
*
* algorithms
* Hard (14.68%)
* Total Accepted: 78K
* Total Submissions: 531.5K
* Testcase Example: '"hit"\n"cog"\n["hot","dot","dog","lot","log","cog"]'
*
*
* 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 transformed word must exist in the word list. Note that beginWord is
* not a transformed word.
*
*
*
* For example,
*
*
* Given:
* beginWord = "hit"
* endWord = "cog"
* wordList = ["hot","dot","dog","lot","log","cog"]
*
*
* Return
*
* [
* ["hit","hot","dot","dog","cog"],
* ["hit","hot","lot","log","cog"]
* ]
*
*
*
*
* 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.
*
*
*
*
* UPDATE (2017/1/20):
* The wordList parameter had been changed to a list of strings (instead of a
* set of strings). Please reload the code definition to get the latest
* changes.
*
*/
class Solution {
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Set<String> words = new HashSet<>();
for (String word : wordList) {
words.add(word);
}
List<String> toVisit = new ArrayList<>();
Set<String> visited = new HashSet<>();
toVisit.add(beginWord);
visited.add(beginWord);
Map<String, List<String>> path = new HashMap<>();
path.put(beginWord, null);
while (!toVisit.isEmpty()) {
List<String> children = new ArrayList<>();
for (String word : toVisit) {
if (word.equals(endWord)) {
return pathes(endWord, path);
}
enqueueNeighbors(children, word, words, visited, path);
}
toVisit = children;
}
return new ArrayList<List<String>>();
}
private void enqueueNeighbors(List<String> toVisit, String word, Set<String> words, Set<String> visited, Map<String, List<String>> path) {
for (int i = 0; i < word.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
String newWord = word.substring(0, i) + c + word.substring(i+1);
if (words.contains(newWord)) {
if (!visited.contains(newWord)) {
visited.add(newWord);
toVisit.add(newWord);
}
if (toVisit.contains(newWord)) {
if (!path.containsKey(newWord)) {
path.put(newWord, new ArrayList<String>());
}
path.get(newWord).add(word);
}
}
}
}
}
private List<List<String>> pathes(String endWord, Map<String, List<String>> path) {
List<List<String>> result = new ArrayList<>();
List<String> parents = path.get(endWord);
if (parents == null) {
List<String> list = new ArrayList<>();
list.add(endWord);
result.add(list);
return result;
}
for (String parent : parents) {
for (List<String> p : pathes(parent, path)) {
p.add(endWord);
result.add(p);
}
}
return result;
}
}