> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Check If a String Contains All Binary Codes

> Tested Python solution for LeetCode 1461 with 25 pytest cases. Generate a practice environment with lcpy.

LeetCode 1461, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation), Rolling Hash, [Hash Function](/catalog/topics/hash-function), Directed Acyclic Graph. [View on LeetCode](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/description/).

Generate this problem as a practice environment: tested reference solution, 25 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1461   # by problem number
lcpy gen -s check_if_a_string_contains_all_binary_codes_of_size_k   # by problem name
```

## Problem

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`.

### Examples

```
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indices 0, 1, 3 and 2 respectively.
```

```
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
```

```
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and does not exist in the array.
```

### Constraints

* `1 <= s.length <= 5 * 10^5`
* `s[i]` is either `'0'` or `'1'`.
* `1 <= k <= 20`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_a_string_contains_all_binary_codes_of_size_k/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_a_string_contains_all_binary_codes_of_size_k/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) each window hashed once; Space: O(2^k) for the set
    def has_all_codes(self, s: str, k: int) -> bool:
        need = 1 << k
        if len(s) < need + k - 1:
            return False
        seen = set()
        for i in range(len(s) - k + 1):
            seen.add(s[i : i + k])
            if len(seen) == need:
                return True
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(n) each window hashed once; Space: O(2^k) for the set | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.