Back to DSA sheet

Check if a String Contains all Binary Codes of Size K

MediumArrays

Given a binary string and an integer k, decide whether every possible binary code of length k appears as a substring at least once.

Input format (stdin): the first line is the binary string, the second line is k. Output true or false.

Examples
Input: 00110110 2
Output: true
00,01,10,11 all appear.
Input: 0110 2
Output: false
00 never appears.
Constraints
  • 1 <= length <= 5*10^5
  • 1 <= k <= 20
Sheets
NeetCode 250
check-if-a-string-contains-all-binary-codes-of-size-k.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.