题目如下:

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.

解题思路:对于任意一个区间[i,j]能否经过至多K次替换可以变成只包含同一个字符的子串,只要判断除去其中某一个字符,其余剩下的字符的总和不超过K即可。所以本题的解题就是就是找到符合这个条件的最长区间。怎么找这个区间?我的方法是引入low和high两个index,从字符串头部开始遍历,依次判断[low,high]区间是否满足条件,满足的话high += 1,不满足则low += 1。

代码如下:

class Solution(object):
def check(self,dic,total,k):
mv = 0
flag = False
for i in dic.iterkeys():
if total - dic[i] <= k:
flag = True
mv = max(mv,total)
if flag == False and len(dic) > 0:
return -1
return mv def characterReplacement(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
if len(s) == 0:
return 0
low = 0
high = 1
dic = {}
dic[s[0]] = 1
total = 1
res = 0
s += '#'
while low < high and high < len(s):
rc = self.check(dic,total,k)
if rc == -1:
dic[s[low]] -= 1
total -= 1
if dic[s[low]] == 0:
del dic[s[low]]
low += 1
else:
dic[s[high]] = dic.setdefault(s[high],0) + 1
total += 1
high += 1 res = max(res,rc)
return res

【leetcode】424. Longest Repeating Character Replacement的更多相关文章

  1. LeetCode 424. Longest Repeating Character Replacement

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

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

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

  3. 424. Longest Repeating Character Replacement

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

  4. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

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

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

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

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

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

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

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

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

  9. LeetCode——Longest Repeating Character Replacement

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

随机推荐

  1. HTTP: 状态码200~505说明

    HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释: 1xx(临时响应) 表示 ...

  2. Redis GeoHash

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11632810.html 背景 微信找附近的人,滴滴找附近的单车,饿了么找附近的餐馆 GeoHash算法 ...

  3. project 计划添加编号或 任务分解时为任务添加编号

    [工具]-[选项]-[视图]-选择[显示大纲数字]-[确定]

  4. 转载--关于FPGA设计数字信号处理电路的心得

    FPGA使用的越来越广泛,除了可用于设计控制电路以为,数字信号处理电路更是FPGA的强项和难点.个人可以说才刚刚入门FPGA设计,也做过一些数字信号处理方面的电路设计,记录下个人心得体会. (一)善用 ...

  5. 圆周率Pi是如何计算出来的

    object SparkPi { def main(args: Array[String]) { val spark = SparkSession .builder .appName("Sp ...

  6. Python基础教程(013)--一行代码不要做多件事情

    前言 学习代码编写风格 内容 语法错误报错的信息 SystaxErrot:invalid syntax 语法错误, invalid 无效 每行代码负责完成一个动作 阅读代码,从左至右,从上到下. 学习 ...

  7. delphi 程序嵌入桌面效果的实现

    function Tform1.CreateRegion(wMask:TBitmap;wColor:TColor;hControl:THandle): HRGN; var dc, dc_c: HDC; ...

  8. 云平台(cloud platforms)

    云平台:允许开发者们或是将写好的程序放在“云”里运行,或是使用“云”里提供的服务,或二者皆是的服务 转向云计算(cloud computing),是业界将要面临的一个重大改变.各种云平台(cloud ...

  9. ElasticSearch 简介概念及核心

    1.ES是什么 ES是面向文档的Nosql,这意味着它可以存储整个对象或文档.然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索.在es中,你可以对文档(而非成行成列的数据)进行索 ...

  10. css > 的写法 html

    .userInfo-view .info .name::after { content: " "; display: inline-block; height: 12rpx; wi ...