[LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
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).
Example 1:
Input: S ="rabbbit", T ="rabbit"Explanation: As shown below, there are 3 ways you can generate "rabbit" from S.
Output: 3
(The caret symbol ^ means the chosen letters)rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S ="babgbag", T ="bag"Explanation: As shown below, there are 5 ways you can generate "bag" from S.
Output: 5
(The caret symbol ^ means the chosen letters)babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
看到有关字符串的子序列或者配准类的问题,首先应该考虑的就是用动态规划 Dynamic Programming 来求解,这个应成为条件反射。而所有 DP 问题的核心就是找出状态转移方程,想这道题就是递推一个二维的 dp 数组,其中 dp[i][j] 表示s中范围是 [0, i] 的子串中能组成t中范围是 [0, j] 的子串的子序列的个数。下面我们从题目中给的例子来分析,这个二维 dp 数组应为:
Ø r a b b b i t
Ø 1 1 1 1 1 1 1 1
r 1 1
a 1 1
b 0 1 2
b 1 3
i 0 3
t 0 3
首先,若原字符串和子序列都为空时,返回1,因为空串也是空串的一个子序列。若原字符串不为空,而子序列为空,也返回1,因为空串也是任意字符串的一个子序列。而当原字符串为空,子序列不为空时,返回0,因为非空字符串不能当空字符串的子序列。理清这些,二维数组 dp 的边缘便可以初始化了,下面只要找出状态转移方程,就可以更新整个 dp 数组了。我们通过观察上面的二维数组可以发现,当更新到 dp[i][j] 时,dp[i][j] >= dp[i][j - 1] 总是成立,再进一步观察发现,当 T[i - 1] == S[j - 1] 时,dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1],若不等, dp[i][j] = dp[i][j - 1],所以,综合以上,递推式为:
dp[i][j] = dp[i][j - 1] + (T[i - 1] == S[j - 1] ? dp[i - 1][j - 1] : 0)
根据以上分析,可以写出代码如下:
class Solution {
public:
int numDistinct(string s, string t) {
int m = s.size(), n = t.size();
vector<vector<long>> dp(n + , vector<long>(m + ));
for (int j = ; j <= m; ++j) dp[][j] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
dp[i][j] = dp[i][j - ] + (t[i - ] == s[j - ] ? dp[i - ][j - ] : );
}
}
return dp[n][m];
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/115
参考资料:
https://leetcode.com/problems/distinct-subsequences/
https://leetcode.com/problems/distinct-subsequences/discuss/37327/Easy-to-understand-DP-in-Java
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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 ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 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 ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
随机推荐
- mysql-新增数据库
一.新增数据库 1.检查mysql 新增数据库之前,先检查是否安装了数据库,本次我们使用的是mysql数据库,检查是否安装mysql直接使用 mysql --version即可: 显示了mysql的版 ...
- Win10+Anaconda+tensorflow-cpu安装教程
基础概念 Python2.x or Python3.x 自从20世纪90年代初Python语言诞生至今,一直在迭代更新,根据出现的时期,可以分为Python2.x和Python3.x两个大版本.其中P ...
- 分布式中session共享的解决方案:spring-session
Session是客户端与服务器通讯会话跟踪技术,是服务器与客户端保持整个通讯的会话基本信息.客户端在第一次访问服务器的时候,服务端会响应一个sessionId并且将它存入到本地的Cookie中,在之后 ...
- Java使用路径通配符加载Resource与profiles配置使用
序言 Spring提供了一种强大的Ant模式通配符匹配,能从一个路径匹配一批资源. Ant路径通配符 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配 ...
- JavaScript ES6新特性介绍
介绍 ES6:ECMScript6 首先,一个常见的问题是,ECMAScript 和 JavaScript 到底是什么关系? ECMAScript是一个国际通过的标准化脚本语言: JavaScript ...
- 在IIS配置时没有启用目录浏览功能 :HTTP 错误 403.14
在IIS配置时没有启用目录浏览功能,浏览网站时,会出现“HTTP 错误 403.14–Forbidden,Web服务器被配置为不列出此目录内容”的提示,怎么解决这个问题呢? 01 02 03 04 0 ...
- layui + mvc + ajax 导出Excel功能
为了更方便,没基础的伙伴更容易理解,我尽量详细简便 省了很多代码,一步一步的试 自己引入文件 1. html 前端视图代码 Layui的数据绑定 全部代码 @{ Layout = null; } &l ...
- 使用EF批量新增数据十分缓慢
使用EF来批量新增数据,发现效率非常的差,几千条数据时甚至需要几分钟来执行,迫于无奈使用sql来执行了. 今天偶然看到一篇关于EF的文章,才发觉原来是自己对EF不够了解的原因. 一般新增时我们是将所有 ...
- MTSC2019大会日程重磅发布,腾讯WeTest独家Topic大揭秘!
WeTest 导读 中国移动互联网测试开发大会 Mobile Testing Summit China(简称 MTSC)是由国内最大的移动测试技术社区 TesterHome 发起的软件测试行业技术会议 ...
- Vue监控器watch的全面解析
前言 前面讲到了计算属性computed,这次讲的是监控器watch,主要任务就是监控变量的变化 正文 watch是一个对象,键是需要观察的表达式,值是对应回调函数.值也可以是方法名,或者包含选项的对 ...