Count Subsequences

Only 1 Testcase passed pls help

for(int i=1;i<=n;i++)
{
       dp[i]=(2*dp[i-1])%mod;
       if(previous_count[str[i-1]]!=-1)
       dp[i]=(dp[i]-dp[previous_count[str[i-1]]-1]+mod)%mod;
       previous_count[str[i-1]]=i;
 }

This is how the recurrence relation will be formed. Try this appproach.

why we do +mod in relation

We add mod so that if it is negative, it becomes positive and we again take mod then

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.