Skip to content

Commit f54bf1e

Browse files
committed
feat: Add solution for LeetCode #1461
1 parent e8a6931 commit f54bf1e

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

SOLVED.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
| 1419 | [Minimum Number of Frogs Croaking](https://leetcode.com/problems/minimum-number-of-frogs-croaking/) | [Code](src/leetcode/problems/minimum-number-of-frogs-croaking.cpp) / [Test](test/leetcode/problems/minimum-number-of-frogs-croaking.cpp) |
162162
| 1446 | [Consecutive Characters](https://leetcode.com/problems/consecutive-characters/) | [Code](src/leetcode/problems/consecutive-characters.cpp) / [Test](test/leetcode/problems/consecutive-characters.cpp) |
163163
| 1458 | [Max Dot Product of Two Subsequences](https://leetcode.com/problems/max-dot-product-of-two-subsequences/) | [Code](src/leetcode/problems/max-dot-product-of-two-subsequences.cpp) / [Test](test/leetcode/problems/max-dot-product-of-two-subsequences.cpp) |
164+
| 1461 | [Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Code](src/leetcode/problems/check-if-a-string-contains-all-binary-codes-of-size-k.cpp) / [Test](test/leetcode/problems/check-if-a-string-contains-all-binary-codes-of-size-k.cpp) |
164165
| 1499 | [Max Value of Equation](https://leetcode.com/problems/max-value-of-equation/) | [Code](src/leetcode/problems/max-value-of-equation.cpp) / [Test](test/leetcode/problems/max-value-of-equation.cpp) |
165166
| 1510 | [Stone Game IV](https://leetcode.com/problems/stone-game-iv/) | [Code](src/leetcode/problems/stone-game-iv.cpp) / [Test](test/leetcode/problems/stone-game-iv.cpp) |
166167
| 1514 | [Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability/) | [Code](src/leetcode/problems/path-with-maximum-probability.cpp) / [Test](test/leetcode/problems/path-with-maximum-probability.cpp) |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "leetcode/core.h"
3+
4+
namespace leetcode {
5+
namespace problem_1461 {
6+
7+
using Func = std::function<bool(string, int)>;
8+
9+
class CheckIfAStringContainsAllBinaryCodesOfSizeKSolution : public SolutionBase<Func> {
10+
public:
11+
//! 1461. Check If a String Contains All Binary Codes of Size K
12+
//! https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/
13+
bool hasAllCodes(string s, int k);
14+
15+
CheckIfAStringContainsAllBinaryCodesOfSizeKSolution();
16+
};
17+
18+
} // namespace problem_1461
19+
} // namespace leetcode
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
#include "leetcode/problems/check-if-a-string-contains-all-binary-codes-of-size-k.h"
3+
4+
namespace leetcode {
5+
namespace problem_1461 {
6+
7+
// 滑动窗口 + 位运算 + 哈希集合
8+
// 时间复杂度: O(n), 空间复杂度: O(2^k)
9+
static bool solution1(string s, int k) {
10+
const int n = s.size();
11+
const int total = 1 << k; // 2^k
12+
13+
// 剪枝:字符串长度不足以包含所有子串
14+
if (n < k + total - 1) {
15+
return false;
16+
}
17+
18+
unordered_set<int> seen;
19+
int mask = total - 1; // k 个 1,用于取低 k 位
20+
int num = 0;
21+
22+
// 初始化窗口
23+
for (int i = 0; i < k; ++i) {
24+
num = (num << 1) | (s[i] - '0');
25+
}
26+
seen.insert(num);
27+
28+
// 滑动窗口
29+
for (int i = k; i < n; ++i) {
30+
// 去掉最高位,加入新位
31+
num = ((num << 1) & mask) | (s[i] - '0');
32+
seen.insert(num);
33+
// 提前退出:如果已经找到所有子串
34+
if (seen.size() == total) {
35+
return true;
36+
}
37+
}
38+
39+
return seen.size() == total;
40+
}
41+
42+
// 使用 vector<bool> 替代 unordered_set,空间更优
43+
// 时间复杂度: O(n), 空间复杂度: O(2^k)
44+
static bool solution2(string s, int k) {
45+
const int n = s.size();
46+
const int total = 1 << k; // 2^k
47+
48+
// 剪枝
49+
if (n < k + total - 1) {
50+
return false;
51+
}
52+
53+
vector<bool> seen(total, false);
54+
int count = 0;
55+
int mask = total - 1;
56+
int num = 0;
57+
58+
// 初始化窗口
59+
for (int i = 0; i < k; ++i) {
60+
num = (num << 1) | (s[i] - '0');
61+
}
62+
seen[num] = true;
63+
count = 1;
64+
65+
// 滑动窗口
66+
for (int i = k; i < n; ++i) {
67+
num = ((num << 1) & mask) | (s[i] - '0');
68+
if (!seen[num]) {
69+
seen[num] = true;
70+
++count;
71+
if (count == total) {
72+
return true;
73+
}
74+
}
75+
}
76+
77+
return count == total;
78+
}
79+
80+
CheckIfAStringContainsAllBinaryCodesOfSizeKSolution::CheckIfAStringContainsAllBinaryCodesOfSizeKSolution() {
81+
setMetaInfo({.id = 1461,
82+
.title = "Check If a String Contains All Binary Codes of Size K",
83+
.url = "https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/"});
84+
registerStrategy("Sliding Window + HashSet", solution1);
85+
registerStrategy("Sliding Window + Vector", solution2);
86+
}
87+
88+
bool CheckIfAStringContainsAllBinaryCodesOfSizeKSolution::hasAllCodes(string s, int k) {
89+
return getSolution()(s, k);
90+
}
91+
92+
} // namespace problem_1461
93+
} // namespace leetcode
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#include "leetcode/problems/check-if-a-string-contains-all-binary-codes-of-size-k.h"
3+
4+
#include "gtest/gtest.h"
5+
6+
namespace leetcode {
7+
namespace problem_1461 {
8+
9+
class CheckIfAStringContainsAllBinaryCodesOfSizeKTest : public ::testing::TestWithParam<string> {
10+
protected:
11+
void SetUp() override { solution.setStrategy(GetParam()); }
12+
13+
CheckIfAStringContainsAllBinaryCodesOfSizeKSolution solution;
14+
};
15+
16+
TEST_P(CheckIfAStringContainsAllBinaryCodesOfSizeKTest, Example1) {
17+
string s = "00110110";
18+
int k = 2;
19+
EXPECT_TRUE(solution.hasAllCodes(s, k));
20+
}
21+
22+
TEST_P(CheckIfAStringContainsAllBinaryCodesOfSizeKTest, Example2) {
23+
string s = "0110";
24+
int k = 1;
25+
EXPECT_TRUE(solution.hasAllCodes(s, k));
26+
}
27+
28+
TEST_P(CheckIfAStringContainsAllBinaryCodesOfSizeKTest, Example3) {
29+
string s = "0110";
30+
int k = 2;
31+
EXPECT_FALSE(solution.hasAllCodes(s, k));
32+
}
33+
34+
INSTANTIATE_TEST_SUITE_P(
35+
LeetCode, CheckIfAStringContainsAllBinaryCodesOfSizeKTest,
36+
::testing::ValuesIn(CheckIfAStringContainsAllBinaryCodesOfSizeKSolution().getStrategyNames()));
37+
38+
} // namespace problem_1461
39+
} // namespace leetcode

0 commit comments

Comments
 (0)