Skip to content

Commit b915669

Browse files
committed
update: 添加问题“1461.检查一个字符串是否包含所有长度为K的二进制子串”的代码和题解 (#1407)
1461: AC.cpp (#1406) - AC,40.08%,36.38% Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 5857f55 commit b915669

9 files changed

Lines changed: 131 additions & 42 deletions

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1461: AC.cpp (#1406) - AC,40.08%,36.38%
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-23 11:30:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-23 11:37:00
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
// THIS CANNOT BE ACCEPTED
11+
class Solution {
12+
public:
13+
bool hasAllCodes(string s, int k) {
14+
unordered_set<string> ma;
15+
for (int i = 0, n = s.size(); i < n; i++) {
16+
for (int l = 1; l <= k && i + l <= n; l++) {
17+
ma.insert(s.substr(i, l));
18+
}
19+
}
20+
return ma.size() == (1 << k);
21+
}
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-23 11:30:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-23 11:37:10
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
bool hasAllCodes(string s, int k) {
14+
unordered_set<string> ma;
15+
for (int i = 0, to = s.size() - k + 1; i < to; i++) {
16+
ma.insert(s.substr(i, k));
17+
}
18+
return ma.size() == (1 << k);
19+
}
20+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@
592592
|1457.二叉树中的伪回文路径|中等|<a href="https://leetcode.cn/problems/pseudo-palindromic-paths-in-a-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/25/LeetCode%201457.%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E7%9A%84%E4%BC%AA%E5%9B%9E%E6%96%87%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134617854" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pseudo-palindromic-paths-in-a-binary-tree/solutions/2541535/letmefly-1457er-cha-shu-zhong-de-wei-hui-91vp/" target="_blank">LeetCode题解</a>|
593593
|1458.两个子序列的最大点积|困难|<a href="https://leetcode.cn/problems/max-dot-product-of-two-subsequences/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/08/LeetCode%201458.%E4%B8%A4%E4%B8%AA%E5%AD%90%E5%BA%8F%E5%88%97%E7%9A%84%E6%9C%80%E5%A4%A7%E7%82%B9%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156713484" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-dot-product-of-two-subsequences/solutions/3875454/letmefly-1458liang-ge-zi-xu-lie-de-zui-d-3f54/" target="_blank">LeetCode题解</a>|
594594
|1460.通过翻转子数组使两个数组相等|简单|<a href="https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/24/LeetCode%201460.%E9%80%9A%E8%BF%87%E7%BF%BB%E8%BD%AC%E5%AD%90%E6%95%B0%E7%BB%84%E4%BD%BF%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9B%B8%E7%AD%89/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126499790" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/solution/letmefly-1460tong-guo-fan-zhuan-zi-shu-z-q812/" target="_blank">LeetCode题解</a>|
595+
|1461.检查一个字符串是否包含所有长度为K的二进制子串|中等|<a href="https://leetcode.cn/problems/check-if-a-string-contains-all-binary-codes-of-size-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/23/LeetCode%201461.%E6%A3%80%E6%9F%A5%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158315342" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-string-contains-all-binary-codes-of-size-k/solutions/3906737/letmefly-1461jian-cha-yi-ge-zi-fu-chuan-fs9a3/" target="_blank">LeetCode题解</a>|
595596
|1462.课程表IV|中等|<a href="https://leetcode.cn/problems/course-schedule-iv/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/09/12/LeetCode%201462.%E8%AF%BE%E7%A8%8B%E8%A1%A8IV/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132825649" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/course-schedule-iv/solutions/2438404/letmefly-1462ke-cheng-biao-ivtuo-bu-pai-gufke/" target="_blank">LeetCode题解</a>|
596597
|1464.数组中两元素的最大乘积|简单|<a href="https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/26/LeetCode%201464.%E6%95%B0%E7%BB%84%E4%B8%AD%E4%B8%A4%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E4%B9%98%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126536351" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/solution/letmefly-1464shu-zu-zhong-liang-yuan-su-txf8l/" target="_blank">LeetCode题解</a>|
597598
|1465.切割后面积最大的蛋糕|中等|<a href="https://leetcode.cn/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/27/LeetCode%201465.%E5%88%87%E5%89%B2%E5%90%8E%E9%9D%A2%E7%A7%AF%E6%9C%80%E5%A4%A7%E7%9A%84%E8%9B%8B%E7%B3%95/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134073948" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/solutions/2500697/letmefly-1465qie-ge-hou-mian-ji-zui-da-d-xqai/" target="_blank">LeetCode题解</a>|
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: 1461.检查一个字符串是否包含所有长度为 K 的二进制子串:哈希表存放所有长度为k的子串
3+
date: 2026-02-23 11:39:33
4+
tags: [题解, LeetCode, 中等, 位运算, 哈希表, 字符串, 暴力, 枚举]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1461.检查一个字符串是否包含所有长度为 K 的二进制子串:哈希表存放所有长度为k的子串
9+
10+
力扣题目链接:[https://leetcode.cn/problems/check-if-a-string-contains-all-binary-codes-of-size-k/](https://leetcode.cn/problems/check-if-a-string-contains-all-binary-codes-of-size-k/)
11+
12+
<p>给你一个二进制字符串&nbsp;<code>s</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。如果所有长度为 <code>k</code>&nbsp;的二进制字符串都是 <code>s</code>&nbsp;的子串,请返回 <code>true</code> ,否则请返回 <code>false</code> 。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre>
19+
<strong>输入:</strong>s = "00110110", k = 2
20+
<strong>输出:</strong>true
21+
<strong>解释:</strong>长度为 2 的二进制串包括 "00","01","10" 和 "11"。它们分别是 s 中下标为 0,1,3,2 开始的长度为 2 的子串。
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>s = "0110", k = 1
28+
<strong>输出:</strong>true
29+
<strong>解释:</strong>长度为 1 的二进制串包括 "0" 和 "1",显然它们都是 s 的子串。
30+
</pre>
31+
32+
<p><strong>示例 3:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>s = "0110", k = 2
36+
<strong>输出:</strong>false
37+
<strong>解释:</strong>长度为 2 的二进制串 "00" 没有出现在 s 中。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>5</sup></code></li>
46+
<li><code>s[i]</code> 不是<code>'0'</code> 就是 <code>'1'</code></li>
47+
<li><code>1 &lt;= k &lt;= 20</code></li>
48+
</ul>
49+
50+
51+
52+
## 解题方法:暴力枚举
53+
54+
使用一个哈希表,存放所有长度为$k$的子串。
55+
56+
最后看看哈希表中子串数量是否等于$2^k$。
57+
58+
+ 时间复杂度$O((len(s) - k)\times k)$
59+
+ 空间复杂度$O((len(s) - k)\times k)$
60+
61+
### AC代码
62+
63+
#### C++
64+
65+
```cpp
66+
/*
67+
* @LastEditTime: 2026-02-23 11:37:10
68+
*/
69+
class Solution {
70+
public:
71+
bool hasAllCodes(string s, int k) {
72+
unordered_set<string> ma;
73+
for (int i = 0, to = s.size() - k + 1; i < to; i++) {
74+
ma.insert(s.substr(i, k));
75+
}
76+
return ma.size() == (1 << k);
77+
}
78+
};
79+
```
80+
81+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158315342)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/23/LeetCode%201461.%E6%A3%80%E6%9F%A5%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E4%B8%B2/)哦~
82+
>
83+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,8 @@ categories: [自用]
18291829
|praise|v. 赞扬<br/>n. 赞扬|
18301830
|||
18311831
|stipulation|n. 规定,约定,合同|
1832+
|||
1833+
|turnip|n. 萝卜|
18321834

18331835
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
18341836
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

showRmScript.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

testDaka.bash.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

todo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# rm Codes\1461-check-if-a-string-contains-all-binary-codes-of-size-k_AC.cpp
2+
13
# 记录iphone快捷指令 自动化 当到达某位置时触发
24

35
# move beautify.sh to github repo/gist

0 commit comments

Comments
 (0)