-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path127. Word Ladder.cpp
More file actions
36 lines (31 loc) · 1.07 KB
/
127. Word Ladder.cpp
File metadata and controls
36 lines (31 loc) · 1.07 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
/*
Problem Link: https://leetcode.com/problems/word-ladder/
Time: 84 ms (Beats 94.98%), Space: 13.9 MB (Beats 85.42%)
*/
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
queue<pair<string,int>> q;
q.push({beginWord, 1});
unordered_set<string> st(wordList.begin(), wordList.end());
st.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 initialChar= word[i]; // imp to store the char for replacing it back if noone matches
for(char c='a'; c<='z'; c++){
word[i]= c;
if(st.find(word) != st.end()){
st.erase(word);
q.push({word, steps + 1});
}
}
word[i]= initialChar;
}
}
return 0;
}
};