-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWordLadder.java
More file actions
368 lines (343 loc) · 12.4 KB
/
Copy pathWordLadder.java
File metadata and controls
368 lines (343 loc) · 12.4 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* <p>字典 <code>wordList</code> 中从单词 <code>beginWord</code><em> </em>和 <code>endWord</code> 的 <strong>转换序列 </strong>是一个按下述规格形成的序列:</p>
*
* <ul>
* <li>序列中第一个单词是 <code>beginWord</code> 。</li>
* <li>序列中最后一个单词是 <code>endWord</code> 。</li>
* <li>每次转换只能改变一个字母。</li>
* <li>转换过程中的中间单词必须是字典 <code>wordList</code> 中的单词。</li>
* </ul>
*
* <p>给你两个单词<em> </em><code>beginWord</code><em> </em>和 <code>endWord</code> 和一个字典 <code>wordList</code> ,找到从 <code>beginWord</code> 到 <code>endWord</code> 的 <strong>最短转换序列</strong> 中的 <strong>单词数目</strong> 。如果不存在这样的转换序列,返回 0。</p>
*
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
* <strong>输出:</strong>5
* <strong>解释:</strong>一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
* <strong>输出:</strong>0
* <strong>解释:</strong>endWord "cog" 不在字典中,所以无法进行转换。</pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= beginWord.length <= 10</code></li>
* <li><code>endWord.length == beginWord.length</code></li>
* <li><code>1 <= wordList.length <= 5000</code></li>
* <li><code>wordList[i].length == beginWord.length</code></li>
* <li><code>beginWord</code>、<code>endWord</code> 和 <code>wordList[i]</code> 由小写英文字母组成</li>
* <li><code>beginWord != endWord</code></li>
* <li><code>wordList</code> 中的所有字符串 <strong>互不相同</strong></li>
* </ul>
* <div><div>Related Topics</div><div><li>广度优先搜索</li><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 910</li><li>👎 0</li></div>
*/
package leetcode7;
import java.util.*;
public class WordLadder {
public static void main(String[] args) {
new WordLadder().new Solution().ladderLength("hit", "cog", new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog")));
}
/**
* BFS,Set实现BeginSet,迭代wordList,每个word去BeginSet中比较
* 注:题目要求是最短转换序列的单词数目,不是转换次数
* 最优解法
*/
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if (!wordList.contains(endWord)) {
return 0;
}
int res = 1;
Set<String> beginSet = new HashSet<>();
beginSet.add(beginWord);
while (!beginSet.isEmpty()) {
if (beginSet.contains(endWord)) {
return res;
}
beginSet = solve(beginSet, wordList);
res++;
}
return 0;
}
private Set<String> solve(Set<String> beginSet, List<String> wordList) {
Set<String> set = new HashSet<>();
Iterator<String> iterator = wordList.iterator();
while (iterator.hasNext()) {
String word = iterator.next();
for (String beginWord : beginSet) {
if (calc(word, beginWord)) {
set.add(word);
iterator.remove();
break;
}
}
}
return set;
}
private boolean calc(String word1, String word2) {
if (word1.length() != word2.length()) {
return false;
}
int res = 0;
for (int i = 0; i < word1.length(); i++) {
if (word1.charAt(i) != word2.charAt(i) && ++res > 1) {
return false;
}
}
return res == 1;
}
}
/**
* BFS,队列实现BeginList,每次poll一个BeginWord判断WordList
*/
class Solution1 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Deque<String> deque = new ArrayDeque<>();
deque.offer(beginWord);
int level = 1;
while (!deque.isEmpty()) {
if (deque.contains(endWord)) {
return level;
}
int size = deque.size();
for (int i = 0; i < size; i++) {
String s = deque.poll();
Iterator<String> iterator = wordList.iterator();
while (iterator.hasNext()) {
String t = iterator.next();
if (calc(s, t)) {
iterator.remove();
deque.offer(t);
}
}
}
level++;
}
return 0;
}
private boolean calc(String s, String t) {
int val = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
val++;
}
}
return val == 1;
}
}
/**
* 单向 BFS,数组实现
* 注:双向 BFS 太复杂,暂不研究
*/
class Solution2 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
List<String> begin = new ArrayList<>();
begin.add(beginWord);
int level = 1;
while (!begin.isEmpty()) {
if (begin.contains(endWord)) {
return level;
}
level++;
begin = solve(begin, wordList);
}
return 0;
}
private List<String> solve(List<String> begin, List<String> wordList) {
List<String> res = new ArrayList<>();
for (String b : begin) {
Iterator<String> iterator = wordList.iterator();
while (iterator.hasNext()) {
String word = iterator.next();
if (calc(b, word)) {
res.add(word);
iterator.remove();
}
}
}
return res;
}
private boolean calc(String aStr, String bStr) {
char[] a = aStr.toCharArray();
char[] b = bStr.toCharArray();
if (a.length != b.length) {
return false;
}
int cnt = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
cnt++;
if (cnt > 1) {
return false;
}
}
}
return cnt == 1;
}
}
/**
* BFS,队列实现
*/
class Solution3 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Deque<String> deque = new ArrayDeque<>();
deque.offer(beginWord);
int level = 1;
while (!deque.isEmpty()) {
if (deque.contains(endWord)) {
return level;
}
int size = deque.size();
for (int i = 0; i < size; i++) {
String s = deque.poll();
Iterator<String> iterator = wordList.iterator();
while (iterator.hasNext()) {
String t = iterator.next();
if (calc(s, t)) {
iterator.remove();
deque.offer(t);
}
}
}
level++;
}
return 0;
}
private boolean calc(String s, String t) {
int val = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
val++;
}
}
return val == 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
/**
* BFS,数组实现
*/
class Solution4 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
List<String> target = new ArrayList<>();
target.add(beginWord);
int level = 1;
while (!target.isEmpty()) {
if (target.contains(endWord)) {
return level;
}
target = calc(wordList, target);
level++;
}
return 0;
}
private List<String> calc(List<String> wordList, List<String> target) {
List<String> res = new ArrayList<>();
Iterator<String> iterator = wordList.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
for (int i = 0; i < target.size(); i++) {
String t = target.get(i);
if (solve(s, t)) {
res.add(s);
iterator.remove();
break;
}
}
}
return res;
}
private boolean solve(String o1, String o2) {
int val = 0;
for (int i = 0; i < o1.length(); i++) {
if (o1.charAt(i) != o2.charAt(i)) {
val++;
}
}
return val == 1;
}
}
/**
* BFS,效率适中
* 双向BFS解法待补充
*/
class Solution5 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
boolean[] visited = new boolean[wordList.size()];
Deque<String> deque = new ArrayDeque<>();
deque.offer(beginWord);
int level = 1;
while (!deque.isEmpty()) {
int size = deque.size();
for (int i = 0; i < size; i++) {
String str = deque.poll();
if (endWord.equals(str)) {
return level;
}
for (int j = 0; j < wordList.size(); j++) {
String word = wordList.get(j);
if (visited[j]) {
continue;
}
int diff = 0;
for (int k = 0; k < word.length(); k++) {
if (word.charAt(k) != str.charAt(k)) {
diff++;
}
}
if (diff == 1) {
visited[j] = true;
deque.offer(word);
}
}
}
level++;
}
return 0;
}
}
/**
* DFS + 回溯,超时,无法通过
*/
class Solution6 {
int min = Integer.MAX_VALUE;
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
boolean[] visited = new boolean[wordList.size()];
resolve(beginWord, endWord, wordList, visited, 1);
return min == Integer.MAX_VALUE ? 0 : min;
}
private void resolve(String beginWord, String endWord, List<String> wordList, boolean[] visited, int level) {
if (endWord.equals(beginWord)) {
min = Math.min(min, level);
return;
}
for (int i = 0; i < wordList.size(); i++) {
if (visited[i]) {
continue;
}
String word = wordList.get(i);
int diff = 0;
for (int j = 0; j < word.length(); j++) {
if (word.charAt(j) != beginWord.charAt(j)) {
diff++;
}
}
if (diff == 1) {
visited[i] = true;
resolve(word, endWord, wordList, visited, level + 1);
visited[i] = false;
}
}
}
}
}