题目
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
解题思路
取出所有长度为k的子串,放到一个集合里,判断是否大小为2^k
代码
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
return len({s[i:i + k] for i in range(len(s)-k+1)}) == 2 ** k