forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordLadder.II.cpp
157 lines (134 loc) · 4.12 KB
/
wordLadder.II.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
// Source : https://oj.leetcode.com/problems/word-ladder-ii/
// Author : Hao Chen
// Date : 2014-10-13
/**********************************************************************************
*
* Given two words (start and end), and a dictionary, find all shortest transformation
* sequence(s) from start to end, such that:
*
* Only one letter can be changed at a time
* Each intermediate word must exist in the dictionary
*
* For example,
*
* Given:
* start = "hit"
* end = "cog"
* dict = ["hot","dot","dog","lot","log"]
*
* Return
*
* [
* ["hit","hot","dot","dog","cog"],
* ["hit","hot","lot","log","cog"]
* ]
*
* Note:
*
* All words have the same length.
* All words contain only lowercase alphabetic characters.
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <unordered_set>
using namespace std;
map< string, unordered_set<string> >&
buildTree(string& start, string& end, unordered_set<string> &dict) {
static map< string, unordered_set<string> > parents;
parents.clear();
unordered_set<string> level[3];
unordered_set<string> *previousLevel = &level[0];
unordered_set<string> *currentLevel = &level[1];
unordered_set<string> *newLevel = &level[2];
unordered_set<string> *p =NULL;
currentLevel->insert(start);
bool reachEnd = false;
while( !reachEnd ) {
newLevel->clear();
for(auto it=currentLevel->begin(); it!=currentLevel->end(); it++) {
for (int i=0; i<it->size(); i++) {
string newWord = *it;
for(char c='a'; c<='z'; c++){
newWord[i] = c;
if (newWord == end){
reachEnd = true;
parents[*it].insert(end);
continue;
}
if ( dict.count(newWord)==0 || currentLevel->count(newWord)>0 || previousLevel->count(newWord)>0 ) {
continue;
}
newLevel->insert(newWord);
//parents[newWord].insert(*it);
parents[*it].insert(newWord);
}
}
}
if (newLevel->empty()) break;
p = previousLevel;
previousLevel = currentLevel;
currentLevel = newLevel;
newLevel = p;
}
if ( !reachEnd ) {
parents.clear();
}
return parents;
}
void generatePath( string start, string end,
map< string, unordered_set<string> > &parents,
vector<string> path,
vector< vector<string> > &paths) {
if (parents.find(start) == parents.end()){
if (start == end){
paths.push_back(path);
}
return;
}
for(auto it=parents[start].begin(); it!=parents[start].end(); it++){
path.push_back(*it);
generatePath(*it, end, parents, path, paths);
path.pop_back();
}
}
vector< vector<string> >
findLadders(string start, string end, unordered_set<string> &dict) {
vector< vector<string> > ladders;
vector<string> ladder;
ladder.push_back(start);
if (start == end){
ladder.push_back(end);
ladders.push_back(ladder);
return ladders;
}
map< string, unordered_set<string> >& parents = buildTree(start, end, dict);
if (parents.size()<=0) {
return ladders;
}
generatePath(start, end, parents, ladder, ladders);
return ladders;
}
void printLadders(vector< vector<string> > &ladders){
int i, j;
for (i=0; i<ladders.size(); i++){
for (j=0; j<ladders[i].size()-1; j++){
cout << ladders[i][j] << " -> ";
}
cout << ladders[i][j] << endl;
}
}
int main(int argc, char** argv)
{
string start = "hit";
string end = "cog";
//unordered_set<string> dict ({"hot","dot","dog","lot","log"});
unordered_set<string> dict ({"bot","cig", "cog", "dit", "dut", "hot", "hit" ,"dot","dog","lot","log"});
vector< vector<string> > ladders;
ladders = findLadders(start, end, dict);
printLadders(ladders);
return 0;
}