|
| 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>给你一个二进制字符串 <code>s</code> 和一个整数 <code>k</code> 。如果所有长度为 <code>k</code> 的二进制字符串都是 <code>s</code> 的子串,请返回 <code>true</code> ,否则请返回 <code>false</code> 。</p> |
| 13 | + |
| 14 | +<p> </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> </p> |
| 41 | + |
| 42 | +<p><strong>提示:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li> |
| 46 | + <li><code>s[i]</code> 不是<code>'0'</code> 就是 <code>'1'</code></li> |
| 47 | + <li><code>1 <= k <= 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) |
0 commit comments