-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord Ladder.cpp
More file actions
177 lines (164 loc) · 4.43 KB
/
Word Ladder.cpp
File metadata and controls
177 lines (164 loc) · 4.43 KB
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
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <unordered_set>
using namespace std;
/*Approach 1 : DFS with queue*/
int ladderLength(string start, string end, unordered_set<string> &dict) {
if (dict.size() == 0) return 0;
queue<string> frontQueue;
queue<int> backQueue;
frontQueue.push(start);
backQueue.push(1);
while(!frontQueue.empty())
{
string str = frontQueue.front();
frontQueue.pop();
int len = backQueue.front();
backQueue.pop();
if (str.compare(end) == 0) return len;
for (int i = 0; i < str.length(); ++i)
{
for (char j = 'a'; j <= 'z'; ++j)
{
string temp = str;
temp[i] = j;
if (temp.compare(end) == 0) return len+1;
if (dict.find(temp) != dict.end())
{
backQueue.push(len+1);
frontQueue.push(temp);
dict.erase(dict.find(temp));
}
}
}
}
return 0;
}
vector<string> getPossibleStrings(string str, unordered_set<string> &dict)
{
int len = str.length();
vector<string> vect;
for (int i = 0; i < len; ++i)
{
string temp = str;
for (char j = 'a'; j <= 'z'; ++j)
{
string temp = str;
temp[i] = j;
if (temp.compare(str) != 0 && dict.find(temp) != dict.end())
{
vect.push_back(temp);
}
}
}
return vect;
}
int ladderLength2(string start, string end, unordered_set<string> &dict) {
if (dict.size() == 0) return 0;
if (start.compare(end) == 0) return 1;
struct node
{
string value;
int level;
node(string str, int l) {value = str; level = l;}
};
queue<node> startQueue;
queue<node> endQueue;
map<string, bool> startMap;
startMap[start] = true;
map<string, int> endMap;
endMap[end] = true;
int sLevel = 1, rLevel = 1;
node temp = node(start, 1);
startQueue.push(temp);
temp = node(end, 1);
endQueue.push(temp);
while (!startQueue.empty() && !endQueue.empty())
{
if (startQueue.size() < endQueue.size())
{
while (!startQueue.empty() && sLevel == startQueue.front().level) {
node temp = startQueue.front();
vector<string> vect = getPossibleStrings(temp.value, dict);
for (auto id = vect.begin(); id != vect.end(); ++id)
{
if (!startMap[*id])
{
startMap[*id] = true;
if (endMap.find(*id) != endMap.end()) {
return startQueue.front().level + endQueue.back().level;
}
temp = node(*id, sLevel + 1);
startQueue.push(temp);
}
}
startQueue.pop();
}
sLevel++;
}
else
{
while (!endQueue.empty() && rLevel == endQueue.front().level) {
node temp = endQueue.front();
vector<string> vect = getPossibleStrings(temp.value, dict);
for (auto id = vect.begin(); id != vect.end(); ++id)
{
if (!endMap[*id])
{
endMap[*id] = true;
if (startMap.find(*id) != startMap.end()) {
return startQueue.back().level + endQueue.front().level;
}
temp = node(*id, rLevel + 1);
endQueue.push(temp);
}
}
endQueue.pop();
}
rLevel++;
}
}
return 0;
}
int main(int argc, char *argv[])
{
unordered_set<string> dict;
dict.insert("hot");
dict.insert("dot");
dict.insert("dog");
dict.insert("lot");
dict.insert("log");
cout<<ladderLength2("hit", "cog", dict)<<endl;
dict.empty();
dict.insert("hot");
dict.insert("dog");
dict.insert("dot");
//"kiss", "tusk", ["miss","dusk","kiss","musk","tusk","diss","disk","sang","ties","muss"]
cout<<ladderLength2("hot", "dog", dict)<<endl;
dict.empty();
dict.insert("misss");
dict.insert("dusk");
dict.insert("kiss");
dict.insert("musk");
dict.insert("tusk");
dict.insert("diss");
dict.insert("disk");
dict.insert("sang");
dict.insert("ties");
dict.insert("muss");
cout<<ladderLength2("kiss", "tusk", dict)<<endl;
cout<<ladderLength("kiss", "tusk", dict)<<endl;
dict.empty();
/*dict.insert("a");
dict.insert("b");
dict.insert("c");
cout<<ladderLength2("a", "c", dict);
dict.empty();
*/
getchar();
return 0;
}