Skip to content

Commit 0e48212

Browse files
committed
feat: add solutions to lc problem: No.2466
1 parent a336f65 commit 0e48212

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

solution/2400-2499/2466.Count Ways To Build Good Strings/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int {
196196

197197
<!-- solution:end -->
198198

199+
<!-- solution:start -->
200+
201+
### Solution 2: Dynamic programming
202+
203+
<!-- tabs:start -->
204+
205+
#### TypeScript
206+
207+
```ts
208+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
209+
const mod = 10 ** 9 + 7;
210+
const f: number[] = new Array(high + 1).fill(0);
211+
f[0] = 1;
212+
213+
for (let i = 1; i <= high; i++) {
214+
if (i >= zero) f[i] += f[i - zero];
215+
if (i >= one) f[i] += f[i - one];
216+
f[i] %= mod;
217+
}
218+
219+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
220+
221+
return ans % mod;
222+
}
223+
```
224+
225+
#### JavaScript
226+
227+
```js
228+
function countGoodStrings(low, high, zero, one) {
229+
const mod = 10 ** 9 + 7;
230+
const f[] = new Array(high + 1).fill(0);
231+
f[0] = 1;
232+
233+
for (let i = 1; i <= high; i++) {
234+
if (i >= zero) f[i] += f[i - zero];
235+
if (i >= one) f[i] += f[i - one];
236+
f[i] %= mod;
237+
}
238+
239+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
240+
241+
return ans % mod;
242+
}
243+
```
244+
245+
<!-- tabs:end -->
246+
247+
<!-- solution:end -->
248+
199249
<!-- problem:end -->

solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md

+53-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
3939
<strong>Output:</strong> 8
40-
<strong>Explanation:</strong>
41-
One possible valid good string is &quot;011&quot;.
42-
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
40+
<strong>Explanation:</strong>
41+
One possible valid good string is &quot;011&quot;.
42+
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
4343
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
4444
</pre>
4545

@@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int {
196196

197197
<!-- solution:end -->
198198

199+
<!-- solution:start -->
200+
201+
### Solution 2: Dynamic programming
202+
203+
<!-- tabs:start -->
204+
205+
#### TypeScript
206+
207+
```ts
208+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
209+
const mod = 10 ** 9 + 7;
210+
const f: number[] = new Array(high + 1).fill(0);
211+
f[0] = 1;
212+
213+
for (let i = 1; i <= high; i++) {
214+
if (i >= zero) f[i] += f[i - zero];
215+
if (i >= one) f[i] += f[i - one];
216+
f[i] %= mod;
217+
}
218+
219+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
220+
221+
return ans % mod;
222+
}
223+
```
224+
225+
#### JavaScript
226+
227+
```js
228+
function countGoodStrings(low, high, zero, one) {
229+
const mod = 10 ** 9 + 7;
230+
const f[] = new Array(high + 1).fill(0);
231+
f[0] = 1;
232+
233+
for (let i = 1; i <= high; i++) {
234+
if (i >= zero) f[i] += f[i - zero];
235+
if (i >= one) f[i] += f[i - one];
236+
f[i] %= mod;
237+
}
238+
239+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
240+
241+
return ans % mod;
242+
}
243+
```
244+
245+
<!-- tabs:end -->
246+
247+
<!-- solution:end -->
248+
199249
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countGoodStrings(low, high, zero, one) {
2+
const mod = 10 ** 9 + 7;
3+
const f[] = new Array(high + 1).fill(0);
4+
f[0] = 1;
5+
6+
for (let i = 1; i <= high; i++) {
7+
if (i >= zero) f[i] += f[i - zero];
8+
if (i >= one) f[i] += f[i - one];
9+
f[i] %= mod;
10+
}
11+
12+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
13+
14+
return ans % mod;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
2+
const mod = 10 ** 9 + 7;
3+
const f: number[] = new Array(high + 1).fill(0);
4+
f[0] = 1;
5+
6+
for (let i = 1; i <= high; i++) {
7+
if (i >= zero) f[i] += f[i - zero];
8+
if (i >= one) f[i] += f[i - one];
9+
f[i] %= mod;
10+
}
11+
12+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
13+
14+
return ans % mod;
15+
}

0 commit comments

Comments
 (0)