题目如下:

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. 【leetcode】609. Find Duplicate File in System

    题目如下: Given a list of directory info including directory path, and all the files with contents in th ...

  2. 前端-个人网页开发最常用的插件Superslide 与 swiper

    给初入前端的同学,安利2个轮播图插件 SuperSlide 与 Swiper ! SuperSlide PC端与移动端 的轮播图特效都是分开写2个js插件,而Swiper 基本一个插件可以写PC端与移 ...

  3. 【Mybatis】Mybatis缓存

    mybatis提供了缓存机制减轻数据库压力,提高数据库性能 mybatis的缓存分为两级:一级缓存.二级缓存 一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效 二级缓 ...

  4. 【HDOJ6606】Distribution of books(二分,BIT)

    题意:给定一个长为n的数组,要求挑它前缀的一段,将其分成k段,使得每段和的最大值最小 1<=k<=n<=2e5,abs(a[i])<=1e9 思路: 刚开始写了线段树TLE 改 ...

  5. 用C#编写ActiveX控件

    http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/homer/archive/2005 ...

  6. Visible Lattice Points

    题目链接 题意:给个N*N的矩形点,求在原点看去能看到多少个点 思路:除了(1,0),(0,1),(1,1)外其他点的xy都互质.所以求欧拉函数.fhi[i]从2加到n,再是两倍,再加3. #incl ...

  7. Redis的高级特性一览

    更多内容,欢迎关注微信公众号:全菜工程师小辉.公众号回复关键词,领取免费学习资料. 应用场景 缓存系统:用于缓解数据库的高并发压力 计数器:使用Redis原子操作,用于社交网络的转发数,评论数,粉丝数 ...

  8. mvc 当中 [ValidateAntiForgeryToken] 的作用 转载https://www.cnblogs.com/hechunming/p/4647646.html

    一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...

  9. s11 day106-107 RBAC模块

    一.登录 把权限存在session中 1. rbac models from django.db import models class Permission(models.Model): " ...

  10. 如何判断索引是否生效--explain

    explain 显示了MySql 如何使用索引来处理select语句以及连接表. 使用方式在select 前面加上 explain就可以了 示例:explain select id , name ta ...