-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path127.word-ladder.cpp
39 lines (31 loc) · 1.14 KB
/
127.word-ladder.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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
queue<string> q;
unordered_map<string, int> visited;
unordered_set<string> exists(wordList.begin(), wordList.end());
q.push(beginWord);
visited[beginWord] = 1;
while (!q.empty()) {
string currWord = q.front();
q.pop();
for (int i = 0; i < currWord.size(); i++) {
string temp = currWord;
// Replace each character of the current word with all possible letters
for (char j = 'a'; j <= 'z'; j++) {
temp[i] = j;
// Check if the new word is in the word list and has not been visited before
if (exists.count(temp) && visited.count(temp) == 0) {
visited[temp] = visited[currWord] + 1;
q.push(temp);
// Stop BFS once we reach the end word
if (temp == endWord) {
return visited[temp];
}
}
}
}
}
return 0; // If no path exists between beginWord and endWord
}
};