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 ...
随机推荐
- curl dns缓存设置
CURLOPT_DNS_USE_GLOBAL_CACHE 启用时会启用一个全局的DNS缓存,此项为线程安全的,并且默认启用.CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信 ...
- hadoop 视频教程2
Hadoop大数据零基础实战培训教程 一,教程内容: 1,Hadoop2.0YARN深入浅出系列 2,Avro数据序列化系统 3,Chukwa集群监控系统 4,Flume日志收集系统 5,Greenp ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set
F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...
- MySQL是如何利用索引的
http://fordba.com/spend-10-min-to-understand-how-mysql-use-index.html
- 什么时候用var关键字
C#关键字是伴随这.NET 3.5以后,伴随着匿名函数.LINQ而来, 由编译器帮我们推断具体的类型.总体来说,当一个变量是局部变量(不包括类级别的变量),并且在声明的时候初始化,是使用var关键字的 ...
- VS2017下Git的使用
一.使用GIT上的项目 (1)找到项目的git地址 (2)打开 vs2017的团队资源管理器面板,直接克隆(复制)远程Github上的项目 (3)追加新项目,到以上工程中. 新建项目时,把项目创建在步 ...
- VisualStudio:如何监控 ADO.NET?
背景 很多场景下我们都需要监控 ADO.NET,如:查看某些框架(ORM)生成的 SQL.如何在不能使用 SQL Profile 的情况下监控 SQL 呢?VS 为我们提供了一个工具,本文做一些介绍! ...
- ibatis.net:第七天,QueryWithRowDelegate
xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...
- 再谈Linux内核中的RCU机制
转自:http://blog.chinaunix.net/uid-23769728-id-3080134.html RCU的设计思想比较明确,通过新老指针替换的方式来实现免锁方式的共享保护.但是具体到 ...
- JavaScript中 location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...