diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md b/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md index 88b8698471c7d..a4d50c1873929 100644 --- a/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md @@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int { + + +### Solution 2: Dynamic programming + + + +#### TypeScript + +```ts +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +#### JavaScript + +```js +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + + + + + diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md b/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md index 00e065265953c..00eff483537a1 100644 --- a/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md @@ -37,9 +37,9 @@ tags:
 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". 
+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.
 
@@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int { + + +### Solution 2: Dynamic programming + + + +#### TypeScript + +```ts +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +#### JavaScript + +```js +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + + + + + diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js new file mode 100644 index 0000000000000..6ef6105beca47 --- /dev/null +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js @@ -0,0 +1,15 @@ +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts new file mode 100644 index 0000000000000..fea7b06e217eb --- /dev/null +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts @@ -0,0 +1,15 @@ +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +}