-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadderII.java
More file actions
114 lines (94 loc) · 3.38 KB
/
WordLadderII.java
File metadata and controls
114 lines (94 loc) · 3.38 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
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
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
public class WordLadderII {
public static void main(String[] args) {
// "red", "tax", ["ted","tex","red","tax","tad","den","rex","pee"]
// "hot", "dog", ["hot","cog","dog","tot","hog","hop","pot","dot"]
//"lost", "cost", ["most","fist","lost","cost","fish"]
String start = "lost";
String end = "cost";
HashSet<String> dict = new HashSet<String>();
// dict.add("ted");
// dict.add("tex");
// dict.add("red");
// dict.add("tax");
// dict.add("tad");
// dict.add("den");
// dict.add("rex");
// dict.add("pee");
dict.add("most");
dict.add("first");
dict.add("lost");
dict.add("cost");
dict.add("fish");
//dict.add("hop");
System.out.println(findLadders(start, end, dict));
}
public static List<List<String>> findLadders(String start, String end,
Set<String> dict) {
List<List<String>> ret = new LinkedList<List<String>> ();
HashMap<String, List<String>> preMap = new HashMap<String, List<String>> ();
Queue<String> ql = new LinkedList<String>();
ql.offer(start);
if(dict.contains(start)) dict.remove(start);
int presize = 0;
int cursize = 1;
while(ql.size() !=0) {
int tempsize = presize;
presize = cursize;
cursize = tempsize;
List<String> usedString = new LinkedList<String>();
while(presize>0){
String temp = ql.poll();
if(temp.equals(end)) break;
for(int i=0; i<temp.length(); i++) {
char[] tempChars = temp.toCharArray();
char flag = tempChars[i];
for(char c = 'a';c<='z'; c++) {
if(c==flag) continue;
tempChars[i] = c;
String newtemp = new String(tempChars);
if(dict.contains(newtemp)) {
usedString.add(newtemp);
if(preMap.containsKey(newtemp)) {
preMap.get(newtemp).add(temp);
} else {
List<String> tempListString = new LinkedList<String>();
tempListString.add(temp);
preMap.put(newtemp, tempListString);
}
ql.offer(newtemp);
cursize++;
}
}
}
presize--;
}
for(String string:usedString) {
dict.remove(string);
}
}
List<String> path = new LinkedList<String>();
generatePath(ret, preMap, start,end, path);
return ret;
}
private static void generatePath(List<List<String>> ret,
HashMap<String, List<String>> preMap, String start, String word,
List<String> path) {
path.add(0, word);
if (word.equals(start)) {
ret.add(path);
return;
}
if (preMap.containsKey(word)) {
for (String string : preMap.get(word)) {
List<String> newPath = new LinkedList<String>(path);
generatePath(ret, preMap, start, string, newPath);
}
}
}
}