115 Distinct Subsequences 不同子序列
给定一个字符串 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 不同子序列的更多相关文章
- [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 ...
- [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 ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
随机推荐
- centos6.5 mysql 5.6修改root密码,以及创建用户并授权
mkdir -p mysql_home/{data,temp,undologs,logs} chown -R mysql:mysql /dbfiles/mysql_home mysql_install ...
- html5--5-3 给直线添加样式
html5--5-3 给直线添加样式 学习要点 strokeStyle属性:设置颜色.渐变或模式(本节课只涉及到颜色) lineWidth属性:--设置线宽 Canvas的路径方法 moveTo() ...
- 清理html中空白符/空格/换行在行内元素中产生的间距
问题:行内元素之间产生间隔 原因:换行符,Tab制表符,空格产生间隔 解决方法: 1.行内元素写成一行 2.设置font-size为0px 把父级文本设置为0px; 再为需要显示文字的行内元素设置文字 ...
- Centos 5.x 升级 python2.7,安装setuptools、mysqldb 完整记录
最近由于有个工作任务需要搭个虚拟机环境,但是环境是搭建在内网,无法直接联网,很多软件都不能直接yum安装, 安装过程实在十分不顺利,在此留个记录给有需要的朋友. 环境是 CentOS 5.7 x6 ...
- HihoCoder 1508 : 剑刃风暴(占位)
描述 主宰尤涅若拥有一招非常厉害的招式——剑刃风暴,“无论是战士还是法师,都害怕尤涅若的武士刀剑技”. 现在战场上有N名敌对英雄,他们的位置分别为(Xi, Yi),而剑刃风暴的伤害范围是一个半径为R的 ...
- spring boot 部署 发布
Spring Boot应用的打包和部署 字数639 阅读2308 评论0 喜欢5 现在的IT开发,DevOps渐渐获得技术管理人员支持.云计算从ECS转向Docker容器技术.微服务的概念和讨论也越来 ...
- Adventure Works 教程
多维建模(Adventure Works 教程) 欢迎使用 Analysis Services 教程. 本教程通过在所有示例中使用虚构公司 Adventure Works Cycles,说明如 ...
- PowerDesigner里怎样查找特定的表
转自:https://blog.csdn.net/u013178480/article/details/78261650 crtl+f查找,在code处输入你要查找的表名,然后点击“find now& ...
- Flutter实战视频-移动电商-64.会员中心_顶部头像UI布局
64.会员中心_顶部头像UI布局 会员中心的样式 member.dart 清除原来的代码生成一个基本的结构 默认返回一个scaffold脚手架工具,body里面布局使用ListView,这样不会出现纵 ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...