forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strobogrammatic-number-ii.cpp
57 lines (54 loc) · 1.78 KB
/
strobogrammatic-number-ii.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
// Time: O(n * 5^(n/2))
// Space: O(n)
class Solution {
public:
vector<string> findStrobogrammatic(int n) {
static const unordered_map<string, string> lookup{{"0", "0"}, {"1", "1"},
{"6", "9"}, {"8", "8"},
{"9", "6"}};
vector<string> result = {""};
if (n % 2) {
result = {"0", "1", "8"};
}
for (int i = n % 2; i < n; i += 2) {
vector<string> new_result;
for (const auto& num : result) {
for (const auto& [a, b] : lookup) {
if (i != n - 2 || a != "0") {
new_result.emplace_back(a + num + b);
}
}
}
result = move(new_result);
}
return result;
}
};
// Time: O(n * 5^(n/2))
// Space: O(n)
class Solution2 {
public:
vector<string> findStrobogrammatic(int n) {
return findStrobogrammaticRecu(n, n);
}
private:
vector<string> findStrobogrammaticRecu(const int n, int k) {
static const unordered_map<string, string> lookup{{"0", "0"}, {"1", "1"},
{"6", "9"}, {"8", "8"},
{"9", "6"}};
if (k == 0) {
return {""};
} else if (k == 1) {
return {"0", "1", "8"};
}
vector<string> result;
for (const auto& num : findStrobogrammaticRecu(n, k - 2)) {
for (const auto& kvp : lookup) {
if (n != k || kvp.first != "0") {
result.emplace_back(kvp.first + num + kvp.second);
}
}
}
return result;
}
};