LeetCode solutions

1557. Check If a String Contains All Binary Codes of Size K

My accepted Python solution to LeetCode problem 1557, Check If a String Contains All Binary Codes of Size K, running in 235ms.

  • Difficulty: Medium
  • Python
  • Runtime 235ms
  • Memory 54MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 235ms, memory 54MB, accepted 2025-12-29.

python
class Solution:
    def hasAllCodes(self, s: str, k: int) -> bool:
        if len(s) < k:
            return False
        
        seen = set()
        for i in range(len(s) - k + 1):
            seen.add(s[i:i+k])
        
        return len(seen) == 2 ** k

Source