Skip to content

Commit 8ac13a6

Browse files
authored
Create 2466. Count Ways To Build Good Strings1 (#676)
2 parents 5922bd2 + 3c50354 commit 8ac13a6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int countGoodStrings(int low, int high, int zero, int one) {
4+
const int MOD = 1e9 + 7; // Modulo to prevent overflow
5+
vector<int> dp(high + 1, 0); // DP array to store the number of ways to create strings of length i
6+
dp[0] = 1; // Base case: One way to create an empty string
7+
int result = 0; // To store the total number of good strings
8+
9+
// Fill the DP array
10+
for (int i = 1; i <= high; ++i) {
11+
if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % MOD;
12+
if (i >= one) dp[i] = (dp[i] + dp[i - one]) % MOD;
13+
if (i >= low) result = (result + dp[i]) % MOD;
14+
}
15+
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)