-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordLadder.cpp
More file actions
31 lines (29 loc) · 1.09 KB
/
wordLadder.cpp
File metadata and controls
31 lines (29 loc) · 1.09 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
// Source: https://leetcode.com/problems/word-ladder/
// Author: Miao Zhang
// Date: 2021-01-20
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
set<string> wordset(wordList.begin(), wordList.end());
string wordsdict = "abcdefghijklmnopqrstuvwxyz";
deque<pair<string, int>> q;
q.push_back(make_pair(beginWord, 1));
while (!q.empty()) {
pair<string, int> cell = q.front();
q.pop_front();
if (cell.first == endWord) {
return cell.second;
}
for (int i = 0; i < cell.first.size(); i++) {
for (auto c: wordsdict) {
string newword = cell.first.substr(0, i) + c + cell.first.substr(i + 1);
if (wordset.find(newword) != wordset.end() && newword != cell.first) {
wordset.erase(newword);
q.push_back(make_pair(newword, cell.second + 1));
}
}
}
}
return 0;
}
};