VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-distinct-subsequences/

⇱ Count Distinct Subsequences - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count Distinct Subsequences

Last Updated : 6 Dec, 2025

Given a string str, Find the number of distinct subsequences that can be formed from it.
A subsequence is a sequence derived from the original string by deleting zero or more characters without changing the relative order of the remaining characters.

Note: Answer can be very large, so, ouput will be answer modulo 109+7.

Examples:

Input: str = "gfg"
Output: 7
Explanation: The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg"

Input: str = "ggg"
Output: 4
Explanation: The four distinct subsequences are "", "g", "gg" and "ggg"

[Naive Approach] By Generating All Subsequences - O(2 ^ n) Time and O(2 ^ n) Space

The idea is to generate every possible subsequence of the string and store them in a HashSet to ensure uniqueness. To do this, we apply recursion on each index of the string. For every character at index i, we have two choices:

  • Include str[i]: Add the current character to the subsequence being built and move to the next index.
  • Exclude str[i]: Skip the current character and move to the next index.

The recursion stops when we reach the end of the string, meaning no more characters are left to process. At that point, we insert the generated subsequence into the HashSet.


Output
7

[Expected Approach] Using Dynamic Programming - O(n) Time and O(n) Space

The idea is to use dynamic programming (DP) to efficiently count all distinct subsequences while avoiding duplicates caused by repeating characters.

We use a DP array dp[i] where each element stores the number of distinct subsequences for the first i characters, with dp[0] = 1 for the empty subsequence. For each character, subsequences can either include or exclude it, so the total doubles. To avoid counting duplicates when a character repeats, we track its last occurrence in a last[] array and subtract the subsequences counted up to its previous occurrence. This ensures all subsequences are counted exactly once.

By iteratively updating the DP array and the last occurrence array for every character, we ensure that each subsequence is counted exactly once. At the end, dp[n] contains the total number of distinct subsequences.


Output
7

[Space Optimized] - O(n) Time and O(1) Space

In the above approach, we use an array to store the last occurrence of each character, which is further used to access value stored in array dp[], but instead of doing so we can directly store the result at last of occurrence of each character, thus we will not required an additional array to store the results.


Output
7


Comment