LeetCode //C - 424. Longest Repeating Character Replacement

424. Longest Repeating Character Replacement

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.
 

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.
There may exists other ways to achieve this answer too.

Constraints:
  • 1 < = s . l e n g t h < = 1 0 5 1 <= s.length <= 10^5 1<=s.length<=105
  • s consists of only uppercase English letters.
  • 0 <= k <= s.length

From: LeetCode
Link: 424. Longest Repeating Character Replacement


Solution:

Ideas:
  1. Frequency Array: count[26] keeps track of the frequency of each character (since there are 26 uppercase letters).
  2. Max Count: maxCount holds the count of the most frequent character in the current window, which helps us determine whether the window can be valid with up to k replacements.
  3. Sliding Window: We expand the window by moving the right pointer and adjust the left pointer whenever the window becomes invalid (i.e., when more than k replacements are needed).
  4. Updating the Result: The valid window size is calculated at each step, and we track the maximum window size.
Code:
int characterReplacement(char* s, int k) {
    
    
    int count[26] = {
    
    0};  // Frequency of characters in the current window
    int maxCount = 0;     // Maximum frequency of a single character in the current window
    int maxLength = 0;    // Result: the maximum length of a valid window
    int left = 0;         // Left pointer of the sliding window
    
    // Iterate through the string with the right pointer
    for (int right = 0; s[right] != '\0'; right++) {
    
    
        // Increment the count for the current character
        count[s[right] - 'A']++;
        
        // Update the maxCount for the most frequent character in the current window
        if (count[s[right] - 'A'] > maxCount) {
    
    
            maxCount = count[s[right] - 'A'];
        }
        
        // The size of the window is (right - left + 1)
        // If we need to replace more than 'k' characters, we need to shrink the window
        if ((right - left + 1) - maxCount > k) {
    
    
            // Reduce the count of the character at the left pointer and move the left pointer
            count[s[left] - 'A']--;
            left++;
        }
        
        // Calculate the maximum valid window size
        maxLength = (right - left + 1) > maxLength ? (right - left + 1) : maxLength;
    }
    
    return maxLength;
}

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/143103051