-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0127_ladderLength.cc
165 lines (157 loc) · 4.09 KB
/
Problem_0127_ladderLength.cc
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 应该根据具体数据状况决定用什么来找邻居
// 1)如果字符串长度比较短,字符串数量比较多,以下方法适合
// 2)如果字符串长度比较长,字符串数量比较少,以下方法不适合
vector<string> getNext(string word, unordered_set<string>& dict)
{
vector<string> res;
for (char cur = 'a'; cur <= 'z'; cur++)
{
for (int i = 0; i < word.length(); i++)
{
if (word[i] != cur)
{
// current word such as "dog"
// then list all d?g
char tmp = word[i];
word[i] = cur;
if (dict.count(word))
{
res.push_back(word);
}
word[i] = tmp;
}
}
}
return res;
}
unordered_map<string, vector<string>> getNexts(vector<string>& words)
{
unordered_set<string> dict(words.begin(), words.end());
unordered_map<string, vector<string>> nexts;
for (int i = 0; i < words.size(); i++)
{
// save next words only one char difference with current word
nexts.emplace(words[i], getNext(words[i], dict));
}
return nexts;
}
// 普通 bfs
int ladderLength1(string beginWord, string endWord, vector<string>& wordList)
{
wordList.push_back(beginWord);
unordered_map<string, vector<string>> nexts = getNexts(wordList);
unordered_map<string, int> distanceMap;
distanceMap.emplace(beginWord, 1);
unordered_set<string> set;
set.emplace(beginWord);
queue<string> queue;
queue.push(beginWord);
// bfs
while (!queue.empty())
{
string cur = queue.front();
queue.pop();
int distance = distanceMap.at(cur);
for (string next : nexts.at(cur))
{
if (next == endWord)
{
return distance + 1;
}
// filter visited word
if (!set.count(next))
{
set.emplace(next);
queue.push(next);
distanceMap.emplace(next, distance + 1);
}
}
}
return 0;
}
// 针对普通 bfs,利用双向 bfs,降低常数复杂度
// begin -> {10单词}
// end -> {5单词} 明显这一轮从单词 end 进行 bfs 的分支更少
int ladderLength2(string beginWord, string endWord, vector<string>& wordList)
{
// 总词表
unordered_set<string> dict(wordList.begin(), wordList.end());
if (!dict.count(endWord))
{
return 0;
}
// 数量小的一侧
unordered_set<string> smallLevel;
// 数量大的一侧
unordered_set<string> bigLevel;
// 由数量小的一侧,所扩展出的下一层
unordered_set<string> nextLevel;
smallLevel.emplace(beginWord);
bigLevel.emplace(endWord);
for (int len = 2; !smallLevel.empty(); len++)
{
for (auto word : smallLevel)
{
// 从小侧扩展
for (int i = 0; i < word.length(); i++)
{
// 每一位字符都尝试
char old = word[i];
for (char c = 'a'; c <= 'z'; c++)
{
if (c == old)
{
continue;
}
word[i] = c;
if (bigLevel.count(word))
{
return len;
}
if (dict.count(word))
{
dict.erase(word);
nextLevel.emplace(word);
}
}
word[i] = old;
}
}
if (nextLevel.size() <= bigLevel.size())
{
smallLevel = std::move(nextLevel);
}
else
{
smallLevel = std::move(bigLevel);
bigLevel = std::move(nextLevel);
}
}
return 0;
}
};
void testLadderLength()
{
Solution s;
vector<string> w1 = {"hot", "dot", "dog", "lot", "log", "cog"};
vector<string> w2 = {"hot", "dot", "dog", "lot", "log"};
EXPECT_EQ_INT(5, s.ladderLength1("hit", "cog", w1));
EXPECT_EQ_INT(0, s.ladderLength1("hit", "cog", w2));
EXPECT_SUMMARY;
}
int main()
{
testLadderLength();
return 0;
}