当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Cooler Navigation helps you shop easily in shopping malls without confusion

双功能RGD-TAT修饰DNA纳米胶束|聚苯胺纳米线修饰DNA(PAINW/DNA)

VR直播营销需求增加,数据模块为我们铺路

回归——分层回归

【无标题】

PEG-siRNA-PCL|siRNA-PEG-LHRH|MPEG-siRNA 甲氧基聚乙二醇修饰核酸

探索创客教育在线管理实施体系

分析少年派2中的Crypto

一 JS中Promise用法、二闭包的概念与用法、三对象创建的四种方式与区区别、四 如何声明一个类

4. SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南
随机推荐
378. 有序矩阵中第 K 小的元素
Writing Elegant Kotlin Code: Talk About What I Think "Kotlinic"
The difference between uri and url is simple to understand (what is the difference between uri and url)
从实例学Kettle(一):获取股票行情数据
荧光量子点修饰siRNA-QDs|纳米金修饰siRNA-Au(RNA修饰方式方法)
WPF 实现抽屉菜单
刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)
OneNote 教程,如何在 OneNote 中做笔记?
OneNote tutorial, how to take notes in OneNote?
打破原则!MongoDB 引入 SQL?
用对象字面量或Map替代Switch/if语句
南华早报 | 助力亚洲最具公信力报章实现AD域自动化管理
怎么实现您的个人知识库?
LOG4J 学习
Kotlin - 协程作用域 CoroutineScope、协程构建器 CoroutineBuilder、协程作用域函数 CoroutineScope Functiom
RedisJson 横空出世!
VSCode配置终端为系统命令行
Samba server configuration (when a server is required)
mysql get field comments and get table fields
WeChat Mini Program 30 Customizing Templates and Obtaining User Login Credentials