-
Notifications
You must be signed in to change notification settings - Fork 0
/
126-Word Ladder II (BFS).cpp
66 lines (66 loc) · 1.98 KB
/
126-Word Ladder II (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
class Solution {
public:
struct node
{
string word;
node *parent;
node(string &w,node* p=NULL):word(w),parent(p){}
};
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList)
{
bool done=false;
int size;
vector<vector<string>> result;
vector<string> res;
vector<string> to_del;
node *temp;
char letter;
unordered_set<string> dict;
for(string &s:wordList)
dict.insert(s);
if(dict.find(endWord)==dict.end())
return {};
queue<node*> q;
q.push(new node(endWord));
dict.erase(endWord);
while(!q.empty())
{
size=q.size();
while(size--)
{
for(char &c:q.front()->word)
{
letter=c;
for(int i=0;i<26;i++)
{
c='a'+i;
if(q.front()->word==beginWord)
{
done=true;
res.clear();
temp=q.front();
c=letter;
res.push_back(beginWord);
while(temp)
res.push_back(temp->word),temp=temp->parent;
result.push_back(res);
}
else if(dict.find(q.front()->word)!=dict.end())
{
q.push(new node(q.front()->word,q.front()));
to_del.push_back(q.front()->word);
}
}
c=letter;
}
q.pop();
}
for(string &x:to_del)
dict.erase(x);
to_del.clear();
if(done)
return result;
}
return result;
}
};