-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 127.cpp
More file actions
43 lines (36 loc) · 1.12 KB
/
Leetcode 127.cpp
File metadata and controls
43 lines (36 loc) · 1.12 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
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
private:
// bool differby1(string s,string &s1){
// int count=0;
// for(int i=0;i<s.size();i++){
// if(s[i]!=s1[i])count++;
// }
// if(count==1)return true;
// return false;
// }
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
queue<pair<string,int>>q;
q.push({beginWord,1});
unordered_set<string>s(wordList.begin(),wordList.end());
s.erase(beginWord);
while(!q.empty()){
string word = q.front().first;
int steps = q.front().second;
q.pop();
if(word == endWord)return steps;
for(int i=0;i<word.size();i++){
char original = word[i];
for(char ch ='a';ch<='z';ch++){
word[i]=ch;
if(s.find(word)!=s.end()){
s.erase(word);
q.push({word,steps+1});
}
}
word[i]=original;
}
}
return 0;
}
};