【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 ...
随机推荐
- jquery的each函数的用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- html5-语义化标签(一)
1.什么是语义化标签? 根据内容的结构化(内容化),选择合适标签 2.为什么要语义化 为了在没有css样式的情况下,页面也能很好的呈现出很好的内容结构.代码结构 方便其他设备的解析(屏幕阅读器.盲人阅 ...
- [18] 螺旋楼梯(Spiral Stairs)图形的生成算法
顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...
- Linux进程调度的运行队列
当Linux内核要寻找一个新的进程在CPU上运行时,必须只考虑处于可运行状态的进程,(即在TASK_RUNNING状态的进程),因为扫描整个进程链表是相当低效的,所以引入了可运行状态进程的双向循环链表 ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 锐浪报表 导出 PDF ANSI码 乱码 问题解决
锐浪 报表 导出PDF时如果 ANSI 码 打勾了会乱码,能将这个选项默认不打勾吗 //在报表导出事件脚本里写脚本,可实现导出控制Sender.AsE2PDFOption.AnsiTextMode=0 ...
- GO语言基础之struct
结构struct 1. Go 中的struct与C中的struct非常相似,并且Go没有class 2. 使用 type <Name> struct{} 定义结构,名称遵循可见性规则(即首 ...
- Java程序调用带参数的shell脚本返回值
Java程序调用带参数的shell脚本返回值 首先来看看linux中shell变量(\(#,\)@,$0,$1,\(2)的含义解释 变量说明: - \)$ Shell本身的PID(ProcessI ...
- CIF、QCIF
分辨率: 每个像素的存储方式都是YUV QQCIF:88*72 QCIF:176*144 CIF:352*288 2CIF:704*288 DCIF:584*384 4CIF:704*576 ...
- WPF 同一个程序 只允许 同时运行一个
方法2 当程序已经运行了 再运行这个程序时,则显示当前这个窗体 http://code.3rbang.com/cshape-run-one/ VS2013附件:http://fil ...