LeetCode 1461 Check If a String Contains All Binary Codes of Size K (Python)

Posted by 小明MaxMing on March 12, 2021

题目

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

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅