Distinct Subsequences
You have been given string 'S' of length 'N' that may contain duplicate alphabets. Your task is to return the count of distinct subsequences of it.
For example:
For the given string “deed” :
The possible subsequences are {“”}, {“d”}, {“e”}, {“de”}, {“e”}, {“de”}, {“ee”}, {“dee”}, {“d”}, {“dd”}, {“ed”}, {“ded”}, {“ed”}, {“ded”}, {“eed”} and {“deed”}.
As, {“d”}, {“e”}, {“de”}, {“ed”} and {“ded”} are repeated.
The distinct subsequences are {“”}, {“d”}, {“e”}, {“de”}, {“ee”}, {“dee”}, {“dd”}, {“ed”}, {“ded”}, {“eed”} and {“deed”}
Thus, the output will be 11.
Note:
As the answer can be large, return your answer modulo 10^9 + 7.
Input format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first and only line of each test case or query contains the string 'S'.
Output format:
For each test case, print a single line containing the count of distinct subsequences modulo 10^9+7.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10 ^ 4
where 'T' is the number of test cases and 'N' is the length of the given string 'S'.
Time limit: 1 sec.
CodingNinjas
author
2y
Brute force
- The IDea is to call a helper function so as to create all possible subsequences of the string which will contain:
- String CUR = the possible subsequence of the string.
- Int ID = the index of ch...read more
CodingNinjas
author
2y
Dynamic programming
- The idea is to use dynamic programming.
- Take a ‘DP’ list which at every index stores the number of subsequences created by the given string which ends at that particular index. Mathe...read more
CodingNinjas
author
2y
Level count
- Take a variable, say ‘LEVELCOUNT’ which stores the count of subsequences ending at a particular character.
- Take another variable, say ‘ALLCOUNT’ which stores the count of total subsequences ...read more
Add answer anonymously...
Top Kellton Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Kellton Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app