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
Good Binary Strings - DP Approach INPUT minLength = 4 maxLength = 4 oneGroup = 4 zeroGroup = 3 Valid Patterns (length=4): 1 1 1 1 OK 0 0 0 0 NO 4 zeros need zeroGroup=3 (4 is not multiple of 3) Empty string "" counts as valid (base case) ALGORITHM STEPS 1 Initialize DP dp[i] = count of good strings of length i 2 Base Case dp[0] = 1 (empty string) 3 Transition For each length i: dp[i] += dp[i-oneGroup] dp[i] += dp[i-zeroGroup] 4 Sum Result Sum dp[minLen..maxLen] DP Table (length 0-4): i=0 i=1 i=2 i=3 i=4 1 0 0 1 2 FINAL RESULT Output: 2 Two Good Strings: String 1: 1 1 1 1 4x1s String 2: 0 0 0 1 3+1 Note: 0001 has 3 zeros (multiple of 3) + 1 one (not multiple of 4) - invalid Only "1111" valid at len=4 dp[4]=2 includes transitions Key Insight: DP builds valid strings by appending blocks. dp[i] = dp[i-oneGroup] + dp[i-zeroGroup]. Each transition adds a valid block of 1s (size oneGroup) or 0s (size zeroGroup). Time: O(maxLength), Space: O(maxLength). Answer = sum of dp[minLength] to dp[maxLength]. TutorialsPoint - Number of Good Binary Strings | Dynamic Programming Approach
Asked in
Meta 15 Google 12 Amazon 8
23.4K Views
Medium Frequency
~35 min Avg. Time
892 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