博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
G 面经 && Leetcode: Longest Repeating Character Replacement
阅读量:4538 次
发布时间:2019-06-08

本文共 4001 字,大约阅读时间需要 13 分钟。

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 = 2Output:4Explanation:Replace the two 'A's with two 'B's or vice versa.Example 2:Input:s = "AABABBA", k = 1Output:4Explanation: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.

 

1 public class Solution { 2     public int characterReplacement(String s, int k) { 3         int[] count = new int[26]; 4         int res = 0; 5         int start = 0; 6         int maxCharCount = 0; 7         char maxChar = '\0'; 8         for (int end=0; end
k) {16 count[s.charAt(start)-'A']--;17 if (maxChar == s.charAt(start)) {18 maxCharCount--;19 for (int i=0; i<26; i++) {20 if (maxCharCount < count[i]) {21 maxCharCount = count[i];22 maxChar = (char)('A' + i);23 }24 }25 }26 start++;27 }28 res = Math.max(res, end-start+1);29 }30 return res;31 }32 }

 

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: (Preferred solution)

1 import collections 2 def findLongestSubstring(s,k): 3     res=0 4     left=0 5     counts = collections.Counter() 6     for right in range(0,len(s)): 7         counts[s[right]] += 1 8         char_w_maxCount = counts.most_common(1)[0][1] 9         while right - left - char_w_maxCount + 1  > k:10             counts[s[left]] -= 111             char_w_maxCount = counts.most_common(1)[0][1]12             left +=113             14         res = max(res,right-left+1)15             16     return res17 18     19 print(findLongestSubstring("abab",2))                420 print(findLongestSubstring("aabbbba",1))        521 print(findLongestSubstring("abcaea",1))        322 print(findLongestSubstring("aababba",1))        423 print(findLongestSubstring("abcae",2))        424 print(findLongestSubstring("aaaaa",2))        525

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6133692.html

你可能感兴趣的文章
【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
查看>>
Yii DataProvider
查看>>
BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
查看>>
hdu 1114 Piggy-Bank
查看>>
maven集成tomcat插件启动报错
查看>>
Boost库编译安装
查看>>
算法复习——数位dp(不要62HUD2089)
查看>>
Spark2.1.0——运行环境准备
查看>>
Codeforces 543.B Destroying Roads
查看>>
noip模拟赛 寻宝之后
查看>>
ZOJ2833*(并查集)
查看>>
外连接简要总结
查看>>
第一次作业-准备篇
查看>>
【C++】继承时构造函数和析构函数
查看>>
opencv源代码之中的一个:cvboost.cpp
查看>>
swift
查看>>
pycharm 快捷键
查看>>
Linux常用命令
查看>>
.net中的设计模式---单例模式
查看>>
安装程序工具 (Installutil.exe)22
查看>>