leetcode_question_115 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
.
Recurse:
Judge Small: Accepted!
Judge Large: Time Limit Exceeded
int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int slen = S.length();
int tlen = T.length();
if(slen <= tlen){
if(S == T) return 1;
else return 0;
} if(S[slen-1] != T[tlen-1]) return numDistinct(S.substr(0,slen-1), T);
else
return numDistinct(S.substr(0,slen-1), T) + numDistinct(S.substr(0,slen-1), T.substr(0,tlen-1));
}
dp:
Judge Small: Accepted!
Judge Large: Accepted!
int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int col = S.length() + 1;
int row = T.length() + 1;
int** dp = new int*[row];
for(int i = 0; i < row; ++i)
dp[i] = new int[col]; for(int i = 0; i < row; ++i)
dp[i][0] = 0;
for(int j = 0; j < col; ++j)
dp[0][j] = 1; for(int i = 1; i < row; ++i)
for(int j = 1; j < col; ++j)
if(T[i-1] == S[j-1]) dp[i][j] = dp[i-1][j-1] + dp[i][j-1];
else dp[i][j] = dp[i][j-1]; int tmp = dp[row-1][col-1]; for(int i = 0; i < row; ++i)
delete[] dp[i];
delete[] dp; return tmp;
}
leetcode_question_115 Distinct Subsequences的更多相关文章
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- 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
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
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 ...
- 【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
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
随机推荐
- 对TCP说三道四(三次握手)
夜朦胧,人方静,无聊的人打开了无聊的电脑看到了一张无聊的图,想着想着就睡着了,梦到了人a和人b的一次聊天. 有一天,a有事情想跟b商量就问b“有时间么,想和你聊一下天”,b想了一会发现自己能抽出时间就 ...
- 只有电信3G是公网ip。
只有电信3G是公网ip,其它网络拿到是内部网.
- Linux中service命令和/etc/init.d/的关系
Linux中service命令和/etc/init.d/的关系 service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...
- easyui的样式easyui-textbox的一个bug
easyui-testbox这个样式很恶心,用了这个就不能用传统的JQ来取值了,最近在使用上又发现了一个问题,就是赋值为0时,在输入框上会不显示,坑. <input class="ea ...
- AngularJs 实例
1.AngularJs 表单验证: 示例 .controller('signupController', ['$scope', function($scope) { $scope.submitted ...
- iOS的Ping++支付接入步骤(详细)
Ping++ SDK 代码下载地址: https://github.com/CoderLeezhen/PingppDemo 参考链接: https://www.pingxx.com/guidance/ ...
- (转载)SQL语句中Group by语句的详细介绍
转自:http://blog.163.com/yuer_d/blog/static/76761152201010203719835 SQL语句中Group by语句的详细介绍 ...
- 安卓Xpost框架
http://pcedu.pconline.com.cn/484/4841077_all.html
- floyd学习中
#include<iostream> #include<math.h> #include<stdio.h> using namespace std; //long ...
- juce中的CallbackMessage
这个类作为所有消息的基类,主要是包装了回调函数 virtual void messageCallback() = 0; /* ===================================== ...