Check If a String Contains All Binary Codes of Size K

Given a binary string s and an integer k, return true if every binary code of length k appears as a substring of s, and false otherwise.

30:00

Check If a String Contains All Binary Codes of Size K
medium
Free written solution
Topics
Companies

Given a binary string s and an integer k, return true if every binary code of length k appears as a substring of s, and false otherwise.

Example 1:
Input: {"s":"00110110","k":2}
Output: true
Constraints:
  • 1≤s.length≤5×1051 \leq s.\text{length} \leq 5 \times 10^5

  • s[i] is either '0' or '1'.

  • 1≤k≤201 \leq k \leq 20

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"00110110","k":2}

There are 2^2 = 4 different binary codes of length 2. Every one of them must show up in s.

s, window of k = 2

0

0

0

1

1

2

1

3

0

4

1

5

1

6

0

7

codes seen

0 / 4

Variables
VariableValue
k2
need4
DepthFunction Call
Stack empty
0/9