-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathWordLadderII.java
117 lines (96 loc) · 3.3 KB
/
WordLadderII.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
package leetcode;
import java.util.*;
/**
* Created by nikoo28 on 7/19/19 2:44 AM
*/
class WordLadderII {
private List<List<String>> findLadders(String start, String end, List<String> wordList) {
HashSet<String> dict = new HashSet<>(wordList);
List<List<String>> res = new ArrayList<>();
HashMap<String, ArrayList<String>> nodeNeighbors = new HashMap<>();// Neighbors for every node
HashMap<String, Integer> distance = new HashMap<>();// Distance of every node from the start node
ArrayList<String> solution = new ArrayList<>();
dict.add(start);
bfs(start, end, dict, nodeNeighbors, distance);
dfs(start, end, nodeNeighbors, distance, solution, res);
return res;
}
// BFS: Trace every node's distance from the start node (level by level).
private void bfs(String start, String end, Set<String> dict, HashMap<String, ArrayList<String>> nodeNeighbors, HashMap<String, Integer> distance) {
for (String str : dict)
nodeNeighbors.put(str, new ArrayList<>());
Queue<String> queue = new LinkedList<>();
queue.offer(start);
distance.put(start, 0);
while (!queue.isEmpty()) {
int count = queue.size();
boolean foundEnd = false;
for (int i = 0; i < count; i++) {
String cur = queue.poll();
int curDistance = distance.get(cur);
ArrayList<String> neighbors = getNeighbors(cur, dict);
for (String neighbor : neighbors) {
nodeNeighbors.get(cur).add(neighbor);
// Check if visited
if (!distance.containsKey(neighbor)) {
distance.put(neighbor, curDistance + 1);
if (end.equals(neighbor))
// Found the shortest path
foundEnd = true;
else
queue.offer(neighbor);
}
}
}
if (foundEnd)
break;
}
}
// Find all next level nodes.
private ArrayList<String> getNeighbors(String node, Set<String> dict) {
ArrayList<String> res = new ArrayList<>();
char[] chs = node.toCharArray();
for (char ch = 'a'; ch <= 'z'; ch++) {
for (int i = 0; i < chs.length; i++) {
if (chs[i] == ch) continue;
char old_ch = chs[i];
chs[i] = ch;
if (dict.contains(String.valueOf(chs))) {
res.add(String.valueOf(chs));
}
chs[i] = old_ch;
}
}
return res;
}
// DFS: output all paths with the shortest distance.
private void dfs(String cur, String end,
HashMap<String, ArrayList<String>> nodeNeighbors,
HashMap<String, Integer> distance, ArrayList<String> solution,
List<List<String>> res) {
solution.add(cur);
if (end.equals(cur)) {
res.add(new ArrayList<>(solution));
} else {
for (String next : nodeNeighbors.get(cur)) {
if (distance.get(next) == distance.get(cur) + 1) {
dfs(next, end, nodeNeighbors, distance, solution, res);
}
}
}
solution.remove(solution.size() - 1);
}
public static void main(String[] args) {
List<String> dict = new ArrayList<>();
dict.add("ted");
dict.add("tex");
dict.add("red");
dict.add("tax");
dict.add("tad");
dict.add("den");
dict.add("rex");
dict.add("pee");
WordLadderII x = new WordLadderII();
x.findLadders("red", "tax", dict);
}
}