[LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2 Output:
4 Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1 Output:
4 Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
这道题给我们了一个字符串,说我们有k次随意置换任意字符的机会,让我们找出最长的重复字符的字符串。这道题跟之前那道 Longest Substring with At Most K Distinct Characters 很像,都需要用滑动窗口 Sliding Window 来解。我们首先来想,如果没有k的限制,让我们求把字符串变成只有一个字符重复的字符串需要的最小置换次数,那么就是字符串的总长度减去出现次数最多的字符的个数。如果加上k的限制,我们其实就是求满足 (子字符串的长度减去出现次数最多的字符个数)<=k 的最大子字符串长度即可,搞清了这一点,我们也就应该知道怎么用滑动窗口来解了吧。我们用一个变量 start 记录滑动窗口左边界,初始化为0,然后遍历字符串,每次累加出现字符的个数,然后更新出现最多字符的个数,然后我们判断当前滑动窗口是否满足之前说的那个条件,如果不满足,我们就把滑动窗口左边界向右移动一个,并注意去掉的字符要在 counts 里减一,直到满足条件,我们更新结果 res 即可。需要注意的是,当滑动窗口的左边界向右移动了后,窗口内的相同字母的最大个数貌似可能会改变啊,为啥这里不用更新 maxCnt 呢?这是个好问题,原因是此题让求的是最长的重复子串,maxCnt 相当于卡了一个窗口大小,我们并不希望窗口变小,虽然窗口在滑动,但是之前是出现过跟窗口大小相同的符合题意的子串,缩小窗口没有意义,并不会使结果 res 变大,所以我们才不更新 maxCnt 的,参见代码如下:
class Solution {
public:
int characterReplacement(string s, int k) {
int res = , maxCnt = , start = ;
vector<int> counts(, );
for (int i = ; i < s.size(); ++i) {
maxCnt = max(maxCnt, ++counts[s[i] - 'A']);
while (i - start + - maxCnt > k) {
--counts[s[start] - 'A'];
++start;
}
res = max(res, i - start + );
}
return res;
}
};
类似题目:
Longest Substring with At Least K Repeating Characters
Longest Substring with At Most K Distinct Characters
Longest Substring with At Most Two Distinct Characters
Longest Substring Without Repeating Characters
参考资料:
https://leetcode.com/problems/longest-repeating-character-replacement/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Repeating Character Replacement 最长重复字符置换的更多相关文章
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode——Longest Repeating Character Replacement
1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...
- Leetcode 424.替换后的最长重复字符
替换后的最长重复字符 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 注意:字符串长度 和 ...
- Java实现 LeetCode 424 替换后的最长重复字符
424. 替换后的最长重复字符 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 注意: 字 ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- 424 Longest Repeating Character Replacement 替换后的最长重复字符
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
随机推荐
- PHP之提取多维数组指定列的方法
前言:有时候在开发中会遇到这样的问题,我们需要把有规律的多维数组按照纵向(列)取出,有下面的方法可用: 我们将拿下面的数组来处理: $arr = array( '0' => array('id' ...
- Mysql5.6 online ddl
Innodb性能改善方面: --Users can add indexes and perform standard table alterations while the database rema ...
- iOS项目开发中的知识点与问题收集整理①(Part 一)
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- csharp: Download SVN source
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- css知识点整理
CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css引入的方式 1.行内样式:行内式是在标记的style ...
- Android开发学习—— 下载网络图片
现在几乎所有的应用都在使用网络来达到浏览的目的.对于特定领域 使用xnpp协议 像即时通讯软件.但大多数还是使用HTTP协议来交互. 网络图片查看器 HTTP协议 下载网络 图片 <Relati ...
- UITableview delegate dataSource调用探究
UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...
- 设置这些之后,Google突然可以打开了
打开的是:https://www.google.com.hk
- css揭秘--笔记(未完)
第0章 关于本书 1, 本书要用到一个工具函数————$$(),它可以让我们更容易获取和遍历所有匹配特定css选择符的dom元素: function $$(selector,context){ con ...