-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path127.cpp
More file actions
31 lines (31 loc) · 1.08 KB
/
127.cpp
File metadata and controls
31 lines (31 loc) · 1.08 KB
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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int wordsize = beginWord.size();
unordered_set<string> words(wordList.begin(),wordList.end()),visited;
if(!words.count(endWord))
return 0;
queue<pair<string,int>> q;
q.push({beginWord,1});
visited.insert(beginWord);
while(!q.empty()){
string beginWord = q.front().first;
int count = q.front().second;
q.pop();
for(int i=0; i<wordsize; i++){
char tmp = beginWord[i];
for(int k = 0; k<26; k++){
beginWord[i] = (char)(k+'a');
if(words.count(beginWord) && !visited.count(beginWord)){
if(beginWord == endWord)
return (count+1);
visited.insert(beginWord);
q.push({beginWord,count+1});
}
}
beginWord[i] = tmp;
}
}
return 0;
}
};