[LeetCode] 940. Distinct Subsequences II Two of different subsequences


Given a string S, count the number of distinct, non-empty subsequences of S .

Since the result may be large, return the answer modulo 10^9 + 7.

Example 1:

Input: "abc"
Output: 7
Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".

Example 2:

Input: "aba"
Output: 6 Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba".

Example 3:

Input: "aaa"
Output: 3 Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".

Note:

  1. S contains only lowercase letters.
  2. 1 <= S.length <= 2000

This question is similar to the previous one [Distinct Subsequences] (http://www.cnblogs.com/grandyang/p/4294105.html), there is only one string here, let us find all the different subsequences, if There are no repeated characters in the string, and the number of subsequences can be directly obtained, but the existence of repeated characters here greatly increases the difficulty. Because the result suggests that the result may be very large, to take the excess of a super large number is equivalent to explicitly saying that dynamic programming should be used to do it. Next, let us consider the definition of the dp array and the derivation of the state transfer equation. At the beginning, bloggers also considered using a one-dimensional array dp, where dp [i] represents the number of different subsequences ending in S [i], just like [this post] (https://leetcode.com/problems/ distinct-subsequences-ii / discuss / 192030 / Java-DP-O \ (N2 \)-time-greater-O \ (N \)-time-greater-O \ (1 \)-space) as defined in, But the state transfer equation is not easy to deduce, although the code of that post can run through, but the explanation is not. Here is still based on [lee215 Great God's post] (https://leetcode.com/problems/distinct-subsequences-ii/discuss/192017/C%2B%2B \ (1 \)-Space) to explain. Here we use a one-dimensional array dp of size 26, where dp [i] means the character i + 'a' The number of different subsequences at the end is limited to 26 because there are only lowercase letters in the title. Take the example of aba to analyze. When encountering a at the beginning, then there is only one subsequence ending in a, which is a. When encountering the middle b, we know that there are 2 subsequences ending in b at this time. They are b and ab, respectively. How are they obtained? In fact, the empty string and a are respectively added with a b. At this time, the value obtained seems to be equal to the result calculated by sum (dp) +1, and then verify it This success does not hold. When encountering a at the end, then there are four subsequences ending in a at this time, namely a, aa, ba, aba, how did they get it? Before this a is added, all the current subsequences are, a, b, ab, if you count an empty string, [], a, b, ab, then add a b after each, you can get the result It seems that it also conforms to the law of sum (dp) +1, which is actually not difficult to understand, because on the basis of the current different sequences, adding any one character will get another different subsequence, followed by adding 1 is In order to add the empty string, this is the state transition equation. The final result is to accumulate the dp array and return it. The code is as follows:
class Solution {
public:
    int distinctSubseqII(string S) {
        int M = 1e9 + 7;
        vector<int> dp(26);
        for (char c : S) {
            dp[c - 'a'] = accumulate(dp.begin(), dp.end(), 1L) % M;
        }
        return accumulate(dp.begin(), dp.end(), 0L) % M;
    }
};

Github sync address:

https://github.com/grandyang/leetcode/issues/940


Similar topics:

Distinct Subsequences


Reference materials:

https://leetcode.com/problems/distinct-subsequences-ii/

https://leetcode.com/problems/distinct-subsequences-ii/discuss/192017/C%2B%2BJavaPython-4-lines-O(N)-Time-O(1)-Space

https://leetcode.com/problems/distinct-subsequences-ii/discuss/192030/Java-DP-O(N2)-time-greater-O(N)-time-greater-O(1)-space


[LeetCode All in One topic explanation summary (continuously updated ...)] (https://www.cnblogs.com/grandyang/p/4606334.html)

Guess you like

Origin www.cnblogs.com/grandyang/p/12735600.html