当前位置:网站首页>424. 替换后的最长重复字符 ●●
424. 替换后的最长重复字符 ●●
2022-07-05 23:06:00 【chenyfan_】
424. 替换后的最长重复字符 ●●
描述
给你一个字符串 s 和一个整数 k 。你可以选择字符串中的任一字符,并将其更改为任何其他大写英文字符。该操作最多可执行 k 次。
在执行上述操作后,返回包含相同字母的最长子字符串的长度。
示例
输入:s = “AABABBA”, k = 1
输出:4
解释:
将中间的一个’A’替换为’B’,字符串变为 “AABBBBA”。
子串 “BBBB” 有最长重复字母, 答案为 4。
题解
1. 双指针 1
用一个一维数组存储窗口内各字母的出现次数,每次以左边界上的字母为固定字母(即不同字母替换成该字母),右指针逐个往右移动,当无法再替换时,左指针右移,并更新当前的固定字母;
注意,因为本解法每次都以左边界字母为固定字母,所以还需要考虑左边界之前的字母被替换后的长度,
比如 s = “ACBBB”,k = 2;正确结果为 5;若不考虑 front ,则结果为 3。
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> cnts(26, 0);
int n = s.length(), ans = 0;
int l = 0, rest = k;
cnts[s[0]-'A'] = 1; // 初始左边界
for(int r = 1; r < n; ++r){
if(s[r] != s[l]) --rest; // 替换操作,--rest
++cnts[s[r]-'A']; // 统计频次
while(rest < 0){
// 替换失败
--cnts[s[l]-'A'];
++l; // 移动左指针
rest = k - ((r-l+1) - cnts[s[l]-'A'])); // 更新固定字母为左边界字母,(r-l+1)-cnts[s[l]-'A'] 表示当前窗口下需替换字母的个数
}
int front = min(rest, l); // 考虑替换前面的字母
ans = max(ans, r-l+1+front); // 更新最大值
}
return ans;
}
};
2. 双指针 2
枚举字符串中的每一个位置作为右端点,然后找到其最远的左端点的位置,满足该区间内除了出现次数最多的那一类字符之外,剩余的字符(即非最长重复字符)数量不超过 k 个。
这样我们可以想到使用双指针维护这些区间,每次右指针右移,如果区间仍然满足条件,那么左指针不移动,否则左指针至多右移一格,保证区间长度不减小(保持不变或增大)。
虽然这样的操作会导致部分区间不符合条件,即移动左指针后,该区间内非最长重复字符超过了 k 个(maxSame发生改变但可能未更新)。
但是这样的区间也同样不可能对答案产生贡献(直到移动右指针出现频次更高的字母)。当我们右指针移动到尽头,左右指针对应的区间的长度必然对应一个长度最大的符合条件的区间。
每次区间右移,我们更新右移位置的字符出现的次数,然后尝试用它更新重复字符出现次数的历史最大值,最后我们使用该最大值计算出区间内非最长重复字符的数量,以此判断左指针是否需要右移即可。
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> cnts(26, 0);
int n = s.length();
int l = 0, r = 0, maxSame = 0;
for(; r < n; ++r){
++cnts[s[r]-'A'];
maxSame = max(maxSame, cnts[s[r]-'A']); // 更新最大频次,出现更大频次时才有效
if(k < (r-l+1) - maxSame){
// 替换失败
--cnts[s[l]-'A']; // 移动左指针,缩小窗口(最大总长度此时不变)
++l;
}
} // for 循环中 r 多走一步,结束时 r = n
return r-l; // 窗口大小保证不减小,最大符合条件的窗口长度
}
};
边栏推荐
- Go语言实现原理——锁实现原理
- leecode-学习笔记
- 无刷驱动设计——浅谈MOS驱动电路
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- 派对的最大快乐值
- Expectation, variance and covariance
- 利用LNMP实现wordpress站点搭建
- Design and implementation of secsha system
- 【原创】程序员团队管理的核心是什么?
- Summary of binary tree recursive routines
猜你喜欢

Negative sampling

TVS管和ESD管的技术指标和选型指南-嘉立创推荐

Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
![[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]](/img/df/9aa83ac5bd9f614942310a040a6dff.jpg)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]

如何快速理解复杂业务,系统思考问题?

透彻理解JVM类加载子系统

【Note17】PECI(Platform Environment Control Interface)

How to quickly understand complex businesses and systematically think about problems?

Finally understand what dynamic planning is

Hcip day 12 (BGP black hole, anti ring, configuration)
随机推荐
Douban scoring applet Part-2
Error when LabVIEW opens Ni instance finder
February 13, 2022-4-symmetric binary tree
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
asp.net弹出层实例
Thoroughly understand JVM class loading subsystem
TypeError: this. getOptions is not a function
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Leetcode sword finger offer brush questions - day 21
Element positioning of Web Automation
派对的最大快乐值
Hcip day 12 (BGP black hole, anti ring, configuration)
Go语言实现原理——锁实现原理
The maximum happiness of the party
regular expression
Selenium+pytest automated test framework practice
Initial experience | purchase and activate typora software
Un article traite de la microstructure et des instructions de la classe
东南亚电商指南,卖家如何布局东南亚市场?