-
Notifications
You must be signed in to change notification settings - Fork 1
/
Word Ladder II.cpp
49 lines (46 loc) · 1.66 KB
/
Word Ladder 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
/*I will be back with a cleaner solution*/
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wList) {
unordered_set<string> wordList(begin(wList), end(wList));
if(!wordList.count(endWord)) return {};
vector<vector<string>> ans;
queue<vector<string>> paths;
wordList.insert(endWord);
paths.push({beginWord});
int level = 1;
int minLevel = INT_MAX;
unordered_set<string> visited;
while (!paths.empty()) {
vector<string> path = paths.front();
paths.pop();
if (path.size() > level) {
for (string w : visited) wordList.erase(w);
visited.clear();
if (path.size() > minLevel)
break;
else
level = path.size();
}
string last = path.back();
for (int i = 0; i < last.size(); ++i) {
string news = last;
for (char c = 'a'; c <= 'z'; ++c) {
news[i] = c;
if (wordList.find(news) != wordList.end()) {
vector<string> newpath = path;
newpath.push_back(news);
visited.insert(news);
if (news == endWord) {
minLevel = level;
ans.push_back(newpath);
}
else
paths.push(newpath);
}
}
}
}
return ans;
}
};