给定一个字符串 S 和一个字符串 T,求 S 的不同的子序列中 T 出现的个数。
一个字符串的一个子序列是指:通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(譬如,"ACE"是"ABCDE" 的一个子序列,而 "AEC" 不是)
下面是一个例子:
S = "rabbbit", T = "rabbit"
返回 3.
详见:https://leetcode.com/problems/distinct-subsequences/description/

Java实现:

class Solution {
public int numDistinct(String s, String t) {
int m=s.length();
int n=t.length();
//dp[i][j]表示S串中从开始位置到第i位置与T串从开始位置到底j位置匹配的子序列的个数
int[][] dp = new int[m + 1][n + 1];
dp[0][0] = 1;//initial //如果S串为空,那么dp[0][j]都是0
for(int j = 1; j <= n; j++){//s is empty
dp[0][j] = 0;
} //如果T串为空,那么dp[i][j]都是1
for (int i = 1; i <= m; i++){//t is empty
dp[i][0] = 1;
} for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; 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[m][n];
}
}

参考:https://www.cnblogs.com/springfor/p/3896152.html

115 Distinct Subsequences 不同子序列的更多相关文章

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

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  2. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  3. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  4. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  5. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

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

  6. 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 ...

  7. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  8. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 115. Distinct Subsequences (String; DP)

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

随机推荐

  1. html5--5-8 绘制圆/弧

    html5--5-8 绘制圆/弧 学习要点 掌握绘制圆弧的方法 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y,w,h) 绘制矩形(无填充) fil ...

  2. 运行swoole_server方法

    运行 php 文件 server.php 运行结果是如下: 只是服务器开启了 如果想看客户端连接的情况 可以测试一下 从新连接一个连接 用命令 方式 telnet 127.0.0.1 9501 这个9 ...

  3. ab-如何提交post请求

    ab -n 1 -c 1 -p post.txt http://***********/ 文件post.txt中存放要post的数据

  4. MongoDB复制集成员及架构介绍(一)

    MongoDB复制集介绍 MongoDB支持在多个机器中通过异步复制达到提供了冗余,增加了数据的可用性.MongoDB有两种类型的复制,第一种是同于MySQL的主从复制模式(MongoDB已不再推荐此 ...

  5. bzoj 4827 [Hnoi2017] 礼物 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 首先,旋转对应,可以把 b 序列扩展成2倍,则 a 序列对应到的还是一段区间: 再把 ...

  6. button的FlatStyle和FlatAppearance属性

    FlatStyle是处理边框的样式,而FlatAppearance是用来设置边框的颜色,宽度和鼠标移动和点击时的效果设置FlatStyle为Flat,并且设置FlatAppearance下的Borde ...

  7. zookeeper分布式安装

    1.先把zookeeper-3.5.2-alpha.tar上传解压到/usr/local/下重命名为zookeeper ############# zookeeper env ############ ...

  8. js的一些刷新功能

    <a href="javascript:location.replace(location.href);" title="刷新"> </a&g ...

  9. 【Linux学习】Linux文件系统1--文件系统的目录结构

    Linux文件系统1--文件系统的目录结构 一.linux文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 1.普通文件:如文本文件. ...

  10. SqlServer2012——表

    1.数据类型 数字类型:int,smallint, 高精度:decimal,numeric 日期和时间:datetime,smalldatetime 二进制:binary,varbinary,imag ...