"""
Given a string s that consists of only uppercase English letters, you can perform at most k operations on that string.
In one operation, you can choose any character of the string and change it to any other uppercase English character.
Find the length of the longest sub-string 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.
"""
class Solution:
def characterReplacement(self, s, k):
from collections import defaultdict
d = defaultdict(int)
i = 0
count = 0 #!!!存储当前出现字符最多的数量
res = 0
for j in range(len(s)):
d[s[j]] += 1
count = max(count, d[s[j]])
while j - i + 1 - count > k: #!!!滑动窗口移动关键
d[s[i]] -= 1
count = max(count, d[s[i]])
i += 1
res = max(res,j - i + 1)
return res

leetcode424 Longest Repeating Character Replacement的更多相关文章

  1. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

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

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

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

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

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

  4. LeetCode——Longest Repeating Character Replacement

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

  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 424. Longest Repeating Character Replacement

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

  7. 【leetcode】424. Longest Repeating Character Replacement

    题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...

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

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

  9. 424. Longest Repeating Character Replacement

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

随机推荐

  1. php用户名正则验证

    在php中,通过正则表达式对用户名进行格式验证,烦了我好久,终于找到了解决办法,在这里分享给大家. 用户名验证规则:用户名只能由数字.字母.中文汉字及下划线组成,不能包含特殊符号. <?php ...

  2. Spring bean的bean的三种实例化方式

     Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...

  3. 安装RabbitMQ,一直提示Erlang版本过低

    1.背景 windows系统,控制面板卸载Erlang后,重新安装Erlang成功,当再安装RabbitMQ时,报如下提示: 意思就是说Erlang版本过低,请安装更高的版本. 出现上面问题的原因,是 ...

  4. win10鼠标右击 新建文件夹 反应缓慢、迟钝

    1. 先使用360杀毒,步骤如下: 2.修改注册表 https://jingyan.baidu.com/article/363872ec9e25972e4ba16f82.html 3.  如果仍未解决 ...

  5. 老段带你学鸟哥Linux视频教程 包含基础班+提高班

    老段带你学鸟哥Linux视频教程 包含基础班+提高班,附带pdf文档. 目录结构如下: 目录:/-老段带你学鸟哥Linux视频教程 [.9G] ┣━━老段带你学鸟哥-服务器篇 [1009.4M] ┃ ...

  6. 初学做uniapp项目过程梳理的一些记录

    1.uniapp不显示h5头部 第一种写法: { "path" : "pages/yunshi/yunshi", "style" : { & ...

  7. 使用$.ajax时的注意事项

    做PHP难免接触js,我也是这样,使用ajax的时候,我比较习惯使用$.ajax({}),这种方式,因为通用性较强.有时候会较少使用js,隔一段时间后再使用,有些细节内容容易模糊不清,这一次,我又忘记 ...

  8. Navicat for MySQL怎么往表中填数据

    只有往表中更新数据,数据库才会起到真正的作用. 工具/原料 仔细阅读 方法/步骤 1.打开数据库,首先连接localhost,如图所示. ​ 2.连接成功后,右侧便会显示已经建成的表,找到要修改的表, ...

  9. 100w并发产生唯一随机id

    #coding=utf-8 import time import base64 import getopt import sys import threading import random impo ...

  10. jquery 判定checkbox是否选中

    CheckBox 判定是否选中 使用 attr('checked')来做判别是不行的,除非所有的选中取消都是使用这个属性来处理. 正确的做法是使用 .prop('checked') 来判定.