1
class Solution {
2
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
3
int count = 1;
4
Set<String> words = new HashSet<>(wordList);
5
Queue<String> q = new LinkedList<String>();
6
q.add(beginWord);
7

8
while (!q.isEmpty()) {
9
int size = q.size();
10

11
while (size-- > 0) {
12
String word = q.poll();
13
char[] chList = word.toCharArray();
14

15
for (int i = 0; i < word.length(); i++) {
16
char tmp = chList[i];
17

18
for (char c = 'a'; c <= 'z'; c++) {
19
chList[i] = c;
20
String newWord = new String(chList);
21

22
if (words.contains(newWord)) {
23
if (newWord.equals(endWord)) {
24
return count + 1;
25
}
26
q.add(newWord);
27
words.remove(newWord);
28
}
29
}
30

31
chList[i] = tmp;
32
}
33
}
34

35
count++;
36
}
37

38
return 0;
39
}
40
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0