-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path127-Word Ladder (Bi-Directional BFS).cpp
47 lines (47 loc) · 1.4 KB
/
127-Word Ladder (Bi-Directional 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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string> dict,front,back,temp;
unordered_set<string>::const_iterator it;
char letter;
string word;
dict.reserve(wordList.size());
for(string &i:wordList)
dict.insert(i);
if(dict.find(endWord)==dict.end())
return 0;
front.insert(beginWord);
back.insert(endWord);
int dist=2;
while(!front.empty()&&!back.empty())
{
temp.clear();
if(back.size()<front.size())
front.swap(back);
for(it=front.begin();it!=front.end();it++)
{
word=*it;
for(int i=0;i<word.length();i++)
{
letter=word[i];
for(int j=0;j<26;j++)
{
word[i]='a'+j;
if(back.find(word)!=back.end())
return dist;
if(dict.find(word)!=dict.end())
{
temp.insert(word);
dict.erase(word);
}
}
word[i]=letter;
}
}
temp.swap(front);
dist++;
}
return 0;
}
};