当前位置:网站首页>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
边栏推荐
- 大二下个人发展小结
- 启牛2980有没有用?开户安全吗、
- ts 之 属性的修饰符public、private、protect
- 珍爱网微服务底层框架演进从开源组件封装到自研
- Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
- Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
- AI 从代码中自动生成注释文档
- 科普|英语不好对NPDP考试有影响吗 ?
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- leetcode:1139. 最大的以 1 为边界的正方形
猜你喜欢

How to make ERP inventory accounts of chemical enterprises more accurate

Duchefa low melting point agarose PPC Chinese and English instructions

Abbkine trakine F-actin Staining Kit (green fluorescence) scheme

学习机器人无从下手?带你体会当下机器人热门研究方向有哪些

ProSci LAG3抗体的化学性质和应用说明

Duchefa丨P1001植物琼脂中英文说明书

XML建模

Duchefa MS medium contains vitamin instructions

ProSci LAG-3 重组蛋白说明书

Duchefa丨MS培养基含维生素说明书
随机推荐
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
systemd-resolved 开启 debug 日志
序列联配Sequence Alignment
Material Design组件 - 使用BottomSheet展现扩展内容(二)
Hongmeng OS' fourth learning
Maker education infiltrating the transformation of maker spirit and culture
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
14、Transformer--VIT TNT BETR
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
概率论机器学习的先验知识(上)
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
Duchefa丨D5124 MD5A 培养基中英文说明书
NPDP如何续证?操作指南来了!
Composition of applet code
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Abnova DNA marker high quality control test program
Which is the best online collaboration product? Microsoft loop, notion, flowus
如何让化工企业的ERP库存账目更准确
[UE4] unrealinsight obtains the real machine performance test report