Iam_Sandeep

Untitled

Dec 12th, 2021 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #User function Template for python3
  2. https://practice.geeksforgeeks.org/problems/number-of-distinct-subsequences0909/1/#
  3. class Solution:
  4.     def distinctSubsequences(self, S):
  5.         # code here
  6.         n,d=len(S),{}
  7.         dp=[0]*(n+1)
  8.         dp[0]=1
  9.         for i in range(1,n+1):
  10.             ch=S[i-1]
  11.             dp[i]=2*dp[i-1]
  12.             if ch in d:
  13.                 idx=d[ch]
  14.                 dp[i]=dp[i]-dp[idx-1]
  15.             d[S[i-1]]=i
  16.         #print(dp,d)
  17.         return dp[-1]%(10**9+7)
  18.          
  19.  
  20. #{
  21. #  Driver Code Starts
  22. #Initial Template for Python 3
  23.  
  24. if __name__ == '__main__':
  25.     T=int(input())
  26.     for i in range(T):
  27.         s = input()
  28.  
  29.         ob = Solution()
  30.         answer = ob.distinctSubsequences(s)
  31.         print(answer)
  32.  
  33. # } Driver Code Ends
Add Comment
Please, Sign In to add comment