-
Notifications
You must be signed in to change notification settings - Fork 457
/
Solution3.java
111 lines (85 loc) · 3.17 KB
/
Solution3.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
/// Source : https://leetcode.com/problems/word-ladder/description/
/// Author : liuyubobobo
/// Time : 2018-03-27
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
/// Bi-directional BFS
/// Time Complexity: O(n*n)
/// Space Complexity: O(n)
public class Solution3 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
int end = wordList.indexOf(endWord);
if(end == -1)
return 0;
if(!wordList.contains(beginWord))
wordList.add(beginWord);
int begin = wordList.indexOf(beginWord);
int n = wordList.size();
boolean[][] g = new boolean[n][n];
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < i ; j ++)
g[j][i] = g[i][j] = similar(wordList.get(i), wordList.get(j));
// bi-derectional-bfs
LinkedList<Integer> qStart = new LinkedList<>();
LinkedList<Integer> qEnd = new LinkedList<>();
int[] stepStart = new int[n];
int[] stepEnd = new int[n];
qStart.addLast(begin);
stepStart[begin] = 1;
qEnd.addLast(end);
stepEnd[end] = 1;
while(!qStart.isEmpty() && !qEnd.isEmpty()){
int curStart = qStart.removeFirst();
int curEnd = qEnd.removeFirst();
for(int i = 0 ; i < n ; i ++) {
if (stepStart[i] == 0 && g[curStart][i]) {
stepStart[i] = stepStart[curStart] + 1;
qStart.addLast(i);
}
}
for(int i = 0 ; i < n ; i ++){
if(stepEnd[i] == 0 && g[curEnd][i]){
stepEnd[i] = stepEnd[curEnd] + 1;
qEnd.addLast(i);
}
}
// check intersection
int res = Integer.MAX_VALUE;
for(int i = 0 ; i < n ; i ++)
if(stepStart[i] != 0 && stepEnd[i] != 0)
res = Integer.min(res, stepStart[i] + stepEnd[i] - 1);
if(res != Integer.MAX_VALUE)
return res;
}
return 0;
}
private boolean similar(String word1, String word2){
if(word1.length() != word2.length() || word1.equals(word2))
throw new IllegalArgumentException();
int diff = 0;
for(int i = 0 ; i < word1.length() ; i ++)
if(word1.charAt(i) != word2.charAt(i)){
diff ++;
if(diff > 1)
return false;
}
return true;
}
public static void main(String[] args) {
ArrayList<String> wordList1 = new ArrayList<String>(
Arrays.asList("hot","dot","dog","lot","log","cog"));
String beginWord1 = "hit";
String endWord1 = "cog";
System.out.println((new Solution()).ladderLength(beginWord1, endWord1, wordList1));
// 5
// ---
ArrayList<String> wordList2 = new ArrayList<String>(
Arrays.asList("a","b","c"));
String beginWord2 = "a";
String endWord2 = "c";
System.out.println((new Solution()).ladderLength(beginWord2, endWord2, wordList2));
// 2
}
}