940. Distinct Subsequences II
Description
Given a string s, return the number of distinct non-empty subsequences of s. Since the answer may be very large, return it modulo 109 + 7.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not.
Example 1:
Input: s = "abc" Output: 7 Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
Example 2:
Input: s = "aba" Output: 6 Explanation: The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
Example 3:
Input: s = "aaa" Output: 3 Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".
Constraints:
1 <= s.length <= 2000sconsists of lowercase English letters.
Solutions
Solution 1: Dynamic Programming
Thinking
Count distinct nonempty subsequences. \(n\le 2000\), so listing subsets is impossible. Classify by last letter: \(f[c]\) is the number of distinct subsequences now ending with \(c\). On reading \(c\), it may follow any previous subsequence or stand alone, so \(f[c]\leftarrow \sum f+1\); a repeated \(c\) overwrites the old family that ended with \(c\).
We define \(f[i]\) as the number of distinct subsequences ending with the \(i\)-th lowercase letter. Initially, all elements in \(f\) are \(0\).
Traverse the string \(s\). For the current character \(c\), update \(f[c]\) to \(\sum_{i=0}^{25} f[i] + 1\). Here \(\sum_{i=0}^{25} f[i]\) is the number of distinct subsequences obtained so far, and \(+1\) means the character \(c\) itself can also be a subsequence.
Finally, the answer is \(\sum_{i=0}^{25} f[i]\) modulo \(10^9 + 7\).
The time complexity is \(O(n \times C)\), and the space complexity is \(O(C)\), where \(n\) is the length of \(s\) and \(C\) is the size of the character set. In this problem, \(C = 26\).
1 2 3 4 5 6 7 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Solution 2: Optimized Dynamic Programming
Thinking
Method 1 rescans \(26\) cells on every update. Keep the running sum \(\textit{ans}\); the increment is \(\textit{ans}-f[i]+1\), so both \(f[i]\) and \(\textit{ans}\) update in \(O(1)\).
Based on Solution 1, we can maintain a variable \(\textit{ans}\) as the sum of all elements in \(f\). Each time we update \(f[i]\), the number of newly added distinct subsequences is \(\textit{ans} - f[i] + 1\). We then update both \(\textit{ans}\) and \(f[i]\) accordingly.
The time complexity is \(O(n)\), and the space complexity is \(O(C)\).
Similar problems:
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |