-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path126.cpp
More file actions
70 lines (64 loc) · 2.11 KB
/
126.cpp
File metadata and controls
70 lines (64 loc) · 2.11 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
class Solution {
vector<vector<string>> res;
vector<string> path;
unordered_map<string,vector<string>> graph;
public:
bool bfs(const string& beginWord,const string& endWord, unordered_set<string>& wordList){ // create a graph with bfs
// like this
// hit -> hot -> (dot,lot) -> (dog,log) -> (cog,cog)
// i.e. all the shortest path from hit to cog
unordered_set<string> current, next;
current.insert(beginWord);
while(true){
for(auto word: current)
wordList.erase(word);
for(auto parent: current){
string word = parent;
for(int i=0; i<word.size(); i++){
char ch = word[i];
for(char c='a'; c<='z'; c++){
word[i] = c;
if(wordList.count(word)){
next.insert(word);
graph[parent].push_back(word);
}
}
word[i] = ch;
}
}
if(next.empty())
return false;
if(next.count(endWord))
return true;
current.clear();
swap(current,next);
}
return false;
}
void dfs(string beginWord, string endWord){
// traverse the graph created dfs wise;
if(beginWord == endWord){
res.push_back(path);
return;
}
for(auto child: graph[beginWord]){
path.push_back(child);
dfs(child,endWord);
path.pop_back();
}
}
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> ust(wordList.begin(), wordList.end());
if(!ust.count(endWord))
return res;
if(beginWord == endWord){
res.push_back({beginWord});
return res;
}
if(!bfs(beginWord,endWord,ust))
return res;
path.push_back(beginWord);
dfs(beginWord,endWord);
return res;
}
};