Skip to content

Commit 67b0466

Browse files
committed
[LeetCode Sync] Runtime - 119 ms (66.46%), Memory - 45.7 MB (57.64%)
1 parent 37060b6 commit 67b0466

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
2+
3+
<ul>
4+
<li>Append the character <code>&#39;0&#39;</code> <code>zero</code> times.</li>
5+
<li>Append the character <code>&#39;1&#39;</code> <code>one</code> times.</li>
6+
</ul>
7+
8+
<p>This can be performed any number of times.</p>
9+
10+
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
11+
12+
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
19+
<strong>Output:</strong> 8
20+
<strong>Explanation:</strong>
21+
One possible valid good string is &quot;011&quot;.
22+
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
23+
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
30+
<strong>Output:</strong> 5
31+
<strong>Explanation:</strong> The good strings are &quot;00&quot;, &quot;11&quot;, &quot;000&quot;, &quot;110&quot;, and &quot;011&quot;.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= low&nbsp;&lt;= high&nbsp;&lt;= 10<sup>5</sup></code></li>
39+
<li><code>1 &lt;= zero, one &lt;= low</code></li>
40+
</ul>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
3+
# Use dp[i] to record to number of good strings of length i.
4+
dp = [1] + [-1] * (high)
5+
mod = 10 ** 9 + 7
6+
7+
# Find the number of good strings of length `end`.
8+
def dfs(end):
9+
if dp[end] != -1:
10+
return dp[end]
11+
count = 0
12+
if end >= zero:
13+
count += dfs(end - zero)
14+
if end >= one:
15+
count += dfs(end - one)
16+
dp[end] = count % mod
17+
return dp[end]
18+
19+
20+
# Add up the number of strings with each valid length [low ~ high].
21+
return sum(dfs(end) for end in range(low, high + 1)) % mod

0 commit comments

Comments
 (0)