-
Notifications
You must be signed in to change notification settings - Fork 64
/
Solution.java
44 lines (44 loc) · 1.35 KB
/
Solution.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
/*
Simply, this is the idea:
hit
/
hot
/ \
dot lot
/ \
dog lot
/ \
cog log
✅
*/
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
HashSet<String> set = new HashSet(wordList);
if(!set.contains(endWord)) return 0;
HashSet<String> visited = new HashSet();
Queue<String> queue = new LinkedList();
queue.add(beginWord);
visited.add(beginWord);
int trans = 1;
while(!queue.isEmpty()){
int s = queue.size();
for(int j=0;j<s;j++){
String word = queue.poll();
if(word.equals(endWord)) return trans;
for(int i=0;i<word.length();i++){
for(int k='a';k<='z';k++){
char wordArr[] = word.toCharArray();
wordArr[i] = (char) k;
String temp = new String(wordArr);
if(set.contains(temp) && !visited.contains(temp)){
queue.add(temp);
visited.add(temp);
}
}
}
}
trans++;
}
return 0;
}
}