-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadder.java
More file actions
38 lines (38 loc) · 1.35 KB
/
WordLadder.java
File metadata and controls
38 lines (38 loc) · 1.35 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
/*
*Given two words (start and end), and a dictionary,
*find the length of shortest transformation sequence from start to end, such that:
*Only one letter can be changed at a time
*Each intermediate word must exist in the dictionary
*/
/*类似于树的结构遍历,依次对其进行修改字符*/
public class Solution {
public int ladderLength(String start, String end, Set<String> dict) {
if(dict.size() == 0){
return 0;
}
LinkedList<String> wordQueue = new LinkedList<String>();
LinkedList<Integer> disQueue = new LinkedList<Integer>();
wordQueue.add(start);
disQueue.add(1);
while(!wordQueue.isEmpty()){
String curWord = wordQueue.pop();
int curStep = disQueue.pop();
if(curWord.equals(end)){
return curStep;
}
for(int i=0;i<curWord.length();i++){
char [] curChar = curWord.toCharArray();
for(char c='a';c<='z';c++){
curChar[i] = c;
String newWord = new String(curChar);
if(dict.contains(newWord)){
wordQueue.add(newWord);
disQueue.add(curStep+1);
dict.remove(newWord);
}
}
}
}
return 0;
}
}