当前位置:网站首页>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
边栏推荐
- Hongmeng OS' fourth learning
- Chemical properties and application instructions of prosci Lag3 antibody
- Material Design组件 - 使用BottomSheet展现扩展内容(二)
- Monorepo management methodology and dependency security
- Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
- Abnova丨 CD81单克隆抗体相关参数和应用
- Common view container class components
- hdu2377Bus Pass(构建更复杂的图+spfa)
- Material design component - use bottomsheet to show extended content (II)
- Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
猜你喜欢

Kubernetes resource object introduction and common commands (V) - (configmap & Secret)

Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate

leetcode:1755. 最接近目标值的子序列和

Duchefa d5124 md5a medium Chinese and English instructions

Open source SPL eliminates tens of thousands of database intermediate tables

Abnova丨DNA 标记高质量控制测试方案

解读协作型机器人的日常应用功能

Abnova total RNA Purification Kit for cultured cells Chinese and English instructions

台风来袭!建筑工地该如何防范台风!

王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
随机推荐
珍爱网微服务底层框架演进从开源组件封装到自研
Typhoon is coming! How to prevent typhoons on construction sites!
ts 之 泛型
Analysis of steam education mode under the integration of five Education
Nprogress plug-in progress bar
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
概率论机器学习的先验知识(上)
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
[Yugong series] go teaching course in July 2022 004 go code Notes
序列联配Sequence Alignment
2.8 basic knowledge of project management process
挖财商学院给的证券账户安全吗?可以开户吗?
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
表单文本框的使用(二) 输入过滤(合成事件)
Abnova blood total nucleic acid purification kit pre installed relevant instructions
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
解析创客教育的知识迁移和分享精神
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题