Distinct Subsequences II - Problem

Given a string s, you need to find the number of distinct non-empty subsequences that can be formed from this string.

A subsequence is formed by deleting some (possibly zero) characters from the original string while maintaining the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde", but "aec" is not.

Important: Two subsequences are considered different if they differ in at least one position. Since the answer can be extremely large, return the result modulo 109 + 7.

Example: For string "abc", the distinct subsequences are: "a", "b", "c", "ab", "ac", "bc", "abc" โ†’ Total: 7

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abc"
โ€บ Output: 7
๐Ÿ’ก Note: The distinct subsequences are: "a", "b", "c", "ab", "ac", "bc", "abc". Total count is 7.
example_2.py โ€” Duplicate Characters
$ Input: s = "aba"
โ€บ Output: 6
๐Ÿ’ก Note: The distinct subsequences are: "a", "b", "ab", "ba", "aba", "aa". Note that "a" appears only once in the count despite being formed in multiple ways.
example_3.py โ€” Single Character
$ Input: s = "aaa"
โ€บ Output: 3
๐Ÿ’ก Note: The distinct subsequences are: "a", "aa", "aaa". Even though there are many ways to form these, we count only distinct ones.

Visualization

Tap to expand
Distinct Subsequences II - Visual ProcessStep 1: Character 'a' - Create first subsequenceInput: "abc"dp = [1, 0, 0, ...] โ†’ Subsequences: {"a"}Step 2: Character 'b' - Expand collectionPrevious: {"a"} โ†’ Append 'b': {"a"+"b"} + {"b"} = {"ab", "b"}dp = [1, 2, 0, ...] โ†’ Subsequences: {"a", "b", "ab"}Step 3: Character 'c' - Final expansionPrevious: {"a", "b", "ab"} โ†’ Append 'c': {"ac", "bc", "abc"} + {"c"}dp = [1, 2, 4, 0, ...] โ†’ Total: 1+2+4 = 7 subsequencesKey Formula: new_count = sum(all previous dp values) + 1This counts all ways to extend existing subsequences + the new single characterResult: 7 distinct subsequences for "abc"
Understanding the Visualization
1
Start Empty
Begin with no subsequences, dp array all zeros
2
Process Each Character
For each character c, create new subsequences by appending c to all existing ones
3
Handle Duplicates
When character repeats, overwrite old count to avoid duplicate subsequences
4
Count Total
Sum all dp values to get total distinct subsequences
Key Takeaway
๐ŸŽฏ Key Insight: By tracking subsequences ending with each character and using the formula `new_count = sum(all_dp) + 1`, we efficiently count distinct subsequences while automatically handling duplicates through overwrites.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, with constant work per character

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Fixed size array of 26 elements for tracking characters a-z

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 2000
  • s consists of lowercase English letters only
  • Return the answer modulo 109 + 7
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen