题目

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.

题解

这道题首先引用我忘记在哪里看到的一句话:

“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”

所以这种类型题可以多往DP思考思考。

首先设置动态规划数组dp[i][j],表示S串中从开始位置到第i位置与T串从开始位置到底j位置匹配的子序列的个数。

如果S串为空,那么dp[0][j]都是0;

如果T串为空,那么dp[i][j]都是1,因为空串为是任何字符串的字串。

可以发现规律,dp[i][j] 至少等于 dp[i][j-1]。

当i=2,j=1时,S 为 ra,T为r,T肯定是S的子串;这时i=2,j=2时,S为ra,T为rs,T现在不是S的子串,当之前一次是子串所以现在计数为1.

同时,如果字符串S[i-1]和T[j-1](dp是从1开始计数,字符串是从0开始计数)匹配的话,dp[i][j]还要加上dp[i-1][j-1]

例如对于例子: S = "rabbbit", T = "rabbit"

当i=2,j=1时,S 为 ra,T为r,T肯定是S的子串;当i=2,j=2时,S仍为ra,T为ra,这时T也是S的子串,所以子串数在dp[2][1]基础上加dp[1][1]。

代码如下:

 1     public int numDistinct(String S, String T) {
 2         int[][] dp = new int[S.length() + 1][T.length() + 1];
 3         dp[0][0] = 1;//initial
 4         
 5         for(int j = 1; j <= T.length(); j++)//S is empty
 6             dp[0][j] = 0;
 7             
 8         for (int i = 1; i <= S.length(); i++)//T is empty
 9             dp[i][0] = 1;
            
         for (int i = 1; i <= S.length(); i++) {
             for (int j = 1; j <= T.length(); 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[S.length()][T.length()];
     }

Reference:http://blog.csdn.net/abcbc/article/details/8978146

Distinct Subsequences leetcode java的更多相关文章

  1. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  2. Distinct Subsequences——Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. leetcode 115 Distinct Subsequences ----- java

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  9. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

随机推荐

  1. Wannafly挑战赛21A

    题目链接 Wannafly挑战赛21A 题解 代码 #include <cstdio> #include <cmath> #define MAX 1000005 #define ...

  2. BZOJ.1028.[JSOI2007]麻将(贪心)

    题目链接 枚举对子,枚举每张牌,先出完它的刻子,剩下的出顺子.\(O(n^3)\). 不是这样 -> 出完所有刻子,最后出顺子.(日常zz) 优先仨相同的,然后顺子,有一次且一定要用一次机会补顺 ...

  3. BZOJ5020 [THUWC 2017]在美妙的数学王国中畅游LCT

    题意很明显是要用LCT来维护森林 难点在于如何处理函数之间的关系 我们可以根据题目给的提示关于泰勒展开的式子 将三种函数变成泰勒展开的形式 因为$x∈[0,1]$ 所以我们可以将三个函数在$x_0=0 ...

  4. linux 下安装 RZ SZ命令 以及使用

    对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,rz是把win的文件上传到linux上    sz是吧 ...

  5. Redis如何处理客户端连接

    本文主要介绍了 Redis 处理客户端连接的一些内部实现机制,包括连接处理.超时.缓冲区等一系列内容. 注:本文所述内容基于 Redis2.6 及以上版本. 连接的建立 Redis 通过监听一个 TC ...

  6. Windows Sysinternals实战指南

    http://www.epubit.com.cn/book/details/4786 Mark Russinovich是Microsoft Azure首席技术官,主要负责微软云计算平台的技术战略和架构 ...

  7. MySQL是如何利用索引的

    http://fordba.com/spend-10-min-to-understand-how-mysql-use-index.html

  8. discuz修改太阳,月亮,星星等级图标

    想必大家都想修改一下默认的等级图标吧,刚才在论坛上看见很多大神的方法都是要修改文件的,不过为了安全起见需要事先备份好才改,这种方法是可行的,但可能有些新手站长不会修改,又或者改错了恢复不来,现在我教大 ...

  9. 两次被百度k站两次恢复的亲身经历

    最后,网站被k了并不是最重要的,最重要的是你能不能找到网站被k的病因所在,不知道病因的话,就盲目的优化,恢复起来可能效果就不太明显,也比较浪费时间.只有找到了被k的原因,这样恢复起来才会得心应手,省时 ...

  10. delphi 启动停止windows服务 转

    http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...