当前位置:网站首页>LeetCode: Distinct Subsequences [115]
LeetCode: Distinct Subsequences [115]
2022-07-05 20:52:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
【 The title of 】
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
.
【 The question 】
Given string S and T, By deleting S Several characters in can get T, be T be called S The subsequence . How many deletion methods can be obtained T
【 Ideas 】
DP problem . Definition A[i][j] (i>j) Express S[1…i] Convert to T[1…j] The number of ways ( There is only one type of operation . That's from S Delete several characters from ). The conversion equation is as follows : hypothesis S[i]==T[j], A[i][j]=A[i-1][j-1]+A[i-1][j]; hypothesis S[i]!=T[j], A[i][j]=A[i-1][j]; Initialization matrix The starting point A[0][0]=1; first line A[0][i]=0; First column A[i][j]=1;
【 Code 】
class Solution {
public:
int numDistinct(string S, string T) {
if(S.length()==0 || S.length()==0)return 0;
if(S.length()<T.length())return 0; // hypothesis S==T, return 1, I think so 1 There are two ways of transformation
vector<vector<int> >matrix(S.length()+1, vector<int>(T.length()+1, 0));
// initialization matrix[0][0]
matrix[0][0]=1;
// Initialize diagonal
for(int j=1; j<=T.length(); j++)
matrix[0][j]=0;
// Initialize the first column
for(int i=1; i<=S.length(); i++)
matrix[i][0]=1;
// Consider others i>j The situation of
for(int i=1; i<=S.length(); i++){
for(int j=1; j<=T.length(); j++){
if(S[i-1]==T[j-1])matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j];
else matrix[i][j]=matrix[i-1][j];
}
}
return matrix[S.length()][T.length()];
}
};
Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117649.html Link to the original text :https://javaforall.cn
边栏推荐
- Is it safe to open an account online? Where can I get a low commission?
- ClickHouse 复制粘贴多行sql语句报错
- Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
- PHP deserialization +md5 collision
- Abnova maxpab mouse derived polyclonal antibody solution
- CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
- 教你自己训练的pytorch模型转caffe(三)
- 国外LEAD美国简称对照表
- Duchefa MS medium contains vitamin instructions
- 当Steam教育进入个性化信息技术课程
猜你喜欢
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Graph embedding learning notes
表单文本框的使用(二) 输入过滤(合成事件)
ProSci LAG3抗体的化学性质和应用说明
AI automatically generates annotation documents from code
Duchefa丨MS培养基含维生素说明书
IC popular science article: those things about Eco
台风来袭!建筑工地该如何防范台风!
leetcode:1755. 最接近目标值的子序列和
随机推荐
Which securities is better for securities account opening? Is online account opening safe?
Duchefa MS medium contains vitamin instructions
MySQL InnoDB架构原理
国外LEAD美国简称对照表
挖财商学院给的证券账户安全吗?可以开户吗?
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
Where is a good stock account? Is online account manager safe to open an account
ts 之 泛型
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
IC popular science article: those things about Eco
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
Norgen AAV提取剂盒说明书(含特色)
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Graph embedding learning notes
NPDP如何续证?操作指南来了!