My clear and easy DP solution defeats 81.63%


  • 0
    F
    class Solution {
    public:
        int numDistinct(string s, string t) {
            int len=t.size();
            int dp[len]={};//dp[i]means the number of subsequence of the substring in t from 0-i
            for(int i=0;i<s.size();i++){
                for(int j=t.size()-1;j>=0;j--){//because the left value may affect the right value,so go from right to left
                    if(s[i]==t[j]){
                        if(j>0) dp[j]+=dp[j-1];
                        else dp[j]+=1;
                    }
                }
            }
            return dp[len-1];
        }
    };
    

Log in to reply
 

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