forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-ladder.cpp
79 lines (77 loc) · 2.75 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Time: O(b^(d/2)), b is the branch factor of bfs, d is the result depth
// Space: O(w * l), w is the number of words, l is the max length of words
// two-end bfs
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> words(cbegin(wordList), cend(wordList));
if (!words.count(endWord)) {
return 0;
}
int ladder = 2;
unordered_set<string> left = {beginWord}, right = {endWord};
while (!empty(left)) {
for (const auto& word : left) {
words.erase(word);
}
unordered_set<string> new_left;
for (const auto& word : left) {
auto new_word = word;
for (int i = 0; i < size(new_word); ++i) {
char prev = new_word[i];
for (int j = 0; j < 26; ++j) {
new_word[i] = 'a' + j;
if (!words.count(new_word)) {
continue;
}
if (right.count(new_word)) {
return ladder;
}
new_left.emplace(new_word);
}
new_word[i] = prev;
}
}
left = move(new_left);
++ladder;
if (size(left) > size(right)) {
swap(left, right);
}
}
return 0;
}
};
// Time: O(b^d), b is the branch factor of bfs, d is the result depth
// Space: O(w * l), w is the number of words, l is the max length of words
class Solution2 {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> lookup(cbegin(wordList), cend(wordList));
if (!lookup.count(endWord)) {
return 0;
}
int ladder = 2;
for (vector<string> q = {beginWord}; !q.empty(); ++ladder) {
vector<string> new_q;
for (const auto& word : q) {
auto new_word = word;
for (int i = 0; i < new_word.length(); ++i) {
char prev = new_word[i];
for (int j = 0; j < 26; ++j) {
new_word[i] = 'a' + j;
if (new_word == endWord) {
return ladder;
}
if (lookup.count(new_word)) {
lookup.erase(new_word);
new_q.emplace_back(new_word);
}
}
new_word[i] = prev;
}
}
q = move(new_q);
}
return 0;
}
};