原题链接在这里: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. 如何理解nRF5芯片外设PPI

    PPI,英文全称Programmable Peripheral Interconnect,是Nordic独有的外设,其设计目的是让CPU处于idle模式下外设与外设之间也能完成相应通信,从而降低系统功 ...

  2. zabbix自动化运维学习笔记(服务器安装)

    最近博主开始接触自动化运维.首先就是zabbix这个开源的监控系统 一开始博主只是在自己的虚拟机上尝试安装.最后终于开始在公司的服务器上正式安装,教程博主也是通过度娘找的 这是原文:链接 安装环境:C ...

  3. Burpsuite的简单应用-y-Proxy

    打开burpsuite:Proxy功能 一.进入Proxy页面,代理设置 将浏览器的代理地址设置一样: 之前没有代理,直接添加,有的话,可以勾选就好了:不同浏览器,设置位置不一致,百度参考 二.执行: ...

  4. 场景设计以及Manual Scenario和Goal-OrientedScenario的区别

    Manual Scenario 手工场景 主要是设计用户变化,通过手工场景可以帮助我们分析系统的性能瓶颈.手动方案:如果要生成手动方案,请选择此方法.通过创建组并指定脚本.负载生成器和每组中包括的 V ...

  5. POJ 1568 极大极小搜索 + alpha-beta剪枝

    极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum & ...

  6. VMware虚拟机安装linux7并设置网络

    1.下载VMware虚拟机 https://www.vmware.com/cn/products/workstation/workstation-evaluation.html 永久激活12位序列号: ...

  7. tcpdump 使用实例

    详细的文档见tcpdump高级过滤技巧 基本语法 ========过滤主机--------- 抓取所有经过 eth1,目的或源地址是 192.168.1.1 的网络数据# tcpdump -i eth ...

  8. DNS污染——domain name的解析被劫持了返回无效的ip

    看下dns污染: bash-3.2$ dig twitter.com +trace ; <<>> DiG 9.10.6 <<>> twitter.com ...

  9. Apache编译安装

    1.准备好源码包并配置好yum源,需要的源码包包括:httpd-2.4.18.apr-1.5.2.tar.gz.apr-util-1.5.4.tar.gz 2.准备用户 groupadd -r apa ...

  10. L162

    More than 250 corporate signatories joined together to try and deal with plastic pollution in an ann ...