115. Distinct Subsequences

class Solution {
    public int numDistinct(String s, String t) {
        int[][] dp=new int[t.length()+1][s.length()+1];
        for(int i=0;i<=t.length();i++)
            for(int j=i;j<=s.length();j++)
            {
                if(i==0)
                    dp[i][j]=1;
                else
                {
                    dp[i][j]=dp[i][j-1];
                    if(t.charAt(i-1)==s.charAt(j-1))
                        dp[i][j]+=dp[i-1][j-1];
                }
            }
        return dp[t.length()][s.length()];
    }
}

  

posted @ 2017-09-30 04:44  Weiyu Wang  阅读(116)  评论(0编辑  收藏  举报