Let dp[i][j] be the number of number of two letter strings which satisfy the given constraints which are of length i and last character is j. if(j==0) the last character is Y , if (j==1) the last character is X;

Now base case is dp[1][0]=1 and dp[1][1]=0;

//calculating for length>1

```
for(int i=2;i<=N;i++){
dp[i][0]=dp[i][1]=0;// initializing to 0;
dp[i][0]=(dp[i-1][1]+dp[i-1][0])%mod;
//for 'Y' the previous can be anything so adding those values
for(int j=1;j<P;j++){
if(i-j<=0)break;
dp[i][1]+=(dp[i-j][0]);
if(dp[i][1]>=mod)dp[i][1]-=mod;
}
}
```

return (dp[N][0]+dp[N][1])%mod;

answered
**19 Mar '18, 14:36**

4★beginner_1111

240●1●10

accept rate:
13%