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

Leetcode: Longest Repeating Character Replacement && G 面经的更多相关文章

  1. G 面经 && Leetcode: 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 最长重复字符置换

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

  3. LeetCode——Longest Repeating Character Replacement

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

  4. LeetCode 424. Longest Repeating Character Replacement

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

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

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

  6. 【leetcode】424. Longest Repeating Character Replacement

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

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

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

  8. leetcode424 Longest Repeating Character Replacement

    """ Given a string s that consists of only uppercase English letters, you can perform ...

  9. 424 Longest Repeating Character Replacement 替换后的最长重复字符

    给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...

随机推荐

  1. 【BZOJ】2333: [SCOI2011]棘手的操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=2333 题意: 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i], ...

  2. 【bzoj3527】[Zjoi2014]力 FFT

    2016-06-01  21:36:44 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 我就是一个大傻叉 微笑脸 #include&l ...

  3. __attribute__ 变量对齐

    http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/ 一.   __attribute__ ((aligned (n))) ...

  4. Centos 下安装Docker 遇到的一些错误

    1.公司的服务器的内核版本:2.6.32-431.23.3.el6_x86_64 如何升级内核请参考前一篇文章 2.在这个地址上面下载 的 https://test.docker.com/builds ...

  5. Nodejs Http发送post请求

    Nodejs  Http发送post请求 var http = require('http'); function epay(params) { console.log(" COME IN& ...

  6. 李洪强iOS经典面试题129

    1. 怎么解决缓存池满的问题(cell) ios中不存在缓存池满的情况,因为通常我们ios中开发,对象都是在需要的时候才会创建,有种常用的说话叫做懒加载,还有在UITableView中一般只会创建刚开 ...

  7. 用Maven生成Eclipse中的Web项目

    转自:http://my.oschina.net/u/939893/blog/170185 进入workspace, 输入以下命令 mvn archetype:generate -DgroupId=  ...

  8. odoo报表条码无法显示解决[转]

    当服务器为Linux(Ubuntu)时,ODOO打印的报表上是有条码的,却显示空白框框.问题在于服务器上没有安装条码的字体,reportlab渲染条码图形失败,导致显示不正常. 将附件中的字体下载,解 ...

  9. HDU1892二维树状数组

    See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Su ...

  10. SSH框架中 Spring设置定时器 Quartz

    一,首先下载quartz-1.6.0.jar架包,到lib目录下 二,写你自己定时器业务方法 package com.lbnet.lzx.timing; import org.quartz.JobEx ...