Given a string, find the count of distinct subsequences of it.
Input : str = "gfg" Output : 7 The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg" Input : str = "ggg" Output : 4 The four distinct subsequences are "", "g", "gg" and "ggg"
The problem of counting distinct subsequences is easy if all characters of input string are distinct. The count is equal to nC0 + nC1 + nC2 + … nCn = 2n.
How to count distinct subsequences when there can be repetition in input string?
A Simple Solution to count distinct subsequences in a string with duplicates is to generate all subsequences. For every subsequence, store it in a hash table if it doesn’t exist already. Time complexity of this solution is exponential and it requires exponential extra space.
An Efficient Solution doesn’t require generation of subsequences.
Let countSub(n) be count of subsequences of first n characters in input string. We can recursively write it as below. countSub(n) = 2*Count(n-1) - Repetition If current character, i.e., str[n-1] of str has not appeared before, then Repetition = 0 Else: Repetition = Count(m) Here m is index of previous occurrence of current character. We basically remove all counts ending with previous occurrence of current character.
How does this work?
If there are no repetitions, then count becomes double of count for n-1 because we get count(n-1) more subsequences by adding current character at the end of all subsequences possible with n-1 length.
If there repetitions, then we find count of all distinct subsequences ending with previous occurrence. This count can be obtained be recursively calling for index of previous occurrence.
Since above recurrence has overlapping subproblems, we can solve it using Dynamic Programming.
Below is C++ implementation of above idea.
Time Complexity : O(n)
Auxiliary Space : O(n)
This article is contributed by Shival Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Print all subsequences of a string
- Number of subsequences of the form a^i b^j c^k
- Number of subsequences in a string divisible by n
- Find all distinct subset (or subsequence) sums of an array
- Minimum and Maximum values of an expression with * and +
- Count All Palindromic Subsequence in a given String
- Count distinct occurrences as a subsequence
- Find minimum sum such that one of every three consecutive elements is taken
- Non-crossing lines to connect points in a circle
- Find number of times a string occurs as a subsequence in given string
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Repeating Subsequence
- Word Wrap Problem | DP-19
- Cutting a Rod | DP-13
- Longest Common Subsequence | DP-4
Improved By : s_pandey06