当前位置:网站首页>The sword refers to Offer II 097. Number of subsequences
The sword refers to Offer II 097. Number of subsequences
2022-07-29 21:32:00 【Xiao Lu wants to brush the force and deduct the question】
前言
给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数.
字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
题目数据保证答案符合 32 位带符号整数范围.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/21dk04
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
解题思路
样本对应模型
dp[i][j]
S从0…i的前缀字符串,How many options can be turned intoT的0…jThe number of schemes for this prefix string

Bottom right is the answer

dp[0][0]填1,Represents two empty strings
第一行填0;
第1列都填1
普遍位置
1)不保留i位置的字符
dp[i-1][j]
2) 一定要用的]位置
有条件,要求s[i]== t[j]的情况下,Only this possibility
dp[i][j]+=dp[i-1][j-1]
代码
class Solution {
public int numDistinct(String s, String t) {
char[] str1=s.toCharArray();
char[] str2=t.toCharArray();
int n=str1.length;
int m=str2.length;
int[][] dp=new int[n+1][m+1];
for(int i=0;i<=n;i++){
dp[i][0]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=dp[i-1][j]+(str1[i-1]==str2[j-1]?dp[i-1][j-1]:0);
}
}
return dp[n][m];
}
}
边栏推荐
- 海量数据查询方案mysql_Mysql海量数据存储和解决方案之二—-Mysql分表查询海量数据…[通俗易懂]
- MySQL数据查询 - 简单查询
- 优惠券系统设计思想
- GalNAc-siRNA甘露糖/半乳糖修饰脱氧核糖核酸|siRNA-S-S-DSPE(RNA修饰技术介绍)
- Samba server configuration (when a server is required)
- SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版
- 【无标题】
- Huawei laptop keyboard locked (how does the laptop keyboard light up)
- C# 窗体与子线程数据交互
- 无文件落地免杀的初尝试思考(上)
猜你喜欢
随机推荐
RedisJson 横空出世!
SAG1-MIC8复合DNA基因疫苗|新型脂质-HAP-DNA复合体|实验要求
uri与url的区别简单理解(uri和url有什么区别)
剑指 Offer II 097. 子序列的数目
PEG-siRNA-PCL|siRNA-PEG-LHRH|MPEG-siRNA 甲氧基聚乙二醇修饰核酸
Setinel 原理简介
ALBERT:A Lite BERT for Self-supervised Learning of Language Representations
SwiftUI * @State 相关问题
常用电源符号含义分享
用 Array.every & Array.some 匹配全部/部分内容 es6
数据可视化----网页显示温湿度
如何优雅的自定义 ThreadPoolExecutor 线程池
mos管闩锁效应理解学习
Kotlin - 协程作用域 CoroutineScope、协程构建器 CoroutineBuilder、协程作用域函数 CoroutineScope Functiom
Unity判断字符串是否可以转为float类型
Safe Browser will have these hidden features that will let you play around with your browser
JUC并发编程基础AQS
Minimal jvm source code analysis
Private domain growth | Private domain members: 15 case collections from 9 major chain industries
leetcode:952. 按公因数计算最大组件大小【并查集】









