## [檢查一個字符串是否包含所有長度為 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