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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 235ms, memory 54MB, accepted 2025-12-29.
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