Skip to content

Commit

Permalink
Merge pull request #1926 from ranganeeraj/patch-14
Browse files Browse the repository at this point in the history
Count Distinct SubSequences using DP
  • Loading branch information
shoaibrayeen authored Oct 2, 2023
2 parents cabb853 + 3ace28d commit b58d535
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <set>
#include <iterator>
#include <bitset>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod=1000000007;


int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) {
string s;
cin>>s;
int l[26] = {0};
int n = s.length();
ll sum[n+1]={0} ;
ll dp[n+1]={0};
dp[0] = 1;
dp[1] = 1;
sum[0] =1;
sum[1] = 2;
l[s[0]-'A'] = 1;
for(int i=2;i<=n;i++){
dp[i] = sum[i-1];
if(l[s[i-1]-'A']!=0) dp[i] = (dp[i] - sum[l[s[i-1]-'A']-1] +mod) %mod;
sum[i] = (sum[i-1] + dp[i] )%mod;
l[s[i-1]-'A'] = i;
}
ll ans =0 ;
for(int i=0;i<=n;i++){
ans = (ans + dp[i] )%mod;
}
cout<<ans;
}
return 0;
}

0 comments on commit b58d535

Please sign in to comment.