-
Notifications
You must be signed in to change notification settings - Fork 28
/
Word_Ladder.cpp
151 lines (131 loc) · 4.99 KB
/
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
class Solution {
public:
bool isNextState(string lhs, string rhs) {
bool flag = false;
for(int i = 0; i < lhs.length(); ++i) {
if(lhs[i] != rhs[i]) {
if(flag) return false;
flag = true;
}
}
return true;
}
void findDict(string s, vector<string> &nexts, unordered_set<string> &dict) {
for(int i = 0; i < s.length(); ++i) {
string str = s;
for(char j = 'a'; j <= 'z'; ++j) {
str[i] = j;
if(dict.find(str) != dict.end()) {
nexts.push_back(str);
}
}
}
}
// DFS
int ladderLengthUtils(string end, string curr, int depth, unordered_set <string> &dict, unordered_map <string, bool> &visited) {
if(curr == end) return depth;
if(isNextState(curr, end)) return depth + 1;
int Min = numeric_limits<int>::max();
for(unordered_set <string>::iterator it = dict.begin(); it != dict.end(); ++it) {
if(!visited[*it]) {
if(isNextState(curr, *it)) {
visited[*it] = true;
Min = min(ladderLengthUtils(end, *it, depth + 1, dict, visited), Min);
visited[*it] = false;
}
}
}
return Min;
}
// double BFS - works faster. here both start and end states are known, so double bfs suits better
int ladderLengthDoubleBFS(string start, string end, unordered_set<string> &dict) {
if (isNextState(start, end)) {
return 2;
}
queue<pair<string, int> > Q, rQ;
unordered_map <string, bool> visited, rVisited;
int level = 1, rlevel = 1;
Q.push(make_pair(start, 1));
rQ.push(make_pair(end, 1));
while (!Q.empty() and !rQ.empty()) {
if (Q.size() < rQ.size()) {
while (!Q.empty() and Q.front().second == level) {
vector<string> nexts;
findDict(Q.front().first, nexts, dict);
for (auto it = nexts.begin(); it != nexts.end(); it++) {
if (!visited[*it]) {
visited[*it] = true;
if (rVisited[*it]) {
return Q.front().second + rQ.back().second;
}
Q.push(make_pair(*it, level + 1));
}
}
Q.pop();
}
level++;
} else {
while (!rQ.empty() and rQ.front().second == rlevel) {
vector<string> nexts;
findDict(rQ.front().first, nexts, dict);
for (auto it = nexts.begin(); it != nexts.end(); it++) {
if (!rVisited[*it]) {
rVisited[*it] = true;
if (visited[*it]) {
return rQ.front().second + Q.back().second;
}
rQ.push(make_pair(*it, rlevel + 1));
}
}
rQ.pop();
}
rlevel++;
}
}
return 0;
}
int ladderLength(string start, string end, unordered_set<string> &dict) {
// return ladderLengthDoubleBFS(start, end, dict);
if(dict.size() == 0) {
if( isNextState(start, end) )
return 2;
}
unordered_map <string, bool> visited;
/* DFS - TLE
int ret = ladderLengthUtils(end, start, 1, dict, visited);
if(ret == numeric_limits<int>::max())
return 0;
return ret;
*/
queue <pair<string, int> > Q;
Q.push(make_pair(start, 1));
visited[start] = true;
while(!Q.empty()) {
pair<string, int> pop = Q.front();
string curr = pop.first;
int depth = pop.second;
Q.pop();
if(curr == end) return depth;
if(isNextState(curr, end)) return depth + 1;
/*
for(unordered_set <string>::iterator it = dict.begin(); it != dict.end(); ++it) {
if(!visited[*it]) {
if(isNextState(curr, *it)) {
visited[*it] = true;
Q.push(make_pair(*it, depth + 1));
}
}
}
*/
vector <string> nexts;
findDict(curr, nexts, dict);
for (vector <string>::iterator it = nexts.begin(); it != nexts.end(); it++) {
if (!visited[*it]) {
visited[*it] = true;
Q.push(make_pair(*it, depth + 1));
}
}
}
return 0;
}
};