-
Notifications
You must be signed in to change notification settings - Fork 78
/
solution.cpp
77 lines (70 loc) · 2.15 KB
/
solution.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
class Solution
{
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict)
{
vector<vector<string> >ans;
if(start == end)
return ans;
unordered_set<string>current , next;
unordered_set<string> flag;
unordered_map<string,vector<string> > father;
current.insert(start);
bool found = false;
while(!current.empty() && !found)
{
for(const auto &x : current)
{
flag.insert(x);
}
for(auto x : current)
{
for(int i = 0 ; i < x.size() ; ++i)
{
for(int j = 'a' ; j <= 'z' ; ++j)
{
if(x[i] == j) continue;
string tmp = x;
tmp[i] = j;
if(tmp == end) found = true;
if(dict.find(tmp) != dict.end() && flag.find(tmp) == flag.end())
{
next.insert(tmp);
father[tmp].push_back(x);
}
}
}
}
current.clear();
swap(current, next);
}
if(found)
{
vector<string> c;
dfs(ans , father , c , start , end);
}
return ans;
}
private:
void dfs(vector<vector<string> >&ans,
unordered_map<string,vector<string> >& father ,
vector<string>& c ,
string& start ,
string& now)
{
c.push_back(now);
if(now == start)
{
ans.push_back(c);
reverse(ans.back().begin() , ans.back().end());
c.pop_back();
return;
}
auto que = father.find(now) -> second;
for(auto& x : que)
{
dfs(ans , father , c , start , x);
}
c.pop_back();
}
};