forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
122 lines (92 loc) · 3.16 KB
/
main3.cpp
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
/// Source : https://leetcode.com/problems/word-ladder/description/
/// Author : liuyubobobo
/// Time : 2017-11-21
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
/// Bi-directional BFS
/// Time Complexity: O(n*n)
/// Space Complexity: O(n)
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int end = find(wordList.begin(), wordList.end(), endWord) - wordList.begin();
if(end == wordList.size())
return 0;
int begin = find(wordList.begin(), wordList.end(), beginWord) - wordList.begin();
if(begin == wordList.size())
wordList.push_back(beginWord);
int n = wordList.size();
vector<vector<bool>> g(n, vector<bool>(n, false));
for(int i = 0 ; i < wordList.size() ; i ++)
for(int j = 0 ; j < i ; j ++)
g[i][j] = g[j][i] = similar(wordList[i], wordList[j]);
// bi-derectional-bfs
vector<int> step_s(n, 0);
vector<int> step_t(n, 0);
queue<int> queue_s;
queue<int> queue_t;
queue_s.push(begin);
step_s[begin] = 1;
queue_t.push(end);
step_t[end] = 1;
while(!queue_s.empty() && !queue_t.empty()){
int cur_s = queue_s.front();
queue_s.pop();
int cur_t = queue_t.front();
queue_t.pop();
for(int i = 0 ; i < wordList.size() ; i ++)
if(step_s[i] == 0 && g[cur_s][i]){
step_s[i] = step_s[cur_s] + 1;
queue_s.push(i);
}
for(int i = 0 ; i < wordList.size() ; i ++)
if(step_t[i] == 0 && g[cur_t][i]){
step_t[i] = step_t[cur_t] + 1;
queue_t.push(i);
}
// check intersection
int res = INT_MAX;
for(int i = 0 ; i < wordList.size() ; i ++)
if(step_s[i] != 0 && step_t[i] != 0)
res = min(res, step_s[i] + step_t[i] - 1);
if(res != INT_MAX)
return res;
}
return 0;
}
private:
bool similar(const string& word1, const string& word2){
assert(word1 != "" && word1.size() == word2.size() && word1 != word2);
int diff = 0;
for(int i = 0 ; i < word1.size() ; i ++)
if(word1[i] != word2[i]){
diff ++;
if(diff > 1)
return false;
}
return true;
}
};
int main() {
vector<string> vec1 = {"hot","dot","dog","lot","log","cog"};
string beginWord1 = "hit";
string endWord1 = "cog";
cout << Solution().ladderLength(beginWord1, endWord1, vec1) << endl;
// 5
// ---
vector<string> vec2 = {"a","b","c"};
string beginWord2 = "a";
string endWord2 = "c";
cout << Solution().ladderLength(beginWord2, endWord2, vec2) << endl;
// 2
// ---
vector<string> vec3 = {"most","fist","lost","cost","fish"};
string beginWord3 = "lost";
string endWord3 = "cost";
cout << Solution().ladderLength(beginWord3, endWord3, vec3) << endl;
// 2
return 0;
}