forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
141 lines (111 loc) · 3.77 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/// Source : https://leetcode.com/problems/word-ladder-ii/description/
/// Author : liuyubobobo
/// Time : 2018-04-23
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
#include <unordered_map>
using namespace std;
/// BFS
/// Time Complexity: O(n*n)
/// Space Complexity: O(n)
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
int end = find(wordList.begin(), wordList.end(), endWord) - wordList.begin();
if(end == wordList.size())
return {};
int begin = find(wordList.begin(), wordList.end(), beginWord) - wordList.begin();
if(begin == wordList.size())
wordList.push_back(beginWord);
int n = wordList.size();
// Create Graph
vector<vector<int>> g(n, vector<int>());
for(int i = 0 ; i < wordList.size() ; i ++)
for(int j = i + 1 ; j < wordList.size() ; j ++)
if(similar(wordList[i], wordList[j])){
g[i].push_back(j);
g[j].push_back(i);
}
unordered_map<int, int> distance;
bfs(g, begin, end, distance);
vector<vector<string>> res;
vector<int> tres = {begin};
getRes(g, begin, end, distance, wordList, tres, res);
return res;
}
private:
void bfs(const vector<vector<int>>& g, int begin, int end,
unordered_map<int, int>& distance){
queue<int> q;
q.push(begin);
distance[begin] = 0;
while(!q.empty()){
int cur = q.front();
q.pop();
// assert(distance.find(cur) != distance.end());
for(int j: g[cur])
if(distance.find(j) == distance.end()){
distance[j] = distance[cur] + 1;
q.push(j);
}
}
}
void getRes(vector<vector<int>>& g, int cur, int end,
unordered_map<int, int>& distance,
const vector<string>& wordList,
vector<int>& tres, vector<vector<string>>& res){
if(tres.size() > 0 && tres[tres.size() - 1] == end){
res.push_back(getPath(tres, wordList));
return;
}
for(int i: g[cur])
if(distance[i] == distance[cur] + 1){
tres.push_back(i);
getRes(g, i, end, distance, wordList, tres, res);
tres.pop_back();
}
return;
}
vector<string> getPath(const vector<int>& path,
const vector<string>& wordList){
vector<string> ret;
for(const int e: path)
ret.push_back(wordList[e]);
return ret;
}
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;
}
};
void print_vector_vector(const vector<vector<string>>& res){
for(const vector<string>& v: res){
for(const string& e: v)
cout << e << " ";
cout << endl;
}
cout << endl;
}
int main() {
vector<string> vec1 = {"hot","dot","dog","lot","log","cog"};
string beginWord1 = "hit";
string endWord1 = "cog";
vector<vector<string>> res1 = Solution().findLadders(beginWord1, endWord1, vec1);
print_vector_vector(res1);
// ---
vector<string> vec2 = {"a","b","c"};
string beginWord2 = "a";
string endWord2 = "c";
vector<vector<string>> res2 = Solution().findLadders(beginWord2, endWord2, vec2);
print_vector_vector(res2);
return 0;
}