Longest Repeating Character Replacement
Recognize the pattern
Brute force idea
A straightforward first read of Longest Repeating Character Replacement is this: Check every substring. Each substring independently checked. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
Better approach
The deeper shift in Longest Repeating Character Replacement is this: Sliding window: track the frequency of the most common character in the window. If windowSize - maxFreq > k, shrink from left. Track maximum window size. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.
Key invariant
At the center of Longest Repeating Character Replacement is one steady idea: A window is valid if the number of characters to replace (windowSize - maxFrequencyInWindow) ≤ k. The optimal strategy is to keep the most frequent character and replace the rest. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Watch out for
The trap in Longest Repeating Character Replacement usually looks like this: Recalculating maxFreq from scratch when the window shrinks — you don't need to. An overestimate of maxFreq never causes wrong answers because the window only grows when maxFreq truly increases. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.