[LeetCode] 424. Longest Repeating Character Replacement (C++)


Source of topic: https://leetcode-cn.com/problems/longest-repeating-character-replacement/

Title description

Give you a string consisting of only uppercase English letters. You can replace the character at any position with another character, and you can replace it up to k times in total. After performing the above operations, find the length of the longest substring containing repeated letters.

Note: The length of the string and k cannot exceed 10^4.

示例 1:

输入:s = "ABAB", k = 2
输出:4
解释:用两个'A'替换为两个'B',反之亦然。
示例 2:

输入:s = "AABABBA", k = 1
输出:4
解释:
将中间的一个'A'替换为'B',字符串变为 "AABBBBA"。
子串 "BBBB" 有最长重复字母, 答案为 4

General idea

  • Given a string, the characters in the interval can be replaced K times. After the replacement is performed, the length of the longest string containing repeated letters is found, the classic double pointer + sliding window problem

Double pointer

  • To find the length of the longest string containing repeated letters, at most k replacements are required, and the meaning of the question can be changed. A substring can have up to k different characters, and the length of the string at this time is obtained. Pay attention to During the process of sliding from left to right, there is no need to update the maximum number of characters in the interval [left, right] curMax
const int MAXN = 50;
int cnt[MAXN];
class Solution {
    
    
public:
    int characterReplacement(string s, int k) {
    
    
        memset(cnt, 0, sizeof(cnt));
        int left = 0, curMax = 0, ans = 0;
        int len = s.length();
        for(int right = 0 ; right < len ; ++right){
    
    
            int index = s[right] - 'A';
            ++cnt[index];
            if(cnt[index] >= curMax)
                curMax = cnt[index];
            while(curMax + k < right - left + 1){
    
    
                --cnt[s[left] - 'A'];
                ++left;
            }
            ans = max(ans, right - left + 1);
        }
        return ans;
    }
};

Complexity analysis

  • Time complexity: O(n). n is the length of the array, both left and right can be regarded as sliding n times
  • Space complexity: O(n). Maintain a count array cnt as a global variable

Specific details

  • Before looking at the official solution, I manually maintained the maximum number of repeated characters in the interval [left, right], which resulted in the code taking 400ms even if it passed. It was realized after reading the official explanation and proof.
  • The questions and explanations raised by the official solution are quoted here

The definition of maxCount is not maintained during the process of "moving the left boundary one position to the right" in the inner loop. Is the conclusion correct?

  • Answer: The conclusion is still correct. When "the left boundary moves one position to the right", maxCount is either unchanged or the value is reduced by 1. Although the value of maxCount is not maintained, the value of the array freq is properly maintained;
  • Before "Left Boundary Move to Right": If there are two characters of equal length, moving the left boundary to the right does not change the value of maxCount. For example, s = [AAABBB], k = 2, after the left border A is removed, the number of characters in the window does not change , which is still 3;
  • If the left boundary is removed, the value of maxCount becomes smaller at this time, and because what we are looking for is only the length of the repeated substring after the longest replacement k times. Next, we continue to move the right border one square to the right. There are two situations:
    • ① If the right boundary reads the character that just moved out of the left boundary, it happens that the value of maxCount is correctly maintained;
    • ② On the right boundary, if you read a character that is not just shifted out of the left boundary 新的子串要想在符合题意的条件下变得更长, maxCount must be more than the previous value, so you will not miss a better solution.

Guess you like

Origin blog.csdn.net/lr_shadow/article/details/113963254