【LeetCode】114. Distinct Subsequences
Distinct Subsequences
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
.
DP,化归为二维地图的走法问题。
r a b b i t
1 0 0 0 0 0 0
r 1
a 1
b 1
b 1
b 1
i 1
t 1
设矩阵transArray,其中元素transArray[i][j]为S[0,...,i]到T[0,...,j]有多少种转换方式。
问题就转为从左上角只能走对角(匹配)或者往下(删除字符),到右下角一共有多少种走法。
transArray[i][0]初始化为1的含义是:任何长度的S,如果转换为空串,那就只有删除全部字符这1种方式。
当S[i-1]==T[j-1],说明可以从transArray[i-1][j-1]走对角到达transArray[i][j](S[i-1]匹配T[j-1]),此外还可以从transArray[i-1][j]往下到达transArray[i][j](删除S[i-1])
当S[i-1]!=T[j-1],说明只能从transArray[i-1][j]往下到达transArray[i][j](删除S[i-1])
class Solution {
public:
int numDistinct(string S, string T) {
int m = S.size();
int n = T.size(); vector<vector<int> > path(m+, vector<int>(n+, ));
for(int i = ; i < m+; i ++)
path[i][] = ; for(int i = ; i < m+; i ++)
{
for(int j = ; j < n+; j ++)
{
if(S[i-] == T[j-])
path[i][j] = path[i-][j-] + path[i-][j];
else
path[i][j] = path[i-][j];
}
} return path[m][n];
}
};
【LeetCode】114. Distinct Subsequences的更多相关文章
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Lintcode】118.Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【LeetCode】491. Increasing Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
随机推荐
- javascript和python取dict字典对象的不同
dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...
- 【BZOJ】【3611】【HEOI2014】大工程
虚树+树形DP 本题100W的点数……不用虚树真的好吗…… Orz ZYF 我的感悟: dp的过程跟SPOJ 1825 FTOUR2 的做法类似,依次枚举每个子树,从当前子树和之前的部分中各找一条最长 ...
- Objective-C:NSArray的常见操作
NSArray不可变字符串的主要操作有:创建.枚举.排序.与NSString之间的相互转换 注意: NSArray可以存对象,不可以存基本数据类型.结构体.数组.指针.nil.NULL NSArray ...
- 3D屏保: 线圈
LineFlower3DSP 一个3D屏保程序,算法的原理类似于圆内轮旋线的生成. 下载地址: http://files.cnblogs.com/WhyEngine/LineFlower3D_sp.z ...
- 线程 Timer TimerTask 计时器 定时任务 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- IDEA是如何导入项目的,及启动导入项目遇到的问题:无法加载主类的一连串问题
1.启动报错误: 找不到或无法加载主类 org.spring.springboot.Application 可能在工程下面有多个module,然后,module里面的iml配置文件不止一个,删除留主的 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- linux环境中设置jacoco覆盖率
cd /alidata1/admin/za-themis pkill -9 -f za-themis #CATALINA_HOME=/root/za-tomcat #CATALINA_BASE=/ro ...
- linux的子进程调用exec( )系列函数
exec( )函数族 : 以下我们来看看一个进程怎样来启动还有一个程序的运行.在Linux中要使用exec函数族.系统调用execve()对当前进程进行替换,替换者为一个指定的程序,其參数包含文件名称 ...
- RTT下spi flash+elm fat文件系统移植小记
背景: MCU:STM32F207 SPI flash: Winbond W25Q16BV OS: RTT V1.1.1 bsp: STM32F20x 1 将spi_core.c,spi_dev.c及 ...