Count Ways To Build Good Strings - Problem

You're a string architect tasked with building strings using a unique construction process. You start with an empty string and can repeatedly perform one of two operations:

  • Add zero consecutive '0' characters
  • Add one consecutive '1' characters

For example, if zero = 2 and one = 3, you could build strings like:

  • "00" (one operation: add 2 zeros)
  • "111" (one operation: add 3 ones)
  • "00111" (two operations: add 2 zeros, then 3 ones)
  • "11100" (two operations: add 3 ones, then 2 zeros)

A good string is one whose length falls between low and high (inclusive). Your goal is to count how many different good strings can be constructed using this process.

Since the answer can be extremely large, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: low = 3, high = 3, zero = 1, one = 1
โ€บ Output: 8
๐Ÿ’ก Note: We need strings of exactly length 3. With zero=1 and one=1, we can add one '0' or one '1' at each step. All possible strings: "000", "001", "010", "011", "100", "101", "110", "111". Total: 8 strings.
example_2.py โ€” Range Case
$ Input: low = 2, high = 3, zero = 1, one = 2
โ€บ Output: 5
๐Ÿ’ก Note: With zero=1 (add one '0') and one=2 (add two '1's), we can build: Length 2: "00", "11" (2 strings). Length 3: "000", "011", "110" (3 strings). Total: 2 + 3 = 5 strings.
example_3.py โ€” Large Numbers
$ Input: low = 1, high = 100000, zero = 1, one = 1
โ€บ Output: 215447031
๐Ÿ’ก Note: This is essentially counting binary strings of length 1 to 100000. For length n, there are 2^n strings. We sum 2^1 + 2^2 + ... + 2^100000 and take modulo 10^9+7, which equals 215447031.

Constraints

  • 1 โ‰ค low โ‰ค high โ‰ค 105
  • 1 โ‰ค zero, one โ‰ค high
  • Answer fits in 32-bit integer after taking modulo 109 + 7

Visualization

Tap to expand
String Construction with DPLength 01 wayLength 2dp[0] = 1Length 3dp[0] = 1Length 5dp[2]+dp[3] = 2+zero+oneDP Recurrence Relationdp[i] = dp[i - zero] + dp[i - one]Number of ways to reach length i = sum of ways to reach previous valid lengthsExample: zero=2, one=3dp[0] = 1 (empty string)dp[2] = dp[0] = 1 (add 2 zeros)dp[3] = dp[0] = 1 (add 3 ones)dp[5] = dp[3] + dp[2] = 2Final AnswerSum dp[i] for all i where low โ‰ค i โ‰ค highIf low=3, high=5:Result = dp[3] + dp[4] + dp[5]Don't forget: answer % (10โน + 7)
Understanding the Visualization
1
Start with empty foundation
Begin with length 0 - there's exactly 1 way to have nothing
2
Add blocks incrementally
For each length i, count ways by adding block of size 'zero' to length (i-zero) or block of size 'one' to length (i-one)
3
Build up to target length
Continue this process until we've calculated ways for all lengths up to 'high'
4
Sum the valid range
Add up all the ways for lengths between 'low' and 'high' inclusive
Key Takeaway
๐ŸŽฏ Key Insight: This problem transforms from exponential recursion to linear DP by recognizing that each length can be built by extending shorter valid lengths. The magic happens when we realize dp[i] depends only on dp[i-zero] and dp[i-one]!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium-High Frequency
~15 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