题目如下:

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. centos在线安装mysql报错:file /etc/my.cnf conflicts between attempted installs of mysql-community-server-8.0.16-2.el7.x86_64 and MariaDB-common-10.4.6-1.el7.centos.x86_64

    错误提示:file /etc/my.cnf conflicts between attempted installs of mysql-community-server-8.0.16-2.el7.x8 ...

  2. 【Flutter学习】基本组件之弹窗和提示(SnackBar、BottomSheet、Dialog)

    一,概述 Flutter中的操作提示主要有这么几种 SnackBar.BottomSheet.Dialog,因为 Dialog样式比较多,放最后讲好了 二,介绍 SnackBar SnackBar的源 ...

  3. JAVA(JDK,JRE)更改目录安装及环境变量配置

    重温一下 JAVA(JDK,JRE)更改目录安装及环境变量配置 https://jingyan.baidu.com/article/e2284b2b5b7ae5e2e7118d11.html 备注:随 ...

  4. python input() 与raw_input()

    使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的1:纯数字输入 当输入为纯数字时 input返回的是数值类型,如int,float   ...

  5. 建站手册-浏览器信息:Google Chrome 浏览器

    ylbtech-建站手册-浏览器信息:Google Chrome 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_chrome.a ...

  6. ANSI转义代码(ANSI escape code)

    ANSI escape code - Wikipedia linux 输出绿色的✓TRUE,红色的✗FALSE : echo -e "\x1B[1;32m✓TRUE \x1B[0mXXX&q ...

  7. ubuntu 设置固定IP

    vim  /etc/network/interface address   要固定的IP地址 netmask  子网掩码  A类地址 默认255.0.0.0   B类地址默 255.255.0.0  ...

  8. LeetCode 最短无序连续子数组

    题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...

  9. fabric && cita 调研对比

    fabric && cita 调研 总结 若计划完全依赖上游的基础功能而不做任何改造,建议选择 fabric:否则,应选择 cita,针对自身业务场景进行持续优化. 一.功能 1.可扩 ...

  10. SSH远程免密码的密钥登录服务(Linux,Linux)

    本次实验基于两台Linux虚拟机之间的实验,一台做服务器,一台做客户机,模拟免密码的密钥登录. 首先两台虚拟机需要可以ping通,用客户机访问服务器. sshd服务主配置文件路径: /etc/ssh/ ...