-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0127.单词接龙.java
123 lines (113 loc) · 3.45 KB
/
0127.单词接龙.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
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
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/*
* @lc app=leetcode.cn id=127 lang=java
*
* [127] 单词接龙
*
* https://leetcode.cn/problems/word-ladder/description/
*
* algorithms
* Hard (47.67%)
* Likes: 1067
* Dislikes: 0
* Total Accepted: 156.1K
* Total Submissions: 327K
* Testcase Example: '"hit"\n"cog"\n["hot","dot","dog","lot","log","cog"]'
*
* 字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 ->
* s2 -> ... -> sk:
*
*
* 每一对相邻的单词只差一个字母。
* 对于 1 <= i <= k 时,每个 si 都在 wordList 中。注意, beginWord 不需要在 wordList 中。
* sk == endWord
*
*
* 给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列
* 中的 单词数目 。如果不存在这样的转换序列,返回 0 。
*
*
* 示例 1:
*
*
* 输入:beginWord = "hit", endWord = "cog", wordList =
* ["hot","dot","dog","lot","log","cog"]
* 输出:5
* 解释:一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。
*
*
* 示例 2:
*
*
* 输入:beginWord = "hit", endWord = "cog", wordList =
* ["hot","dot","dog","lot","log"]
* 输出:0
* 解释:endWord "cog" 不在字典中,所以无法进行转换。
*
*
*
* 提示:
*
*
* 1 <= beginWord.length <= 10
* endWord.length == beginWord.length
* 1 <= wordList.length <= 5000
* wordList[i].length == beginWord.length
* beginWord、endWord 和 wordList[i] 由小写英文字母组成
* beginWord != endWord
* wordList 中的所有字符串 互不相同
*
*
*/
// @lc code=start
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set = new HashSet<>(wordList);
if (set.size() == 0 || !set.contains(endWord))
return 0;
set.remove(beginWord);
Queue<String> queue = new LinkedList<>();
Set<String> visited = new HashSet<>();
queue.offer(beginWord);
visited.add(beginWord);
int step = 1;
while (!queue.isEmpty()) {
int count = queue.size();
for (int i = 0; i < count; i++) {
String currentWord = queue.poll();
if (changeWordEveryOneLetter(currentWord, endWord, queue, set, visited))
return ++step;
}
step++;
}
return 0;
}
private boolean changeWordEveryOneLetter(String currentWord, String endWord, Queue<String> queue, Set<String> words,
Set<String> visited) {
char[] cs = currentWord.toCharArray();
for (int i = 0; i < endWord.length(); i++) {
char c = cs[i];
for (char j = 'a'; j <= 'z'; j++) {
if (c == j)
continue;
cs[i] = j;
String nextWord = String.valueOf(cs);
if (words.contains(nextWord)) {
if (endWord.equals(nextWord))
return true;
if (!visited.contains(nextWord)) {
queue.add(nextWord);
visited.add(nextWord);
}
}
}
cs[i] = c;
}
return false;
}
}
// @lc code=end