forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.cpp
120 lines (92 loc) · 3.08 KB
/
main4.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
/// Source : https://leetcode.com/problems/word-ladder/description/
/// Author : liuyubobobo
/// Time : 2017-11-21
/// Updated: 2018-03-27
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
#include <unordered_map>
using namespace std;
/// Bi-directional BFS
/// No need to calculate all pairs similarity
/// Time Complexity: O(n*n)
/// Space Complexity: O(n)
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
if(find(wordList.begin(), wordList.end(), endWord) == wordList.end())
return 0;
// bi-derectional bfs
unordered_map<string, int> step_s;
unordered_map<string, int> step_t;
queue<string> queue_s;
queue<string> queue_t;
queue_s.push(beginWord);
step_s[beginWord] = 1;
queue_t.push(endWord);
step_t[endWord] = 1;
while(!queue_s.empty() && !queue_t.empty()){
string sWord = queue_s.front();
queue_s.pop();
string tWord = queue_t.front();
queue_t.pop();
for(string word: wordList){
if(step_s.find(word) == step_s.end() && similar(word, sWord)){
step_s[word] = step_s[sWord] + 1;
queue_s.push(word);
}
if(step_t.find(word) == step_t.end() && similar(word, tWord)){
step_t[word] = step_t[tWord] + 1;
queue_t.push(word);
}
}
// check intersection
int res = INT_MAX;
for(string word: wordList)
if(step_s.find(word) != step_s.end() && step_t.find(word) != step_t.end())
res = min(res, step_s[word] + step_t[word] - 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
// ---
vector<string> vec4 = {"hot","dot","dog","lot","log"};
string beginWord4 = "hit";
string endWord4 = "cog";
cout << Solution().ladderLength(beginWord4, endWord4, vec4) << endl;
// 0
return 0;
}