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 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的字串的所有可能性的数目(不存在就是0)。
刚开始算的的时候比较暴力,所以超时了。
public class Solution {
char[] word1;
char[] word2;
int result = 0;
public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
word1 = s.toCharArray();
word2 = t.toCharArray();
for( int i = 0;i<=len1-len2;i++){
if( word1[i] == word2[0])
helper(i+1,1);
}
return result; }
public void helper(int start1,int start2){ if( start2 == word2.length ){
result++;
return ;
}
for( int i = start1;i< word1.length ;i++){
if( word1[i] == word2[start2] )
helper(i+1,start2+1);
} }
}
所以用DP算法。
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
如果当前字符相同,dp[i][j]结果等于用(dp[i-1][j-1])和(dp[i-1][j])求和
如果当前字符不同,dp[i][j] = dp[i-1][j]
public class Solution { public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
char[] word1 = s.toCharArray();
char[] word2 = t.toCharArray();
int[][] dp = new int[len1+1][len2+1]; for( int i = 0;i<=len1;i++){
for( int j = 0;j<=i && j<=len2;j++){
if( i == 0 && j != 0)
dp[i][j] = 0;
else if( j == 0)
dp[i][j] = 1;
else if( word1[i-1] == word2[j-1] )
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
else
dp[i][j] = dp[i-1][j]; }
} return dp[len1][len2];
} }
leetcode 115 Distinct Subsequences ----- java的更多相关文章
- 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 不同的子序列
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 计算不同子序列个数
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".构造如下地图("."表示空位 ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- 神奇的Noip模拟试题 T3 科技节 位运算
3 科技节 (scifest.pas/.c/.cpp) [问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那 ...
- Debugger Exception Notification
--------------------------- Debugger Exception Notification --------------------------- Project PJSP ...
- sql 行专列 列转行 普通行列转换
转载:http://www.cnblogs.com/newwind521/archive/2010/11/25/1887203.html sql 行专列 列转行 普通行列转换 /* 标题:普通行列转换 ...
- Rhel6-mailsystem配置文档
(postfix+dovecot+mysql+extmail) 理论基础:
- 图解傅里叶变换(so easy)
话不多说先上两个GIF图. 第一个动画和第二个动画其实都是对时域的周期矩形形波(近似看成矩形波,并不是严格意义的矩形方波)进行傅里叶变换分析. 对于第一个图形来说,它侧重展示变换的本质之一:叠加性,每 ...
- 爬虫再探实战(五)———爬取APP数据——超级课程表【四】——情感分析
仔细看的话,会发现之前的词频分析并没有什么卵用...文本分析真正的大哥是NLP,不过,这个坑太大,小白不大敢跳...不过还是忍不住在坑边上往下瞅瞅2333. 言归正传,今天刚了解到boson公司有py ...
- python3爬虫初探(一)之urllib.request
---恢复内容开始--- #小白一个,在此写下自己的python爬虫初步的知识.如有错误,希望谅解并指出. #欢迎和大家交流python爬虫相关的问题 #2016/6/18 #----第一把武器--- ...
- Raising Modulo Numbers_快速幂取模算法
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- Unity3D ShaderLab 模拟精灵动画
Unity3D ShaderLab 模拟精灵动画 在上一篇,介绍了通过Shader 模拟纹理运动,那么更深一步讲,我们也可以把帧动画的精灵纹理运动通过shader实现. 虽然大家都是在游戏脚本中做更高 ...
- SQL基础2
create database fuxi --创建一个名为“fuxi”的数据库go --连接语句use fuxi --使用名为“fuxi”的数据库gocreat ...