-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path127.单词接龙.java
126 lines (120 loc) · 3.61 KB
/
127.单词接龙.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
124
125
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
* @lc app=leetcode.cn id=127 lang=java
*
* [127] 单词接龙
*/
// @lc code=start
class Solution {
int n, len;
List<List<Integer>> graph = new ArrayList<>();
boolean[] vis;
List<List<Integer>> pre = new ArrayList<>();
Set<Integer> cur = new HashSet<>();
Set<Integer> next = new HashSet<>();
int begin, end;
static class QueueNode {
int node = -1;
List<String> path = new ArrayList<>();
QueueNode(int node, List<String> path){
this.node = node;
this.path = path;
}
}
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if(wordList.indexOf(beginWord) < 0){
wordList.add(beginWord);
}
if(wordList.indexOf(endWord) < 0){
return 0;
}
begin = wordList.indexOf(beginWord);
end = wordList.indexOf(endWord);
n = wordList.size();
len = beginWord.length();
for(int i = 0;i < n;++i){
graph.add(new ArrayList<>());
pre.add(new ArrayList<>());
}
vis = new boolean[n];
for(int i = 0;i < n;++i){
for(int j = i + 1;j< n;++j){
if(connected(wordList.get(i), wordList.get(j))){
graph.get(i).add(j);
graph.get(j).add(i);
}
}
}
if(graph.get(begin).isEmpty()){
return 0;
}
cur.add(begin);
vis[begin] = true;
bfs();
if(pre.get(end).isEmpty()){
return 0;
}
List<List<String>> curPath = new ArrayList<>();
List<List<String>> nextPath = new ArrayList<>();
Set<Integer> curPre = new HashSet<>();
Set<Integer> nextPre = new HashSet<>();
curPath.add(new ArrayList<>());
curPre.add(end);
while(!curPre.isEmpty()){
nextPath.clear();
nextPre.clear();
for(int preNode: curPre){
for(List<String> path: curPath){
String preWord = wordList.get(preNode);
if(!path.isEmpty() && !connected(preWord, path.get(0))) {
continue;
}
List<String> newPath = new ArrayList<>();
newPath.add(wordList.get(preNode));
newPath.addAll(path);
nextPath.add(newPath);
}
nextPre.addAll(pre.get(preNode));
}
Set<Integer> temp = curPre;
curPre = nextPre;
nextPre = temp;
List<List<String>> tempPath = curPath;
curPath = nextPath;
nextPath = tempPath;
}
return curPath.get(0).size();
}
private void bfs(){
while(!cur.isEmpty()){
next.clear();
for(int node: cur){
for(int nextNode: graph.get(node)){
if(!vis[nextNode]){
pre.get(nextNode).add(node);
next.add(nextNode);
}
}
}
for(Integer node: next){
vis[node] = true;
}
Set<Integer> temp = cur;
cur = next;
next = temp;
}
}
private boolean connected(String a, String b){
int difCnt = 0;
for(int i = 0;i < len;++i){
if(a.charAt(i) != b.charAt(i)){
++difCnt;
}
}
return difCnt == 1;
}
}
// @lc code=end