【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 ...
随机推荐
- C语言:自定义一个查找字串的功能函数,类似于<string.h>中的strstr()
//自定义一个字符串字串查找标准库函数strstr() #include<stdio.h> #include<string.h> char* myStrstr(char *st ...
- 一个巧妙的方法实现elementUI的table的行选中
问题背景:点击上面的框,选中下面对象的行数据 刚开始考虑使用的是table的事件:toggleRowSelection,但是发现一个奇怪的现象 <div v-if="orderData ...
- 关于vue单页面应用总是先出现主页一闪而过的现象
问题描述:每次强制刷新登陆页面时,总是会出现主页一闪而过的现象,如果主页上有请求,还会请求后台数据.感觉不太正常,所以想到研究下为什么,然后去掉这个主页一闪而过的现象 1.先看下我之前的app的rou ...
- 如何使用动画和精灵表单 Cocos2d-x 2.1.4
本文实践自 Ray Wenderlich.Tony Dahbura 的文章< How to Use Animations and Sprite Sheets in Cocos2D ...
- Cognos访问权限之让拒绝更友善
关于cognos的访问权限之前我也做了不少总结,但是由于时间关系加上用户也只要实现功能就好,我们做的效果就是像很多人一样,就那样就好了.但是有很多事情,只要你肯动脑筋,你会发现,你还可以做的更好,下面 ...
- xhEditor在线编辑器使用实例
使用xhEditor的最大好处就是不用去处理烦人的HTML标签问题,研究了一天,记录备用 前台HTML: <%@ Page Language="C#" AutoEventWi ...
- 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"
一:std:编译器错误解决 二:错误提示 "std::string::find_first_of(char const*, unsigned long, unsigned long) con ...
- YUM常用命令详解
yum是一个用于管理rpm包的后台程序,用python写成,可以非常方便的解决rpm的依赖关系.在建立好yum服务器后,yum客户端可以通过 http.ftp方式获得软件包,并使用方便的命令直接管理. ...
- Element学习
生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...
- 1z0-052 q209_2
2: View the Exhibit to examine the output produced by the following query at three different times s ...