【leetcode】424. Longest Repeating Character Replacement
题目如下:
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的更多相关文章
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 424 Longest Repeating Character Replacement 替换后的最长重复字符
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...
- 424. Longest Repeating Character Replacement
以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- [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 && 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 ...
随机推荐
- PHP计算经纬度在百度多边形区域内
最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...
- Vue学习笔记-组件通信-父传子(props中的驼峰标识)
在组件中,使用选项props来声明需要从父级接收到的数据.props的值有两种方式:方式一:字符串数组,数组中的字符串就是传递时的名称.方式二:对象,对象可以设置传递时的类型,也可以设置默认值等. & ...
- loadrunner(预测系统行为和性能的负载测试工具)
LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用LoadRu ...
- BZOJ 1369: [Baltic2003]Gem(树形dp)
传送门 解题思路 直接按奇偶层染色是错的,\(WA\)了好几次,所以要树形\(dp\),感觉最多\(log\)种颜色,不太会证. 代码 #include<iostream> #includ ...
- bugku | flagphp
1.开脑洞 http://123.206.87.240:8002/flagphp/?hint=1 2.读源码得到条件是要求反序列化后的‘ISecer’的值等于$KEY <?php error_r ...
- iOS 开发加密做法
一般做法是这样的: 客户端 每一个请求的URL中加上时间的参数.对url中的参数是排序好的. 然后对这个URL进行MD5.将这个MD5作为最后一个参数(sign)拼接到url最后. 服务端 收到请求后 ...
- 在React中跨组件分发状态的三种方法
在React中跨组件分发状态的三种方法 当我问自己第一百次时,我正在研究一个典型的CRUD屏幕:"我应该将状态保留在这个组件中还是将其移动到父组件?". 如果需要对子组件的状态进行 ...
- Modular arithmetic and Montgomery form 实现快速模乘
题目: 电音之王 题解: 求数列前n项相乘并取模 思路: ①.这题的乘法是爆long long的,可以通过快速幂的思想去解决(按数位对其中的一个数进行剖分).当然你的乘法会多出一个log的复杂度... ...
- Linux 下查看用户组信息
1.id 通过id 可以直接查看到当前登陆用户的uid, group id , group name 等等.. 2.groups
- java并发编程笔记(八)——死锁
java并发编程笔记(八)--死锁 死锁发生的必要条件 互斥条件 进程对分配到的资源进行排他性的使用,即在一段时间内只能由一个进程使用,如果有其他进程在请求,只能等待. 请求和保持条件 进程已经保持了 ...