原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/

题目:

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.

题解:

类似快慢指针维护substring的方法在Minimum Window Substring里有总结.

随着runner向前推进找出最高frequency word的count. substring整个长度减掉最大的count 就是需要调换的字符数, 若大于k就移动walker.

移动walker时只需要不需要更改maxCount, runner-walker-maxCount自会变小.

Why we don't need to update maxCount, here we only need to care the longest substring, it is maxCount+k.

Thus we only needto care when there is new largert maxCount.

Here while only execuates once, move walker once, it already break the while condition.

Time Complexity: O(s.length()).

Space: O(1).

AC Java:

 class Solution {
public int characterReplacement(String s, int k) {
int [] map = new int[256];
int walker = 0;
int runner = 0;
int maxCount = 0;
int res = 0;
while(runner < s.length()){
maxCount = Math.max(maxCount, ++map[s.charAt(runner++)]);
while(runner-walker-maxCount > k){
map[s.charAt(walker++)]--;
} res = Math.max(res, runner-walker);
}
return res;
}
}

类似Longest Substring with At Most Two Distinct Characters.

LeetCode 424. Longest Repeating Character Replacement的更多相关文章

  1. 【leetcode】424. Longest Repeating Character Replacement

    题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...

  2. 424 Longest Repeating Character Replacement 替换后的最长重复字符

    给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...

  3. 424. Longest Repeating Character Replacement

    以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...

  4. 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...

  5. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  6. Leetcode: Longest Repeating Character Replacement && G 面经

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  7. G 面经 && Leetcode: Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  8. LeetCode——Longest Repeating Character Replacement

    1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...

  9. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

随机推荐

  1. GTID主从 与 传统主从复制

    一.主从复制 1.)普通主从复制: 普通主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID.如果您省略ser ...

  2. python基础方法

    一.忽略大小写相等upper(),lower() def cmp(str1,str2): return str1.upper()==str2.upper() list1 = 'MAC' list2 = ...

  3. Attribute 'items' must be an array, a Collection or a Map错误解决!

    唉!真的要说一句话叫做论一串代码的重要性!就是如此的气人!气的牙根痒痒! 前几天刚刚写过SpringMVC之ModelAndView的 jsp值在浏览页面不显示的问题!也是因为这一串代码,但是这一次一 ...

  4. 搞懂分布式技术4:ZAB协议概述与选主流程详解

    搞懂分布式技术4:ZAB协议概述与选主流程详解 ZAB协议 ZAB(Zookeeper Atomic Broadcast)协议是专门为zookeeper实现分布式协调功能而设计.zookeeper主要 ...

  5. Repeat a string repeat a string

    重要的事情说3遍! 重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. 这是一些对你有帮助的资源: Global String Object 这道题的思路就是按照题目要求一步一步 ...

  6. C#_串口通信_SerialPort_一个最基础的串口程序

    一个最最基础的 串口通信 程序!!! 最近正在学c#_还不是很熟悉_只是有点java的基础 SerialPort类 的介绍 http://msdn.microsoft.com/zh-cn/librar ...

  7. SVM之解决线性不可分

    SVM之问题形式化 SVM之对偶问题 SVM之核函数 >>>SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 上一篇SVM之核函数介绍了通过计算样本核函数,实际上将样本映射 ...

  8. 安装mysql 5.7版本遇到问题及解决办法

    遇见问题:1.执行MySql启动命令:net start mysql 报错:Mysql 服务正在启动Mysql 服务无法启动 服务没有报告任何错误. 解决办法:在mysql的安装目录下在my-defa ...

  9. iOS系统知识架构(转)

    转载的,哪些所谓的资深开发,谁敢说自己没有知识盲区?http://ios.skyfox.org/route.html

  10. yii2.0 使用不同语言

    1.建立语言目录.文件.项目根目录建立messages文件夹.存放不同语言对应的目录文件. 例如中文和英文 message 下建立两个文件夹 en.zh_CN 里面可以对应着多个翻译文件 2.在mai ...