## [检查一个字符串是否包含所有长度为 K 的二进制子串](https://leetcode-cn.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/)
#### 思路
* 当看到这道题,最开始蹦出来的想法是:根据长度K生成所有可能的二进制字符串。再遍历这些字符串,判断是不是存在于string中。这样显然不是很好,应为K的长度是不可控的,题目限定最大值为20,即最多`$ 2^{20} $`。
* **更换思路**,一共两个变量,s和k。那换个维度,从s角度入手。遍历s,拿出所有长度为k的字串,判断是不是都包含所有k长的二进制字符串
* 如何判断呢?从s中拿出字串有很多重复,要排除重复自然想到用hashmap存放。既然排除了重复就没必要比较内容了,我们只要判断所有没有重复的字串的个数是不是k能生成所有可能性的个数就好了
AC!
#### 代码
python3
```
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
m = set()
for i in range(len(s)-k+1):
m.add(s[i:i+k])
return len(m) == (1 << k)
```
- 目录
- excel-sheet-column-number
- divide-two-integers
- house-robber
- fraction-to-recurring-decimal
- profile
- kids-with-the-greatest-number-of-candies
- qiu-12n-lcof
- new-21-game
- product-of-array-except-self
- minimum-depth-of-binary-tree
- univalued-binary-tree
- shun-shi-zhen-da-yin-ju-zhen-lcof
- permutations
- satisfiability-of-equality-equations
- word-ladder-ii
- ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
- palindrome-number
- network-delay-time
- daily-temperatures
- longest-common-prefix
- sum-of-mutated-array-closest-to-target
- 周赛专题
- make-two-arrays-equal-by-reversing-sub-arrays
- check-if-a-string-contains-all-binary-codes-of-size-k
- course-schedule-iv
- cherry-pickup-ii
- maximum-product-of-two-elements-in-an-array
- maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
- reorder-routes-to-make-all-paths-lead-to-the-city-zero
- probability-of-a-two-boxes-having-the-same-number-of-distinct-balls
- shuffle-the-array
- the-k-strongest-values-in-an-array
- design-browser-history
- paint-house-iii
- final-prices-with-a-special-discount-in-a-shop