Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- https://practice.geeksforgeeks.org/problems/number-of-distinct-subsequences0909/1/#
- class Solution:
- def distinctSubsequences(self, S):
- # code here
- n,d=len(S),{}
- dp=[0]*(n+1)
- dp[0]=1
- for i in range(1,n+1):
- ch=S[i-1]
- dp[i]=2*dp[i-1]
- if ch in d:
- idx=d[ch]
- dp[i]=dp[i]-dp[idx-1]
- d[S[i-1]]=i
- #print(dp,d)
- return dp[-1]%(10**9+7)
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- if __name__ == '__main__':
- T=int(input())
- for i in range(T):
- s = input()
- ob = Solution()
- answer = ob.distinctSubsequences(s)
- print(answer)
- # } Driver Code Ends
Add Comment
Please, Sign In to add comment