-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCount Ways To Build Good Strings.cpp
69 lines (43 loc) · 1.85 KB
/
Count Ways To Build Good Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Solution by Rahul Surana
***********************************************************
Given the integers zero, one, low, and high, we can construct a string by starting with an empty string,
and then at each step perform either of the following:
Append the character '0' zero times.
Append the character '1' one times.
This can be performed any number of times.
A good string is a string constructed by the above process having a length between low and high (inclusive).
Return the number of different good strings that can be constructed satisfying these properties.
Since the answer can be large, return it modulo 109 + 7.
***********************************************************
*/
#define MOD 1000000007
class Solution {
public:
vector<int> dp;
// memonization
// int df(int x, int y, int len, int l, int h){
// if(len > h) return 0;
// if(dp[len] != -1) return dp[len];
// if(len >= l && len <= h)
// return dp[len] = (1 + df(x,y,len+x,l,h)%MOD + df(x,y,len+y,l,h)%MOD)%MOD;
// return dp[len] = (df(x,y,len+x,l,h)%MOD + df(x,y,len+y,l,h)%MOD)%MOD;
// }
int countGoodStrings(int low, int high, int zero, int one) {
dp.resize(high+1,0);
// return df(zero,one,0,low,high);
//Tabulation
for(int i = high; i >= 0; i--){
if(i < low) {
dp[i] += ((((i+zero) > high)? 0 : dp[i+zero]) +
(((i+one) > high) ? 0 : dp[i+one]))%MOD;
}
else{
dp[i] += (1 +
(((i+zero) > high)? 0 : dp[i+zero]) +
(((i+one) > high) ? 0 : dp[i+one]))%MOD;
}
}
return dp[0];
}
};