当前位置:网站首页>golang刷leetcode:最大波动的子字符串
golang刷leetcode:最大波动的子字符串
2022-08-02 20:38:00 【用户9710217】
字符串的 波动 定义为子字符串中出现次数 最多 的字符次数与出现次数 最少 的字符次数之差。
给你一个字符串 s ,它只包含小写英文字母。请你返回 s 里所有 子字符串的 最大波动 值。
子字符串 是一个字符串的一段连续字符序列。
示例 1:
输入:s = "aababbb"
输出:3
解释:
所有可能的波动值和它们对应的子字符串如以下所示:
- 波动值为 0 的子字符串:"a" ,"aa" ,"ab" ,"abab" ,"aababb" ,"ba" ,"b" ,"bb" 和 "bbb" 。
- 波动值为 1 的子字符串:"aab" ,"aba" ,"abb" ,"aabab" ,"ababb" ,"aababbb" 和 "bab" 。
- 波动值为 2 的子字符串:"aaba" ,"ababbb" ,"abbb" 和 "babb" 。
- 波动值为 3 的子字符串 "babbb" 。
所以,最大可能波动值为 3 。
示例 2:
输入:s = "abcde"
输出:0
解释:
s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值都是 0 。
提示:
1 <= s.length <= 104
s 只包含小写英文字母。
解题思路:
1,问题简化:首先我们总是可以拆分出长度为1的子串,最小波动值必为1;因此本题就是求最大波动值
2,假设我们已经知道i位置之前的包含b的子串波动值f0(i-1,a,b),不包含b的子串波动值f1(i,a,b),其中a为出现频率最大的字符,b为出现频率最小的字符
3,对于i位置有三种情况:
A,s[i]==a
B,s[i]==b
C,是其他字符
4,对于情况B,又分两种情况需要考虑
B1,前i-1个字符中含有b
B2,前i-1个字符中不含b
5,递推方程如下
A,s[i]==a
f0(i,a,b)=f0(i-1,a,b)+1
f1(i,a,b)=f1(i-1,a,b)+1
B,s[i]==b
f0(i,a,b)=max(f0(i-1,a,b)-1,-1)
f1(i,a,b)=max(f1(i-1,a,b)-1,f0(i-1,a,b)-1,-1)
C,是其他字符
f0(i,a,b)=f0(i-1,a,b)
f1(i,a,b)=f1(i-1,a,b)
6,由于只依赖i-1位置,所以两个变量即可
代码实现
func largestVariance(s string) int {
res:=0
for a:='a';a<='z';a++{
for b:='a';b<='z';b++{
if a==b{
continue
}
for i,dp0,dp1:= 0,-10000,-10000; i < len(s);i++ {
v:=0
if s[i] == byte(a){
v=1
}
if s[i]==byte(b){
v=-1
}
if s[i]==byte(b){
dp1=max(dp1 + v, max(dp0 + v, v))
}else{
dp1 = dp1 + v
}
dp0 = max(dp0 + v, v)
res = max(res, dp1)
}
}
}
return res
}
func max(a,b int)int{
if a>b{
return a
}
return b
}
边栏推荐
- Nervegrowold hands-on learning deep learning V2 - Bert pre training data set and code implementation
- The five classification of software testing
- [21 Days Learning Challenge] Bubble Sort and Insertion Sort
- ICLR 2022最佳论文:基于对比消歧的偏标签学习
- 汇编语言中b和bl关键字的区别
- Which thread pool does Async use?
- Xcode13.1 run engineering error fatal error: 'IFlyMSC/IFly h' file not found
- golang刷leetcode: 在每个树行中找最大值
- 浅议.NET遗留应用改造
- 李沐动手学深度学习V2-bert预训练数据集和代码实现
猜你喜欢
接口测试常用工具及测试方法(入门篇)
PyRosetta 安装方法之Conda安装
2022年金九银十,Android面试中高频必问的问题汇总
php 单引号 双引号 -> => return echo
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
软件成分分析:华为云重磅发布开源软件治理服务
【SLAM】DM-VIO(ros版)安装和论文解读
WPF development through practical 】 【 automatic production management platform
Common tools and test methods for interface testing (Introduction)
sre成长之路
随机推荐
信息学奥赛一本通(1259:【例9.3】求最长不下降序列)
Packages and packages, access modifiers
Day35 LeetCode
C#异步和多线程
PLC工作原理动画
SQL基础练习题(mysql)
Li Mu hands-on learning deep learning V2-bert and code implementation
软件测试的流程规范有哪些?具体要怎么做?
[C题目]力扣142. 环形链表 II
HCIP--路由策略实验
Electrical diagram of power supply system
Xcode13.1 run engineering error fatal error: 'IFlyMSC/IFly h' file not found
How to use windbg check c # a thread stack size?
解道8-编程技术5
Common tools and test methods for interface testing (Introduction)
Digital twins help visualize the construction of smart cities
Details in C# you don't know
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
C# Monitor class
Day35 LeetCode