KatInterview

424. Longest Repeating Character Replacement

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the length of the longest substring of a given string s that can be made into a string of a single repeating character by changing at most k characters.

Solution

Longest Repeating Character Replacement in Python

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        counts = [0] * 26
        left = 0
        max_freq = 0
        max_length = 0
        
        for right in range(len(s)):
            # Increment count for the character at the right pointer.
            char_index = ord(s[right]) - ord('A')
            counts[char_index] += 1
            
            # Update max_freq, which is the max frequency of a single character seen in any window.
            max_freq = max(max_freq, counts[char_index])
            
            # The number of characters to change is window's length minus max_freq.
            # If this is more than k, the window is invalid, so we shrink it.
            # A window is valid if (right - left + 1) - max_freq <= k
            if (right - left + 1) - max_freq > k:
                counts[ord(s[left]) - ord('A')] -= 1
                left += 1
            
            # Update the maximum length found so far.
            # The window size `right - left + 1` is the current maximum possible length.
            max_length = max(max_length, right - left + 1)
            
        return max_length

Longest Repeating Character Replacement in Java

class Solution {
    public int characterReplacement(String s, int k) {
        int[] counts = new int[26];
        int left = 0;
        int maxFreq = 0;
        int maxLength = 0;
        
        for (int right = 0; right < s.length(); right++) {
            // Increment the count of the character at the right pointer.
            counts[s.charAt(right) - 'A']++;
            // Update maxFreq, which is the max frequency of a single character seen in any window.
            maxFreq = Math.max(maxFreq, counts[s.charAt(right) - 'A']);
            
            // The number of characters to change is window's length minus maxFreq.
            // If this is more than k, the window is invalid, so we shrink it.
            // A window is valid if (right - left + 1) - maxFreq <= k
            if ((right - left + 1) - maxFreq > k) {
                counts[s.charAt(left) - 'A']--;
                left++;
            }
            
            // Update the maximum length found so far.
            // The window size `right - left + 1` is the current maximum possible length.
            maxLength = Math.max(maxLength, right - left + 1);
        }
        
        return maxLength;
    }
}

Longest Repeating Character Replacement in C++

class Solution {
public:
    int characterReplacement(string s, int k) {
        vector<int> counts(26, 0);
        int left = 0;
        int maxFreq = 0;
        int maxLength = 0;
        
        for (int right = 0; right < s.length(); ++right) {
            // Increment count for the character at the right pointer.
            counts[s[right] - 'A']++;
            // Update maxFreq, which is the max frequency of a single character seen in any window.
            maxFreq = max(maxFreq, counts[s[right] - 'A']);
            
            // The number of characters to change is window's length minus maxFreq.
            // If this is more than k, the window is invalid, so we shrink it.
            // A window is valid if (right - left + 1) - maxFreq <= k
            if ((right - left + 1) - maxFreq > k) {
                counts[s[left] - 'A']--;
                left++;
            }
            
            // Update the maximum length found so far.
            // The window size `right - left + 1` is the current maximum possible length.
            maxLength = max(maxLength, right - left + 1);
        }
        
        return maxLength;
    }
};

Longest Repeating Character Replacement in TypeScript

function characterReplacement(s: string, k: number): number {
    const counts: number[] = new Array(26).fill(0);
    let left = 0;
    let maxFreq = 0;
    let maxLength = 0;
    
    for (let right = 0; right < s.length; right++) {
        // Increment count for the character at the right pointer.
        const rightCharIndex = s.charCodeAt(right) - 'A'.charCodeAt(0);
        counts[rightCharIndex]++;
        
        // Update maxFreq, the max frequency of a single character seen in any window.
        maxFreq = Math.max(maxFreq, counts[rightCharIndex]);
        
        // The number of characters to change is window's length minus maxFreq.
        // If this is more than k, the window is invalid, so we shrink it.
        // A window is valid if (right - left + 1) - maxFreq <= k
        if ((right - left + 1) - maxFreq > k) {
            const leftCharIndex = s.charCodeAt(left) - 'A'.charCodeAt(0);
            counts[leftCharIndex]--;
            left++;
        }
        
        // Update the maximum length found so far.
        // The window size `right - left + 1` is the current maximum possible length.
        maxLength = Math.max(maxLength, right - left + 1);
    }
    
    return maxLength;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.