MediumDynamic ProgrammingUpdated Sep 2026

2466. Count Ways To Build Good Strings

Asked at Visa

Difficulty

Medium

Time

O(high)

Space

O(high)

Pattern

Dynamic Programming

Companies

1

Problem

You build a binary string step by step, starting from an empty string. At each step you do exactly one of two things: append a block of zero copies of '0', or append a block of one copies of '1'. You may repeat this as many times as you like, including zero times.

A string is called good if it can be built this way and its final length is between low and high, inclusive. Return how many different good strings exist. Two strings count as different if their characters differ at any position, so the order of the blocks matters.

Because the count can be huge, return it modulo 10^9 + 7. Note that only the total length decides whether a string is good — the actual arrangement of characters is fixed by the sequence of block choices you made.

Building a string block by block

start: "" (length 0)append 1 zero → "0"append 1 one → "01"append 1 one → "011"With zero = 1 and one = 1, "011" is built in 3 stepsand has length 3, which is inside [3, 3].
Each step appends a fixed-size block, so the length grows by zero or one each time.

Constraints

  • ●1 <= low <= high <= 10^5
  • ●1 <= zero, one <= low

Examples

Example 1
Inputlow = 3, high = 3, zero = 1, one = 1
Output8
WhyEach step adds exactly one character, so any binary string of length 3 can be built. There are 2^3 = 8 such strings, from "000" to "111".
Example 2
Inputlow = 2, high = 3, zero = 1, one = 2
Output5
WhyLength 2 gives "00" (two zero-blocks) and "11" (one one-block). Length 3 gives "000", "110" and "011". That is 2 + 3 = 5 strings.

Asked At

1 companies
CompanyDifficulty
VisaMediumView all Visa questions →

How to Think About It

1

Each different sequence of append choices produces a different string, so counting strings = counting sequences of steps.

2

Key insight: dp[len] = number of strings of exactly that length. Each string of length len ends with either a block of zeros or a block of ones.

3

Recurrence: dp[len] = dp[len - zero] + dp[len - one] (when those indices are non-negative), with dp[0] = 1.

4

Sum dp[low..high] for the answer, taking the modulo throughout.

5

Walkthrough: low = high = 3, zero = one = 1: dp = [1,2,4,8] -> 8 strings.

Optimal Approach

Step 1: dp = [0] * (high + 1), dp[0] = 1.
Step 2: For i from 1 to high:
If i >= zero: dp[i] += dp[i - zero].
If i >= one: dp[i] += dp[i - one].
Take mod.
Step 3: Return sum(dp[low..high]) % MOD.

Time: O(high). Space: O(high).

dp counts strings by length

dp01231248zero = one = 1: dp[0] = 1, then dp[i] = dp[i-1] +dp[i-1], giving 1, 2, 4, 8.dp[len] = dp[len - zero] + dp[len - one]answer = sum(dp[low..high]) mod 1e9+7
dp[len] adds the two ways to reach len: a zero-block or a one-block at the end.
ComplexityTime O(high)Space O(high)We fill one dp entry per length up to high, and each entry does two constant-time lookups.

What Trips People Up in Real Interviews

1

Worrying about duplicate strings when zero == one. Blocks of zeros and blocks of ones are different characters, so sequences never collide.

2

Generating actual strings with backtracking — exponential.

3

Summing only dp[high] instead of every length in [low, high].

4

Forgetting the modulo on the final sum.

Solution Code

O(high) time
def countGoodStrings(low, high, zero, one):
    MOD = 10**9 + 7
    dp = [0] * (high + 1)
    dp[0] = 1
    for i in range(1, high + 1):
        if i >= zero:
            dp[i] += dp[i - zero]
        if i >= one:
            dp[i] += dp[i - one]
        dp[i] %= MOD
    return sum(dp[low:high + 1]) % MOD

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Count Ways To Build Good Strings problem?

Count Ways To Build Good Strings builds strings by repeatedly appending either `zero` copies of `0` or `one` copies of `1`, and asks how many distinct strings have length between `low` and `high`, modulo `10^9 + 7`. Only the length matters for counting, so it is Climbing Stairs with two custom step sizes.

How do you solve Count Ways To Build Good Strings?

The optimal approach runs in O(high) time and O(high) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Count Ways To Build Good Strings?

Count Ways To Build Good Strings is asked at Visa. It is a medium difficulty problem.

What are common mistakes on Count Ways To Build Good Strings?
  • Worrying about duplicate strings when `zero == one`. Blocks of zeros and blocks of ones are different characters, so sequences never collide.
  • Generating actual strings with backtracking — exponential.
  • Summing only `dp[high]` instead of every length in `[low, high]`.
  • Forgetting the modulo on the final sum.