🔗 Problem Link
LeetCode 424 – Longest Repeating Character Replacement
👉 https://leetcode.com/problems/longest-repeating-character-replacement/
Introduction
When I first looked at this problem, it didn’t immediately feel like a typical sliding window question. It talks about replacing characters, not finding substrings directly.
But the moment you carefully read:
"Return the length of the longest substring containing the same letter after at most k replacements."
That’s when the pattern becomes clear.
This is a variable-size sliding window problem where we try to maintain a valid window under a certain condition.
In this blog, I’ll explain:
- The intuition behind the problem
- Why sliding window works
- The key tricky condition
- Step-by-step explanation of the code
- Time and space complexity
📌 Problem Understanding
We are allowed to:
- Pick any character in the string
- Replace it with any uppercase letter
- Perform at most
kreplacements
We need to find the maximum length substring that can become all the same character after at most k changes.
🧠 Key Observation
In any window (substring):
To make all characters the same, we need to:
- Keep the most frequent character
- Replace all other characters
So the number of replacements required is:
If that value is ≤ k, then the window is valid.
This is the most important insight in the entire problem.
🚀 Why Sliding Window?
We are asked for:
- Longest substring
- Under a constraint
- With dynamic window expansion
That is a clear sign of the sliding window pattern.
We use:
i→ left pointerj→ right pointer- HashMap → to track character frequencies
💻 Your Code
🔍 Step-by-Step Explanation
1️⃣ Expanding the Window
We add the current character into the frequency map.
2️⃣ Finding Maximum Frequency
We calculate the highest frequency character inside the window.
Why?
Because that is the character we will keep.
All others will be replaced.
3️⃣ The Most Important Condition (Tricky Part)
Let’s break this:
j - i + 1→ Window sizemaxfreq→ Count of most frequent characterwindowSize - maxfreq→ Replacements needed
If replacements needed > k → window is invalid
So we shrink from left.
4️⃣ Shrinking the Window
We remove left character and move i forward.
5️⃣ Updating Maximum Length
We update our answer whenever the window is valid.
🎯 Why This Works
We always maintain:
That guarantees:
We can convert the entire window into one repeating character using at most k replacements.
⏱ Time and Space Complexity
Time Complexity: O(26 * n) ≈ O(n)
- For each character, we scan at most 26 letters.
- Since there are only uppercase letters, this is effectively linear.
Space Complexity: O(26)
At most 26 uppercase characters stored.
🔥 Important Optimization Insight
Your solution recalculates maxfreq every time using a loop.
A common optimization is:
- Maintain a global
maxfreq - Update only when expanding window
- Do not recalculate during shrinking
That reduces unnecessary scanning.
But your solution is completely correct and efficient.
🏁 Final Thoughts
This problem teaches:
- Sliding Window with condition
- Frequency tracking
- Window validation logic
- Thinking in terms of "how many changes needed"
The key formula to remember:
If you truly understand this condition, you unlock many similar problems like:
- Longest Substring with K Replacements
- Max Consecutive Ones III
- Fruit Into Baskets




