leetcode424 Longest Repeating Character Replacement
"""
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的更多相关文章
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode——Longest Repeating Character Replacement
1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- 424. Longest Repeating Character Replacement
以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...
随机推荐
- 简单的jquery Ajax进行登录!
本案例包括login.html.login.php.jquery-1.12.0.min.js三个文件,只需将这三个文件放到同一文件夹下,即可运行. login.html: <!DOCTYPE h ...
- Java面向对象编程 -6.2
数组的引用传递 通过数组的基本定义可以发现,在数组使用的过程中依然需要使用new进行内存空间的开辟,同理,那么也一定存在有内存的关系匹配问题. 但是数组本身毕竟属于引用数据类型,那么既然是引用数据类型 ...
- LaunchPad
链接:https://ac.nowcoder.com/acm/contest/3665/D来源:牛客网 Hery is a boy with strong practical abilities. N ...
- Rsync+inotify数据同步
安装环境 备份服务器端:CentOS7,IP:192.168.1.100 备份客户端:CentOS7,IP:192.168.1.200 服务器端Rsync服务部署 1.安装程序包 # yum –y i ...
- Java读文件夹
使用JAVA读取文件夹中的多个文件 package hx.ReadFile; import java.io.FileNotFoundException; import java.io.IOExcept ...
- SpringCloud全家桶学习之客户端负载均衡及自定义负载均衡算法----Ribbon(三)
一.Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具(这里区别于nginx的负载均衡).简单来说,Ribbon是Netf ...
- pdf .js和tableexport.js使用时遇到的2问题。
pdf .js 问题一:报错 network.js:71 The provided value 'moz-chunked-arraybuffer' is not a valid enum value ...
- 根据权限显示accordion
前端界面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.asp ...
- Centos 安装 mysql 5.7
下载mysql yum包 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 安转软件源 xxx.rpm是刚刚 ...
- JS 上传图片压缩,原比例压缩
复制 粘贴 改吧改吧就可用,原生js var fileObj = file.file;//原文件 file是我用vue-vant里的组件,里边有file(原文件)和content(base64) 其它 ...