2466. Count Ways To Build Good Strings
Difficulty: Medium
Topics: Dynamic Programming
Given the integers zero
, one
, low
, and high
, we can construct a string by starting with an empty string, and then at each step perform either of the following:
- Append the character
'0'
zero
times. - Append the character
'1'
one
times.
This can be performed any number of times.
A good string is a string constructed by the above process having a length between low
and high
(inclusive).
Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 109 + 7
.
Example 1:
- Input: low = 3, high = 3, zero = 1, one = 1
- Output: 8
-
Explanation:
- One possible valid good string is "011".
- It can be constructed as follows: "" -> "0" -> "01" -> "011".
- All binary strings from "000" to "111" are good strings in this example.
Example 2:
- Input: low = 2, high = 3, zero = 1, one = 2
- Output: 5
- Explanation: The good strings are "00", "11", "000", "110", and "011".
Constraints:
1 <= low <= high <= 105
1 <= zero, one <= low
Hint:
- Calculate the number of good strings with length less or equal to some constant
x
. - Apply dynamic programming using the group size of consecutive zeros and ones.
Solution:
We need to focus on constructing strings of different lengths and counting the number of valid strings that meet the given conditions. Let's break down the approach:
Problem Breakdown
State Definition:
Letdp[i]
represent the number of valid strings of lengthi
that can be constructed using the providedzero
andone
values.-
Recurrence Relation:
- From any length
i
, we can append either:-
zero
consecutive0
s, so the previous string would be of lengthi-zero
, and we would adddp[i-zero]
ways. -
one
consecutive1
s, so the previous string would be of lengthi-one
, and we would adddp[i-one]
ways.
-
- From any length
The recurrence relation becomes:
dp[i] = dp[i - zero] + dpi - one
-
Base Case:
- There is exactly one way to construct an empty string:
dp[0] = 1
.
- There is exactly one way to construct an empty string:
-
Final Computation:
- The total number of valid strings of length between
low
andhigh
is the sum ofdp[i]
for alli
fromlow
tohigh
.
- The total number of valid strings of length between
Solution Steps
- Initialize a
dp
array of sizehigh + 1
where each indexi
represents the number of valid strings of lengthi
. - Loop through each possible length
i
from 1 tohigh
, updatingdp[i]
based on the recurrence relation. - Compute the sum of
dp[i]
fromlow
tohigh
to get the final result.
Let's implement this solution in PHP: 2466. Count Ways To Build Good Strings
<?php
function countGoodStrings($low, $high, $zero, $one) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example Usage
$low = 3;
$high = 3;
$zero = 1;
$one = 1;
echo countGoodStrings($low, $high, $zero, $one); // Output: 8
$low = 2;
$high = 3;
$zero = 1;
$one = 2;
echo countGoodStrings($low, $high, $zero, $one); // Output: 5
?>
Explanation:
-
Initialization: We start by setting the base case
dp[0] = 1
, meaning that there is one way to form a string of length0
(the empty string). -
Dynamic Programming Update: For each possible string length
i
from 1 tohigh
, we check if we can append a string of lengthzero
orone
to a smaller string. We updatedp[i]
accordingly by adding the number of ways to form a string of lengthi-zero
andi-one
, ensuring that the result is taken modulo 109 + 7. -
Final Result Calculation: We sum up all values of
dp[i]
fori
in the range[low, high]
to get the final count of valid strings.
Time Complexity:
- Filling the
dp
array takesO(high)
. - Summing the valid results between
low
andhigh
takesO(high - low + 1)
.
Thus, the overall time complexity is ** O(high) **, which is efficient enough for the input limits.
Space Complexity:
- We use an array
dp
of sizehigh + 1
, so the space complexity is O(high).
This solution efficiently solves the problem within the constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks π. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)