Ctrl K

Binary Strings Without Long Runs

Medium
You build a string out of exactly zeros zeros and ones ones. A string qualifies if it has no run of equal characters longer than max_run, that is, no max_run + 1 consecutive characters are all the same. Count the qualifying strings. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
zeros=2, ones=1, max_run=1 → 1

Only 010 works: 001 and 100 put two zeros side by side.

Example 2
zeros=2, ones=2, max_run=2 → 6

No run can exceed 2 here anyway, so all C(4, 2) = 6 arrangements count.

Example 3
zeros=3, ones=2, max_run=2 → 7

Out of 10 arrangements, drop those with three zeros in a row: 00011, 10001, 11000. That leaves 7.

Constraints
  • 1 ≤ zeros, ones, max_run ≤ 200
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit for the Best Averages MediumThe Growing Grab Limit MediumSegments With an Odd Sum MediumHop Across Safe Tiles MediumCalm Before and After Medium
loading Python…
Input
zeros=2, ones=1, max_run=1
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.