-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordLadder-I.cpp
35 lines (35 loc) · 1.05 KB
/
WordLadder-I.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
class Solution {
public:
int ladderLength(string startWord, string targetWord, vector<string>& wordList) {
queue<pair<string,int>>q;
q.push({startWord,1});
unordered_set<string>s(wordList.begin(),wordList.end());
s.erase(startWord);
int ans=0;
while(!q.empty()){
string word=q.front().first;
int freq=q.front().second;
q.pop();
if(word==targetWord){
return freq;
}
// hat initial word
for(int i=0;i<word.size();i++){
char original=word[i];
// aat
// bat
// cat.......... zat
for(char ch='a';ch<='z';ch++){
word[i]=ch;
// it exists in the set
if(s.find(word)!=s.end()){
s.erase(word);
q.push({word,freq+1});
}
}
word[i]=original;
}
}
return 0;
}
};