Number of Good Binary Strings - Problem
Binary String Validation Challenge

You're given four parameters: minLength, maxLength, oneGroup, and zeroGroup. Your task is to count how many "good" binary strings exist that satisfy specific block size requirements.

What makes a binary string "good"?
1. Its length is between minLength and maxLength (inclusive)
2. Every consecutive block of 1's has a length that's a multiple of oneGroup
3. Every consecutive block of 0's has a length that's a multiple of zeroGroup

Example: In string "00110111100":
• Blocks of 1's have sizes [2, 4]
• Blocks of 0's have sizes [2, 1, 2]

Return the count modulo 109 + 7. Note that 0 is considered a multiple of all numbers, so empty blocks are always valid.

Input & Output

example_1.py — Basic Case
$ Input: minLength = 2, maxLength = 3, oneGroup = 1, zeroGroup = 2
Output: 2
💡 Note: Valid strings are "00" (length 2) and "111" (length 3). "00" has one block of 0s with size 2 (multiple of 2). "111" has one block of 1s with size 3 (multiple of 1). Other strings like "01", "10", "11" have invalid block sizes.
example_2.py — Multiple Valid Strings
$ Input: minLength = 4, maxLength = 4, oneGroup = 4, zeroGroup = 3
Output: 1
💡 Note: Only "1111" is valid. It has one block of 1s with size 4 (multiple of 4). Strings with 0s would need blocks of size 3, but we need exactly length 4, so mixed strings can't work.
example_3.py — Edge Case
$ Input: minLength = 1, maxLength = 1, oneGroup = 1, zeroGroup = 1
Output: 2
💡 Note: Both "0" and "1" are valid. Each has a single block of size 1, which is a multiple of both group requirements.

Constraints

  • 1 ≤ minLength ≤ maxLength ≤ 105
  • 1 ≤ oneGroup, zeroGroup ≤ maxLength
  • Result fits in 32-bit integer after modulo operation

Visualization

Tap to expand
Building Valid Binary Strings with DPStep 1: Base CaseEmptyStep 2: Add First Valid Blocks00111Step 3: Extend with Valid Blocks0011111100DP State Transitionsdp[length][ending_char] represents count ofvalid strings of given length ending withthe specified character.Transitions:• Add block of 0s (size = k×zeroGroup)• Add block of 1s (size = k×oneGroup)• Can only alternate between different charsTime: O(n²) | Space: O(n)Final Step: Sum Valid LengthsCount all strings with lengths in [minLength, maxLength]Result = Σ dp[i][0] + dp[i][1]
Understanding the Visualization
1
Start Empty
Begin with no chain - this is our base case
2
Add First Segment
Add either 2 blue beads (00) or 3 red beads (111)
3
Extend Chains
From chains ending in blue, we can only add red segments; from chains ending in red, we can only add blue segments
4
Count Valid Lengths
Sum up all chains that fall within our desired length range
Key Takeaway
🎯 Key Insight: Instead of generating all possible strings, we build valid ones incrementally by tracking the ending character and length, ensuring we only add blocks of the correct sizes.
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
52.8K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen