-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path127. Word Ladder.cpp
250 lines (208 loc) · 8.07 KB
/
127. Word Ladder.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//Graph, BFS
//Runtime: 652 ms, faster than 27.76% of C++ online submissions for Word Ladder.
//Memory Usage: 14.3 MB, less than 47.16% of C++ online submissions for Word Ladder.
class Solution {
public:
bool connected(string& s1, string& s2){
int m = s1.size();
int n = s2.size();
if(m != n) return -1;
int count = 0;
for(int i = 0; i < m; ++i){
if(s1[i] != s2[i]){
++count;
if(count > 1) return false;
}
}
return count == 1;
};
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
auto it = find(wordList.begin(), wordList.end(), beginWord);
int start;
if(it == wordList.end()){
wordList.push_back(beginWord);
start = wordList.size()-1;
}else{
start = it - wordList.begin();
}
it = find(wordList.begin(), wordList.end(), endWord);
if(it == wordList.end()) return 0;
int end = it - wordList.begin();
// cout << "start: " << start << ", end: " << end << endl;
int n = wordList.size();
vector<vector<int>> adjList(n);
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
if(connected(wordList[i], wordList[j])){
adjList[i].push_back(j);
adjList[j].push_back(i);
// cout << wordList[i] << " <-> " << wordList[j] << endl;
}
}
}
//BFS
queue<int> q;
int cur;
int level = 1; //the length of the path containing only start
vector<bool> visited(n, false);
q.push(start);
visited[start] = true;
while(!q.empty()){
int levelSize = q.size();
// cout << "level: " << level << ", levelSize: " << levelSize << endl;
while(levelSize-- > 0){
cur = q.front(); q.pop();
// cout << wordList[cur] << endl;
if(cur == end){
return level;
}
for(int nei : adjList[cur]){
if(!visited[nei]){
visited[nei] = true;
q.push(nei);
}
}
}
++level;
}
//cannot find a path
return 0;
}
};
//Approach 1: Breadth First Search
//Runtime: 156 ms, faster than 71.95% of C++ online submissions for Word Ladder.
//Memory Usage: 23.9 MB, less than 21.59% of C++ online submissions for Word Ladder.
//time: O(M^2*N), space: O(M^2*N)
//M is the length of each word and N is the total number of words in the input word list
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
auto it = find(wordList.begin(), wordList.end(), beginWord);
int start;
if(it == wordList.end()){
wordList.push_back(beginWord);
start = wordList.size()-1;
}else{
start = it - wordList.begin();
}
it = find(wordList.begin(), wordList.end(), endWord);
if(it == wordList.end()) return 0;
int end = it - wordList.begin();
int m = beginWord.size();
int n = wordList.size();
//(generic word, id in wordList)
unordered_map<string, vector<int>> allComboDict;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
//replace ith char with '*'
string newword = wordList[i].substr(0, j) + '*' + wordList[i].substr(j+1);
allComboDict[newword].push_back(i);
}
}
//BFS
queue<int> q;
int cur;
int level = 1; //the length of the path containing only start
vector<bool> visited(n, false);
q.push(start);
visited[start] = true;
while(!q.empty()){
int levelSize = q.size();
// cout << "level: " << level << ", levelSize: " << levelSize << endl;
while(levelSize-- > 0){
cur = q.front(); q.pop();
// cout << wordList[cur] << endl;
if(cur == end){
return level;
}
//iterate all char and build its generic word
for(int i = 0; i < m; ++i){
string newword = wordList[cur].substr(0, i) + '*' + wordList[cur].substr(i+1);
//use this generic word to find its neighbor
for(int nei : allComboDict[newword]){
if(!visited[nei]){
visited[nei] = true;
q.push(nei);
}
}
}
}
++level;
}
//cannot find a path
return 0;
}
};
//Approach 2: Bidirectional Breadth First Search
//Runtime: 120 ms, faster than 84.54% of C++ online submissions for Word Ladder.
//Memory Usage: 24.2 MB, less than 21.02% of C++ online submissions for Word Ladder.
//time: O(M^2*N), space: O(M^2*N)
class Solution {
public:
int m, n;
//(generic word, id in wordList)
unordered_map<string, vector<int>> allComboDict;
int visitWordNode(vector<string>& wordList,
queue<int>& q, vector<int>& visited, vector<int>& visited_other){
int cur = q.front(); q.pop();
// cout << wordList[cur] << " : " << visited[cur] << endl;
for(int i = 0; i < m; ++i){
string newword = wordList[cur].substr(0, i) + '*' + wordList[cur].substr(i+1);
for(int& nei : allComboDict[newword]){
if(visited_other[nei] > 0){
//use visited[cur] to retrieve cur's level
// cout << wordList[nei] << " : " << visited_other[nei] << endl;
return visited[cur] + visited_other[nei];
}
if(visited[nei] < 0){
visited[nei] = visited[cur]+1;
q.push(nei);
}
}
}
return -1;
};
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
auto it = find(wordList.begin(), wordList.end(), beginWord);
int start;
if(it == wordList.end()){
wordList.push_back(beginWord);
start = wordList.size()-1;
}else{
start = it - wordList.begin();
}
it = find(wordList.begin(), wordList.end(), endWord);
if(it == wordList.end()) return 0;
int end = it - wordList.begin();
m = beginWord.size();
n = wordList.size();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
//replace ith char with '*'
string newword = wordList[i].substr(0, j) + '*' + wordList[i].substr(j+1);
allComboDict[newword].push_back(i);
}
}
queue<int> q_begin, q_end;
q_begin.push(start);
q_end.push(end);
//-1 for not visited, >= 0 for its level
vector<int> visited_begin(n, -1), visited_end(n, -1);
/*
we want to find the length of the sequence,
so both "start" and "end" take one place
*/
visited_begin[start] = 1;
visited_end[end] = 1;
int ans;
while(!q_begin.empty() && !q_end.empty()){
// cout << "forward: " << endl;
ans = visitWordNode(wordList, q_begin, visited_begin, visited_end);
if(ans > -1) return ans;
// cout << "backward: " << endl;
ans = visitWordNode(wordList, q_end, visited_end, visited_begin);
if(ans > -1) return ans;
}
return 0;
}
};