[Leetcode][JAVA] 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.
使用动态规划算法思考的题目。因为如果知道T的子串在S以及其子串中distinct subsequences个数,那么T在S中的distinct subsequences个数也能通过它算出来。关键是如何找出转移函数。
使用题目中的例子,考虑简单的情况,S=rab,T=rab时,很明显number=1. 当S=rabb(多加一位)后,原有的number依然会有,因为rab还是能跟rab匹配。然而因为新加的字符与T的最后一个字符一样,这个时候T最后一个字符可以去匹配新加的字符,这个是在之前没有的情况,需要额外考虑。那么,多出来的distinct subsequences数就应该是排除掉T最后一个字符(匹配新字符去了)以及S中的新添加字符后的个数,即为S=rab,T=ra的情况。总的个数就是原来的number加上S=rab,T=ra情况下的个数。
如果S多加的一位不是T的最后一位字符,那么就不会有多出来的情况,这时候的distinct subsequences个数应该还是原来的个数。
创建一个二维数组,dp. dp[i][j]表示T.substring(0,i)和S.substring(0,j)的情况,0<=i<=T.length(), 0<=j<=S.length().
dp[i][j] = dp[i][j-1] //原有情况
+ dp[i-1][j-1] //如果T.charAt(i-1)==S.charAt(j-1)
起始情况dp[0][j]=1, 因为T为空字符串时匹配个数始终为1.
dp[i][j]=0如果j<i因为当T长度大于S肯定没有这样的subsequences.
代码如下:
public int numDistinct(String S, String T) {
if(T.length()>S.length())
return 0;
int[][] dp = new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++) {
for(int j=i;j<=S.length();j++) {
if(i==0)
dp[i][j]=1;
else
dp[i][j] = dp[i][j-1]+(T.charAt(i-1)==S.charAt(j-1)?dp[i-1][j-1]:0);
}
}
return dp[T.length()][S.length()];
}
因为每次循环只需要拿到上一次的dp值,所以可以对空间复杂度进一步优化:
public int numDistinct(String S, String T) {
int m = T.length();
int n = S.length();
if(m>n)
return 0;
int[] dp = new int[m+1];
dp[0] = 1;
for(int j=1;j<=n;j++)
{
for(int i=m;i>0;i--)
{
dp[i] += (T.charAt(i-1)==S.charAt(j-1))?dp[i-1]:0;
}
}
return dp[m];
}
[Leetcode][JAVA] Distinct Subsequences的更多相关文章
- 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 ----- 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 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode OJ] 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 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- Distinct Subsequences leetcode java
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
随机推荐
- @ModelAttribute 注解及 POJO入参过程
一.modelattribute注解 @ModelAttribute注解的方法有两种,一种无返回值,一种有返回值,方法的可以用@RequestParam注解来获取请求的参数,如果不获取参数,可以不用此 ...
- 解决: maven编译项目报“非法字符: \65279 ”错误
打包maven项目的时候,出现异常: [INFO] ------------------------------------------------------------------------ [ ...
- 用css伪类制作三角形的三种方法
在手机上写三角形的时候,我一般都用伪类,刚开始的时候用的图片,但是在现在的手机高清屏幕上,图片容易失真,还是用伪类吧! 第一种:一个90度的“ > ”, 只有线条.(可以做下拉框的箭头之类的) ...
- (转载)python2+selenium自动化测试系列(一)
1.Selenium2+python自动化1-环境搭建 2.Selenium2+python自动化2-pip降级selenium3.0 3.Selenium2+python自动化3-解决pip使用异常 ...
- NGUI 灰化按钮或图标
在游戏中某些地方可能需要对按钮进行灰化显示,从而表示不能点击!在网上找了一下有些方法是直接用UITexture+灰化shader去做这件事!另外有些方案写的不太清楚,看不懂!不过也基本都是要使用灰化s ...
- MFC通过txt查找文件并进行复制-备忘
MFC基于对话框的Demo txt中每行一个23位的卡号. 文件夹中包含以卡号命名的图像文件.(fpt或者bmp文件) 要求遍历文件夹,找到txt中卡号所对应的图像文件,并复制出来. VC6.0写的. ...
- Oracle 表空间
表空间 编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 表空间是数据库的逻辑划分,一个表空间只能属于一个数据库.所有的数据库对象都存放在指定的表空间中.但主要存放的是表 ...
- jdk8 Lambda表达式与匿名内部类比较
Labmda表达式与匿名内部类 前言 Java Labmda表达式的一个重要用法是简化某些匿名内部类(Anonymous Classes)的写法.实际上Lambda表达式并不仅仅是匿名内部类的语法糖, ...
- Guava学习笔记:Google Guava 类库简介
http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...
- DQL、DML、DDL、DCL的概念与区别
SQL(Structure Query Language)语言是数据库的核心语言. SQL的发展是从1974年开始的,其发展过程如下:1974年-----由Boyce和Chamberlin提出,当时称 ...