G 面经 && Leetcode: Longest Repeating Character Replacement

时间:2021-04-02 02:26:39
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