当前位置:网站首页>940. Different subsequences II
940. Different subsequences II
2022-07-29 21:39:00 【Xiao Lu wants to brush the force and deduct the question】
前言
给定一个字符串 s,计算 s 的 different non-empty subsequences 的个数.因为结果可能很大,So returning an answer needs to be right 10^9 + 7 取余 .
字符串的 子序列 is to remove some via the original string(也可能不删除)characters without changing the relative positions of the remaining characters.
例如,“ace” 是 “abcde” 的一个子序列,但 “aec” 不是
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/distinct-subsequences-ii
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
解题思路
when there are no repeating characters

The new substring is{1}
2到来,新加{2},{1,2}
3到来,Quantity is before2倍
{3},{1,3},{2,3},{1,2,3}
when there are repeated characters


第二个1到来,重复了{1},Therefore, the number of new additions is 1

第三个1来了,重复了2个

因此可以推出,Suppose the number of letters that appear before is X=100;
Then the number of the next occurrence of the same letter is
all=perAll(上一个位置的all)+newAll(The number of new letters if not repeated)-X;
用一个数组存储26The number of strings ending in letters,
all加上之前的all,If the same letter appears,Just subtract the number of the same letter in the array

来到b,all=2
空串和{b}
来到第一个c,newAll=2,all=4,记录c:2
来到第2个c,newAll=4,all=8
因为c有记录,因此all要减2,
代码
class Solution {
public int distinctSubseqII(String s) {
if (s == null || s.length() == 0) {
return 0;
}
char[] str=s.toCharArray();
int all=1;
int mod=1000000007;
int[] map=new int[26];
for(int i=0;i<str.length;i++){
int newAll=all;
int curAll=(all+newAll)%mod;
all=(curAll-map[str[i]-'a']+mod)%mod;
map[str[i]-'a']=newAll;
}
return all-1;
}
}
边栏推荐
- LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
- WeChat Mini Program 30 Customizing Templates and Obtaining User Login Credentials
- 叶酸&适配体修饰DNA纳米载体|CdS纳米颗粒修饰DNA|科研试剂
- VR直播营销需求增加,数据模块为我们铺路
- SwiftUI * @State 相关问题
- conda虚拟环境 | install 与 list 问题
- 三品牌下半年将带来多款新品,东风日产将迎来“产品大潮”
- 从实例学Kettle(一):获取股票行情数据
- 荧光量子点修饰siRNA-QDs|纳米金修饰siRNA-Au(RNA修饰方式方法)
- GalNAc-siRNA甘露糖/半乳糖修饰脱氧核糖核酸|siRNA-S-S-DSPE(RNA修饰技术介绍)
猜你喜欢

QT安装、创建项目与调试,在VS中的使用:手把手教程

.NET 6.0中使用Identity框架实现JWT身份认证与授权

GET_ENTITYSET Method Implementation Guide for SAP ABAP OData Service Data Provider Class

SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版

干货!联邦学习中的合作均衡

ALBERT: A Lite BERT for Self-supervised Learning of Language Representations

全景教程丨VR全景拍摄如何拍摄日出和日落的场景?

R语言对airbnb数据nlp文本挖掘、地理、词云可视化、回归GAM模型、交叉验证分析

The younger brother asked: Is the work of a programmer a day’s work of code?

GalNAc-siRNA甘露糖/半乳糖修饰脱氧核糖核酸|siRNA-S-S-DSPE(RNA修饰技术介绍)
随机推荐
Cobaltstrike和BurpSuite桌面快捷配置
Related phrases include usage and collocation (include)
打破原则!MongoDB 引入 SQL?
错误解决:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]
全景教程丨VR全景拍摄如何拍摄日出和日落的场景?
MySQL - Design game user information table
模型推理模板
SAG1-MIC8复合DNA基因疫苗|新型脂质-HAP-DNA复合体|实验要求
Mass data query scheme mysql_Mysql massive data storage and solution 2 - Mysql sub-table query massive data... [easy to understand]
Permutations of a small feat: cantor
核壳二氧化钛纳米颗粒修饰DNA|二氢杨梅素修饰DNA药物|相关介绍
解析掌握现代化少儿编程实操能力
太卷了,企业级的智慧物业系统,也完全开源....
高通WLAN框架学习(31)-- Power save
全自动化机器学习建模!效果吊打初级炼丹师!
378. The Kth Smallest Element in an Ordered Matrix
Unity determines whether a string can be converted to float type
RNA的化学修饰原理|Gal-PEG-siRNA|siRNA-S-S-DSPE|siRNA-s-s-PEG|cholesterol-siRNA
Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
[ACTF2020 Freshman Competition]Exec 1