Share my C++ solution


  • 0
    S
    class Solution {
    public:
        int numDistinct(string s, string t) {
            int ssz = s.size();
            int tsz = t.size();
            
            int *dp = new int[tsz + 1];
            
            dp[0] = 1;
            for(int i = 1; i <= tsz; i++) {
                dp[i] = 0;
            }
            
            for(int i = 1; i <= ssz; i++) {
                for(int j = tsz; j >= 1; j--) {
                    if(s[i-1] == t[j-1]) {
                        dp[j] += dp[j-1];
                    }
                }
            }
            
            return dp[tsz];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.