Description Link to heading

115.distinct-subsequence

Solution Link to heading

dp[i][j] denotes the occurrences of the first j characters of t in the first i characters of s:

  • if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];(uses[i - 1] and not use s[i - 1]匹配)
  • else dp[i][j] = dp[i - 1][j];

Code Link to heading

class Solution {
public:
    int numDistinct(string s, string t) {
        if (s.size() < t.size())
            return 0;
        vector<vector<uint32_t>> dp(s.size() + 1, vector<uint32_t>(t.size() + 1, 0));
        // dp[0][0] = 1;
        for (int i = 0; i <= s.size(); i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= s.size(); i++) {
            for (int j = 1; j <= i && j <= t.size(); j++) {
                if (s[i - 1] == t[j - 1])
                    dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
                else
                    dp[i][j] = dp[i - 1][j];
            }
        }
        return dp[s.size()][t.size()];
        
    }
};