题目

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. 一个页面从输入URL 到页面加载显示完成,这个过程中都发生了什么?

    1.当发送一个URL请求时,浏览器会开启一个线程来处理这个请求,同时在远程DNS服务器上启动一个DNS查询,解析获取网址的IP地址:2.浏览器与远程Web服务器通过TCP三次握手协商来建立一个TCP/ ...

  2. Top 10 Revit Architecture 2014 books

    Revit Architecture, along with ArchiCAD, is most used BIM software in architectural design. Although ...

  3. 2018-2019-20172329 《Java软件结构与数据结构》第五周学习总结

    2018-2019-20172329 <Java软件结构与数据结构>第五周学习总结 教材学习内容总结 <Java软件结构与数据结构>第九章-排序与查找 一.查找 1.查找概念简 ...

  4. HICON泄漏

    通常,我们使用的HICON对象只需用DestroyIcon后就不存在内存泄漏了,但是当我们使用GetIconInfo后会发现程序GDI资源存在泄漏,原因是GetIconInfo会产生2个HBITMAP ...

  5. Educational Codeforces Round 14 B. s-palindrome 水题

    B. s-palindrome 题目连接: http://www.codeforces.com/contest/691/problem/B Description Let's call a strin ...

  6. SOC 与 ARM

    SOC是指片上系统,意思是一个芯片就构成一个包括了存储.CPU.甚至还有AD.UART等等其他资源的系统!而ARM只是CPU的一种,有的片上系统是51.nios.PIC.等等不一而是!特别是nios, ...

  7. 使用Axure RP原型设计实践06,登录验证

    登录验证主要功能包括: ● 用户名错误,提示无效用户名,用户名和密码文本框清空● 用户名存在,密码错误,提示密码错误,密码清空,焦点进入密码框● 用户名和密码都正确,验证通过 本篇接着"使用 ...

  8. Visual Studio 2013 sqlce 配置(转)

    Visual Studio 2013 把內建 SQL CE 的管理工具拿掉了 下载SQL Server Compact Toolbox by ErikEJ并安装 打开VS2013,新建一工程,在“视图 ...

  9. GIS中的数据库.gdb与.mdb的区别

    gdb是文件地理数据库,mdb是个人地理数据库,都是数据库文件类型. 个人地理数据库,是以access数据库为基础的个人将数据库格式mdb,可以存储不超过2G的文件,只适合Windows系统下: 文件 ...

  10. YUI-compressor 在Linux下安装和使用

    介绍一个非常流行的javascript压缩工具YUI compressor,可以提供更好的压缩效率:该工具由著名的Yahoo Exceptional Performance项目组出品. JSMin非常 ...