G 面经 && Leetcode: 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.
真是被这道题气死了,总是弄不对
The problem says that we can make at most k changes to the string (any character can be replaced with any other character). So, let's say there were no constraints like the k. Given a string convert it to a string with all same characters with minimal changes. The answer to this is
length of the entire string - number of times of the maximum occurring character in the string
Given this, we can apply the at most k changes constraint and maintain a sliding window such that
(length of substring - number of times of the maximum occurring character in the substring) <= k
The initial step is to extend the window to its limit, that is, the longest we can get to with maximum number of modifications. Until then the variable start will remain at 0.
Then as end increase, the whole substring from 0 to end will violate the rule, so we need to update start accordingly (slide the window). We move start to the right until the whole string satisfy the constraint again. Then each time we reach such situation, we update our max length.
Prefered solution:
class Solution {
public int characterReplacement(String s, int k) {
int[] counts = new int[26];
int l = 0, r = 0, res = 0;
char maxChar = '\0';
int maxCount = 0;
for (; r < s.length(); r ++) {
char cur = s.charAt(r);
counts[cur - 'A'] ++;
if (counts[cur - 'A'] > maxCount) {
maxCount = counts[cur - 'A'];
maxChar = cur;
}
while (l<= r && r - l + 1 > maxCount + k) {
counts[s.charAt(l) - 'A'] --;
if (s.charAt(l) == maxChar) {
maxCount = counts[s.charAt(l) - 'A'];
for (int t = 0; t < 26; t ++) {
if (counts[t] > maxCount) {
maxCount = counts[t];
maxChar = (char)('A' + t);
}
}
}
l ++;
}
res = Math.max(res, r - l + 1);
}
return res;
}
}
Another Solution Window never shrink:
public int characterReplacement(String s, int k) {
int[] charCount = new int[26]; int left, right, maxCount, maxLen;
left = right = maxCount = maxLen = 0; while(right < s.length()){
charCount[s.charAt(right) - 'A']++;
maxCount = Math.max(maxCount, charCount[s.charAt(right) - 'A']);
if(right - left + 1 - maxCount > k) charCount[s.charAt(left++) - 'A']--;
maxLen = Math.max(right++ - left + 1, maxLen);
}
return maxLen;
}
Very easy and simple Python solution: (Idea the same but easy to understand)
import collections
def findLongestSubstring(s,k):
res=0
left=0
counts = collections.Counter()
for right in range(0,len(s)):
counts[s[right]] += 1
char_w_maxCount = counts.most_common(1)[0][1]
while right - left - char_w_maxCount + 1 > k:
counts[s[left]] -= 1
char_w_maxCount = counts.most_common(1)[0][1]
left +=1 res = max(res,right-left+1) return res print(findLongestSubstring("abab",2)) 4
print(findLongestSubstring("aabbbba",1)) 5
print(findLongestSubstring("abcaea",1)) 3
print(findLongestSubstring("aababba",1)) 4
print(findLongestSubstring("abcae",2)) 4
print(findLongestSubstring("aaaaa",2)) 5
G 面经 && Leetcode: Longest Repeating Character Replacement的更多相关文章
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [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 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- leetcode424 Longest Repeating Character Replacement
""" Given a string s that consists of only uppercase English letters, you can perform ...
- 424 Longest Repeating Character Replacement 替换后的最长重复字符
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...
随机推荐
- React组件的State
React组件的State 1.正确定义State React把组件看成一个状态机.通过与用户的交互,实现不同状态,然后渲染UI,让用户界面和数据保持一致.组件的任何UI改变,都可以从State的变化 ...
- shell脚本之tr命令使用
tr命令用来进行对标准输入的内容做替换.例如 # echo 'HELLO WORLD!!!' | tr "A-Z" "a-z" hello world!!! 这 ...
- ReactNative小笔记
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export d ...
- FFmpeg制作+x264+faac
https://blog.csdn.net/leixiaohua1020/article/details/47071547 雷神的博客 https://www.jianshu.com/p/3f023 ...
- 出于性能考虑,C语言自动地以传地址的方式将数组传递给被调函数 const 编译错误 最小权限原则
#include <stdio.h> int main(void) { char array[5]; printf("array=%p,&array[0]=%p,& ...
- 终于碰到iOS对象集合深拷贝的坑
从原始数组,拆分排列组合成新数组,同时给新的数组中的模型元素追加字段,数组的容量翻倍,如果不用深拷贝,后面追加的值就把前面的值覆盖了 UnitModel *model1 = [UnitModel ne ...
- 12.2RAC搭建记录
12.2RAC环境搭建记录 安装前资源检查 资源限制要求/etc/security/limits.conf Table 6-1 Installation Owner Resource Limit Re ...
- scala-模式匹配
option模式匹配: var map1=Map("abc"->5,"eee"->6) var x=map1.get("abc" ...
- eclipse背景色设置成护眼色(豆沙绿)
1.点击windows -->preferences 2.展开Editors 3.选择自定义颜色 4.把色调调成:85 饱和度调成:123 亮度调成205 即可调成豆沙绿色了 然后点确定.
- IGMP协议
IGMP报文格式: 4bit的IGMP版本(1)+4bit的IGMP类型(1-2)+8bit未用+16bit检验和(同ICMP)+32bit组地址(D类IP地址) 类型为1说明是由多播路由器发出的查询 ...