Distinct Subsequences leetcode java
题目:
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.
题解:
这道题首先引用我忘记在哪里看到的一句话:
“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”
所以这种类型题可以多往DP思考思考。
首先设置动态规划数组dp[i][j],表示S串中从开始位置到第i位置与T串从开始位置到底j位置匹配的子序列的个数。
如果S串为空,那么dp[0][j]都是0;
如果T串为空,那么dp[i][j]都是1,因为空串为是任何字符串的字串。
可以发现规律,dp[i][j] 至少等于 dp[i][j-1]。
当i=2,j=1时,S 为 ra,T为r,T肯定是S的子串;这时i=2,j=2时,S为ra,T为rs,T现在不是S的子串,当之前一次是子串所以现在计数为1.
同时,如果字符串S[i-1]和T[j-1](dp是从1开始计数,字符串是从0开始计数)匹配的话,dp[i][j]还要加上dp[i-1][j-1]
例如对于例子: S = "rabbbit", T = "rabbit"
当i=2,j=1时,S 为 ra,T为r,T肯定是S的子串;当i=2,j=2时,S仍为ra,T为ra,这时T也是S的子串,所以子串数在dp[2][1]基础上加dp[1][1]。
代码如下:
1 public int numDistinct(String S, String T) {
2 int[][] dp = new int[S.length() + 1][T.length() + 1];
3 dp[0][0] = 1;//initial
4
5 for(int j = 1; j <= T.length(); j++)//S is empty
6 dp[0][j] = 0;
7
8 for (int i = 1; i <= S.length(); i++)//T is empty
9 dp[i][0] = 1;
for (int i = 1; i <= S.length(); i++) {
for (int j = 1; j <= T.length(); 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[S.length()][T.length()];
}
Reference:http://blog.csdn.net/abcbc/article/details/8978146
Distinct Subsequences leetcode java的更多相关文章
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences——Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 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][JAVA] 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 ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...
随机推荐
- Kali Linux 2017.3发布了
Kali Linux 2017.3发布了 Kali Linux官方在11月21日发布Kali Linux 2017的第三个版本2017.3.这次发布变化相对不大,主要是设置面板风格发生改变,增加少量 ...
- 用C语言的rand()和srand()产生伪随机数的方法总结
标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始 ...
- UVALive 6911 Double Swords 树状数组
Double Swords 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...
- Google Reader明日关闭:14款替代品对比
北京时间6月30日上午消息,谷歌将于7月1日关闭RSS阅读器服务Google Reader,目前许多用户已转向其他的RSS阅读器服务. 美国科技博客Marketing Land本周对市面上多个RSS阅 ...
- 【Go命令教程】9. go list
go list 命令的作用是列出指定的 代码包 的信息.与其他命令相同,我们需要以 代码包导入路径 的方式给定代码包.被给定的代码包可以有多个.这些代码包对应的目录中必须直接保存有 Go 语言源码文件 ...
- delphi 游戏
http://www.cnblogs.com/devlyn/archive/2010/08/24/1807190.html
- 监测uitableview 向上滑动和向下滑动的事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat height = _varietyTableView.frame.si ...
- PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上-投影机 设置
无论是老师或是讲师还是即将要演讲的人,在讲课之前一定会做好课件,到哪一页该讲哪些内容,到哪里该如何去讲等等.那么一般的讲师会将这些课件存放到哪里呢?是用个书本记载下来呢,还是直接存放到电脑上呢?其实本 ...
- springboot实现服务器端消息推送(websocket + sockjs + stomp)
服务器端推送技术在web开发中比较常用,可能早期很多人的解决方案是采用ajax向服务器轮询消息,这种方式的轮询频率不好控制,所以大大增加了服务器的压力,后来有了下面的方案:当客户端向服务器发送请求时, ...
- 架构:The Onion Architecture : part 2(洋葱架构:第二篇)(转载)
原位地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-2/. In part 1, I introduced an archi ...