-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_ladder.cpp
55 lines (46 loc) · 1.73 KB
/
word_ladder.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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> dict (wordList.begin(),wordList.end());
if(dict.count(endWord)==0)
return 0;
int level =1;
queue<string> bfs_queue;
bfs_queue.push(beginWord);
while(!bfs_queue.empty())
{
int size = bfs_queue.size();
for(int i=0;i<size;i++)
{
string word = bfs_queue.front();
bfs_queue.pop();
// if next neighbour is the endword return from current level only
// no need to process further since goal is reached
if(word.compare(endWord)==0)
{
return level;
}
for(int j =0 ;j<word.size() ;j++)
{
char c= word[j];
// make permutations of word
for(int k =0;k<26; k++)
{
word[j] = 'a' + k;
// if word permutation exists in dictionary add permutation as next neighbor to q and
// delete word from dict since we already found it
if(dict.count(word) >0 )
{
bfs_queue.push(word);
dict.erase(word);
}
}
// original word back
word[j] = c;
}
}
++level;
}
return 0;
}
};