Number of Good Binary Strings - Problem
You are given four integers minLength, maxLength, oneGroup and zeroGroup.
A binary string is good if it satisfies the following conditions:
- The length of the string is in the range
[minLength, maxLength]. - The size of each block of consecutive 1's is a multiple of
oneGroup. - The size of each block of consecutive 0's is a multiple of
zeroGroup.
For example, in a binary string 00110111100, sizes of each block of consecutive ones are [2,4] and sizes of each block of consecutive zeros are [2,1,2].
Return the number of good binary strings. Since the answer may be too large, return it modulo 10⁹ + 7.
Note that 0 is considered a multiple of all numbers.
Input & Output
Example 1 — Basic Case
$
Input:
minLength = 4, maxLength = 4, oneGroup = 4, zeroGroup = 3
›
Output:
1
💡 Note:
Valid strings of length 4: Only '1111' is valid (one block of 1s with size 4, 4 % 4 = 0). '0000' is invalid because block size 4 is not divisible by zeroGroup=3.
Example 2 — Multiple Valid Strings
$
Input:
minLength = 2, maxLength = 3, oneGroup = 2, zeroGroup = 1
›
Output:
8
💡 Note:
For zeroGroup=1, any block of 0s is valid. For oneGroup=2, blocks of 1s must have even size. Valid strings include: '00', '11', '000', '011', '101', '110', etc.
Example 3 — No Valid Strings
$
Input:
minLength = 1, maxLength = 1, oneGroup = 2, zeroGroup = 2
›
Output:
0
💡 Note:
Length 1 strings are '0' or '1'. '0' has block size 1, but 1 % 2 ≠ 0. '1' has block size 1, but 1 % 2 ≠ 0. No valid strings exist.
Constraints
- 1 ≤ minLength ≤ maxLength ≤ 105
- 1 ≤ oneGroup, zeroGroup ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code