this question took me to realize, but read the instruction clearly this should be a easy problem.
class Solution {
Integer[] memo;
int MOD = 1_000_000_007;
public int countGoodStrings(int low, int high, int zero, int one) {
memo = new Integer[high + 1];
int res = 0;
for(int i = low; i <= high; i ++){
res += dp(i,zero,one);
res %= MOD;
}
return res;
}
private int dp(int l, int zero, int one){
// base case
if(l == 0) return 1; // we made to zero then we have 1 good string
if(l < zero && l < one) return 0; //this case we are not allowed to append
// solved case
if(memo[l] != null) return memo[l]; // we have done this before
// solving the problem
int appendZeros = dp(l - zero,zero,one);
int appendOnes = dp(l - one, zero, one);
return memo[l] = (appendOnes + appendZeros) % MOD;
}
}
