原题链接在这里: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. JavaScript封装Ajax工具函数及jQuery中的ajax,xhr在IE的兼容

    封装ajax工具函数 首先要思考:1.为什么要封装它?提高开发效率2.把一些不确定的情况考虑在其中 a. 请求方式 b. 请求地址 c. 是否异步 d. 发送参数 e. 成功处理 f. 失败处理3.确 ...

  2. ContentPresenter元素

    一个内容控件 分解它的“结构树”,肯定能够看到ContentPresenter“元素”,该元素的功能:用来为“内容控件”显示“Content”

  3. Spring框架中,在工具类或者普通Java类中调用service或dao

    spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...

  4. 数据挖掘之Python调用R包、函数、脚本

    Python中集成R :参考博客http://blog.csdn.net/weidelight/article/details/44946785

  5. Python之路day13 web 前端(JavaScript,DOM操作)

    参考链接:http://www.cnblogs.com/wupeiqi/articles/5433893.html day13 1. CSS示例 2. JavaScript 3. DOM操作 上节内容 ...

  6. Ubuntu相关命令

    此贴包含自己搭建网站以及自学Ubuntu遇到的相关命令,方便以后查看,故相关帖子整理记录在此! 用户切换 当前用户切换到root用户,只需要执行sudo su即可. root用户切回user用户,只需 ...

  7. Ansible 小手册系列 十一(变量)

    变量名约束 变量名称应为字母,数字和下划线. 变量应始终以字母开头. 变量名不应与python属性和方法名冲突. 变量使用 通过命令行传递变量(extra vars) ansible-playbook ...

  8. day5-subprocess模块

    一.概述 实际应用中,有些时候我们不得不跟操作系统进行指令级别的交互,如Linux中的shell.Python中早期通过os模块和commands模块来实现操作系统级别的交互,但从2.4版本开始,官方 ...

  9. ubuntu12.04+openni+nit+SensorKinect环境搭建

    一.安装openni 1.下载openni                  OpenNI-Bin-Dev-Linux-x64-v1.5.4.0.tar.bz2 2.cd ~; mkdir kinec ...

  10. hystrix 线程数,超时时间设置测试

    拜读了大拿的文章,收藏起来 https://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&mid=2247483791&idx=1&sn= ...