-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Ladder_II.java
More file actions
81 lines (61 loc) · 2.72 KB
/
Word_Ladder_II.java
File metadata and controls
81 lines (61 loc) · 2.72 KB
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
public class Solution {
public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
ArrayList<String> temp = new ArrayList<String>();
if(start == null || end == null || dict == null) return res;
if(start.equals(end)){
temp.add(start);
temp.add(end);
res.add(temp);
}
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
map.put(end, new ArrayList<String>());
map.put(start, new ArrayList<String>());
for(String str : dict) map.put(str, new ArrayList<String>());
HashSet<String> set = new HashSet<String>();
LinkedList<String> queue = new LinkedList<String>();
ArrayList<String> cur = new ArrayList<String>();
queue.offer(end);
while(!queue.isEmpty()){
int size = queue.size();
cur.clear();
set.clear();
for(int i = 0;i < size;i++){
String top = queue.poll();
dict.remove(top);
cur.add(top);
}
for(String str : cur)
for(int i = 0;i < str.length();i++)
for(char c = 'a';c <= 'z';c++){
char[] word = str.toCharArray();
word[i] = c;
String tmp = new String(word);
if(!tmp.equals(str) && dict.contains(tmp)){
if(!set.contains(tmp)) queue.offer(tmp);
map.get(str).add(tmp);
set.add(tmp);
}
}
if(set.contains(start)) break;
}
temp.add(end);
buildPath(end,start,map,res,temp);
return res;
}
private void buildPath(String end,String start,HashMap<String,ArrayList<String>> map, ArrayList<ArrayList<String>> res,
ArrayList<String> temp){
if(end.equals(start)){
ArrayList<String> record = new ArrayList<String>(temp);
Collections.reverse(record);
res.add(record);
return;
}
ArrayList<String> pre = map.get(end);
for(String str : pre){
temp.add(str);
buildPath(str,start,map,res,temp);
temp.remove(temp.size() - 1);
}
}
}