-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadderII.java
More file actions
63 lines (60 loc) · 2.5 KB
/
WordLadderII.java
File metadata and controls
63 lines (60 loc) · 2.5 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
public class Solution {
public ArrayList<ArrayList<String>> findLadders(String start, String end, ArrayList<String> C) {
ArrayList<ArrayList<String>> sol = new ArrayList<>();
int n = start.length();
Queue<ArrayList<String>> q = new LinkedList<>();
ArrayList<String> arr = new ArrayList<>();
arr.add(start);
q.add(arr);
HashSet<String> hs = new HashSet<>();
HashSet<String> vis = new HashSet<>();
for (String str : C) {
hs.add(str);
}
hs.add(start);
hs.add(end);
vis.add(start);
int min = Integer.MAX_VALUE;
while (!q.isEmpty()) {
Queue<ArrayList<String>> aux = new LinkedList<>();
ArrayList<String> level = new ArrayList<>();
while (!q.isEmpty()) {
arr = q.poll();
String str = arr.get(arr.size() - 1);
if (str.equals(end)) {
sol.add(arr);
min = Math.min(min,arr.size());
continue;
}
for (int i = 0; i < n; i++) {
String st = str;
char[] carr = st.toCharArray();
for (char ch = 'a'; ch <= 'z'; ch++) {
if (ch != str.charAt(i)) {
carr[i] = ch;
st = String.valueOf(carr);
if (hs.contains(st) && !vis.contains(st)) {
ArrayList<String> tmp = new ArrayList<>();
for (String ss : arr) {
tmp.add(ss);
}
tmp.add(st);
aux.add(tmp);
if (!st.equals(end))
level.add(st);
}
}
}
}
}
vis.addAll(level);
q.addAll(aux);
}
ArrayList<ArrayList<String>> ans = new ArrayList<>();
for(ArrayList<String> abc: sol){
if(abc.size() == min)
ans.add(abc);
}
return ans;
}
}