-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 13 May | 2466. Count Ways To Build Good Strings.cpp
- Loading branch information
1 parent
94bc7d2
commit bff189d
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution { | ||
int mod = 1e9+7; | ||
int solve(int target, int one, int zero, vector<int> &dp){ | ||
if(target == 0) | ||
return 1; | ||
|
||
if(target < 0) | ||
return 0; | ||
|
||
if(dp[target] != -1) return dp[target]; | ||
long long sum; | ||
sum = solve(target-one, one, zero, dp) + solve(target-zero, one, zero, dp); | ||
return dp[target] = sum%mod; | ||
} | ||
public: | ||
int countGoodStrings(int low, int high, int zero, int one) { | ||
int ans = 0; | ||
vector<int> dp(high+1, -1); | ||
|
||
for(int i = low; i <= high; i++){ | ||
ans = ((ans%mod) + solve(i, one, zero, dp))%mod; | ||
} | ||
return ans; | ||
} | ||
}; |