115. Distinct Subsequences

/*
115. Distinct Subsequences   
Total Accepted: 59855
Total Submissions: 198263
Difficulty: Hard
Contributors: Admin
Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

Hide Tags Dynamic Programming String

*/
public class NumDistinct {
    
    public static void main(String[] args) {
        String s = "rabbbit";
        String t = "rabbit";
        NumDistinct nd = new NumDistinct();
        System.out.println(nd.numDistinct(s,t));
    }
    
    // http://blog.csdn.net/fly_yr/article/details/50408457
    public int numDistinct(String s, String t) {
        if (s == null || s.length() == 0  || t == null || t.length() == 0) {
            return 0;
        }   
        
        int lens = s.length();
        int lent = t.length();
        int[][] dp = new int[lens + 1][lent + 1];
        dp[0][0] = 1;  // s(0, i) -> t(0) 的方法數(shù)為1
        for (int i = 1; i <= lens; i++) {
            dp[i][0] = 1;
            for (int j = 1; j <= lent; j++) {
                dp[i][j] = dp[i-1][j];
                if (s.charAt(i -1 ) == t.charAt(j -1)) {
                    dp[i][j] += dp[i-1][j-1];
                }
            }
        }
        return dp[lens][lent];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容