/
word_ladder_2_bfs.cpp
103 lines (95 loc) · 3.03 KB
/
word_ladder_2_bfs.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
/**
* breadth first search algorithm
* time: depends on the pruning
* space: depends on the pruning, using a map to reduce the space for storing the path
* date: 26-01-2015
* author: Teddy
*/
/**
* start = "hit"
* end = "cog"
* dict = ["hot", "dot", "dog", "lot", "log"]
* res = ["hit", "hot", "dot", "dog", "cog"], ["hit", "hot", "lot", "log", "cog"]
*/
/**
* use DFS
* state of BFS: put the strings on the path in a queue, using "$" as a seperator
* start of BFS: start string and "$" seperator
* end of BFS: queue empty or reach the end string.
* an extra step: return the results.
*/
/**
* I create a hash to store the words, so the path can describe by indices which
* are intergers, this strategy will reduce the space from string to integer, but
* still memory limit exceeded. This is really a hard problem.
*/
class Solution {
private:
bool notUsed(vector<int> v, int index){
for(int i = 0; i < v.size(); i++){
if(v[i] == index){
return false;
}
}
return true;
}
void buildMap(unordered_set<string> dict, unordered_map<string, int> &pos,
vector<string> &words){
for(unordered_set<string>::iterator it = dict.begin(); it != dict.end(); it++){
words.push_back(*it);
pos[*it] = words.size() - 1;
}
}
vector<string> genStr(vector<int> index, vector<string> s, string end){
vector<string> r;
for(int i = 0; i < index.size(); i++){
r.push_back(s[index[i]]);
}
r.push_back(end);
return r;
}
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > results;
queue<vector<int> > q;
unordered_map<string, int> pos;
vector<string> words;
words.push_back(start);
buildMap(dict, pos, words);
vector<int> r;
bool reached = false;
r.push_back(0);
q.push(r);
vector<int> seperator;
seperator.push_back(-1);
q.push(seperator);
while(!q.empty()){
vector<int> cur = q.front();
q.pop();
if(cur[0] == -1){
if(reached || q.empty()){
break;
}
q.push(seperator);
continue;
}
string s = words[cur.back()];
for(int i = 0; i < s.size(); i++){
for(char c = 'a'; c <= 'z'; c++){
string tmp = s;
tmp[i] = c;
if(tmp == end){
results.push_back(genStr(cur, words, end));
reached = true;
}
else if(dict.count(tmp) > 0 && notUsed(cur, pos[tmp])){
cur.push_back(pos[tmp]);
q.push(cur);
cur.pop_back();
}
}
}
}
return results;
}
};