forked from algorithm010/algorithm010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC127_word_ladder.java
More file actions
55 lines (50 loc) · 1.96 KB
/
LC127_word_ladder.java
File metadata and controls
55 lines (50 loc) · 1.96 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
package Week07;
import java.util.*;
public class LC127_word_ladder {
//Two-ended BFS
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> wordSet = new HashSet<>(wordList);
if (wordSet.size() == 0 || !wordSet.contains(endWord)){
return 0;
}
Set<String> beginVisited = new HashSet<>();
Set<String> endVisited = new HashSet<>();
Set<String> visited = new HashSet<>();
beginVisited.add(beginWord);
endVisited.add(endWord);
int len = beginWord.length();
int step =1;
while(!beginVisited.isEmpty() && !endVisited.isEmpty()){
if (beginVisited.size()>endVisited.size()){
Set<String> temp = beginVisited;
beginVisited = endVisited;
endVisited = temp;
}
//用Set在遍历时不能删除元素,所以用temp临时保存下一个beginSet
Set<String> nextLevelVisited = new HashSet<>();
for (String word: beginVisited){
char[] chs = word.toCharArray();
for (int i=0; i<len; i++){
char originChar = chs[i];
for (char c='a'; c<='z'; c++){
if (c== originChar) continue;
chs[i] = c;
String newWord = String.valueOf(chs);
if (endVisited.contains(newWord)){
return step+1;
}
if (wordSet.contains(newWord)&&!visited.contains(newWord)){
nextLevelVisited.add(newWord);
visited.add(newWord);
}
}
//next letter, recover original char
chs[i] = originChar;
}
}
beginVisited = nextLevelVisited;
step++;
}
return 0;
}
}