Longest Repeating Character Replacement
Problem Statement
You are given a string s of uppercase letters and a number k.
You may pick at most k positions in the string and change the letter at each one to any other uppercase letter.
After your changes, return the length of the longest substring in which every letter is the same.
A string s of uppercase English letters and an integer k.
One integer: the longest run of one repeated letter you can make with at most k changes.
Change both A's to B (or both B's to A) and the whole string is one letter.
Change the A at index 3 to B to get "AABBBBA". The block "BBBB" has length 4. Other choices also give 4.
- • 1 ≤ s.length ≤ 10^5
- • s contains only uppercase English letters
- • 0 ≤ k ≤ s.length
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, grow right while length − maxFreq ≤ k
Grow right; while length − (max of 26 counts) > k, shrink left
Keep the biggest count ever seen; the window slides or grows, never shrinks
Try every start. Grow the end one letter at a time, keeping counts and the most common count maxFreq of the substring. The substring needs length − maxFreq changes. While that is ≤ k, it is a candidate. Once it is > k, stop for this start.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.