-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_127.cpp
40 lines (40 loc) · 1.06 KB
/
LC_127.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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string>s(wordList.begin(),wordList.end());
queue<string>q;
q.push(beginWord);
string temp;
int i,len,jump=1,sz,j,k;
char c;
while(!q.empty())
{
sz=q.size();
for(i=0;i<sz;i++)
{
temp=q.front();
q.pop();
if(temp==endWord)
return jump;
s.erase(temp);
len=temp.length();
for(j=0;j<len;j++)
{
c=temp[j];
for(k=0;k<26;k++)
{
temp[j]='a' + k;
if(temp[j]==c)
continue;
if(s.count(temp))
q.push(temp);
}
temp[j]=c;
}
}
jump++;
}
return 0;
}
};