当前位置:网站首页>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
边栏推荐
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- 台风来袭!建筑工地该如何防范台风!
- How to renew NPDP? Here comes the operation guide!
- 解析创客教育的知识迁移和分享精神
- 概率论机器学习的先验知识(上)
- Abnova丨CRISPR SpCas9 多克隆抗体方案
- Duchefa丨低熔点琼脂糖 PPC中英文说明书
- Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
- Norgen AAV extractant box instructions (including features)
- 基于flask写一个接口
猜你喜欢

Duchefa low melting point agarose PPC Chinese and English instructions

Abnova丨血液总核酸纯化试剂盒预装相关说明书

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) 重组蛋白 中英文说明书
![[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system](/img/1d/22bf47bfa30b9bdc2e8fd348180f49.png)
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system

14、Transformer--VIT TNT BETR

2.8 basic knowledge of project management process

教你自己训练的pytorch模型转caffe(二)

Analysis of steam education mode under the integration of five Education

Abnova丨DNA 标记高质量控制测试方案
随机推荐
systemd-resolved 开启 debug 日志
mysql全面解析json/数组
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
清除app data以及获取图标
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
国外LEAD美国简称对照表
Duchefa丨MS培养基含维生素说明书
Écrire une interface basée sur flask
示波器探头对测量带宽的影响
Applet global configuration
启牛2980有没有用?开户安全吗、
Where is a good stock account? Is online account manager safe to open an account
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Monorepo management methodology and dependency security
XML建模
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
Duchefa丨D5124 MD5A 培养基中英文说明书
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
MYSQL IFNULL使用功能
MySQL InnoDB架构原理