Understanding the Problem
We are given a string s
that contains only uppercase English letters and an integer k
. Our task is to find the length of the longest substring where we can change at most k
characters so that all characters in that substring become the same.
This is a classic sliding window problem. The goal is to find a window (substring) that, after making at most k
changes, becomes a block of repeating characters.
Intuition: If we know how many times the most frequent character appears in our current window, and the remaining characters can be changed within k
operations, then this window is valid.
Step-by-Step Solution with Example
Step 1: Initialize Variables
We keep a frequency array (or hashmap) to count characters in the current window. We'll use two pointers left
and right
to create a sliding window. Also, track maxCount
, which is the frequency of the most frequent character in the window.
Step 2: Iterate with Right Pointer
We expand the window by moving right
. For each character, we update its count and check if the window is still valid. A window is valid if:
(window length) - (maxCount) <= k
This condition tells us if we can convert all other characters to the most frequent one within k
changes.
Step 3: Shrink the Window if Invalid
If the condition is not met, we shrink the window from the left and update frequencies accordingly.
Step 4: Track the Maximum Length
At each step, update maxLen
with the size of the current valid window.
Example:
s = "AABABBA", k = 1
- Start with
left = 0
, right = 0
, window = "A", maxCount = 1 → valid → maxLen = 1
- right = 1 → window = "AA", maxCount = 2 → valid → maxLen = 2
- right = 2 → window = "AAB", maxCount = 2 → (3 - 2 = 1) ≤ k → valid → maxLen = 3
- right = 3 → window = "AABA", maxCount = 3 → (4 - 3 = 1) ≤ k → valid → maxLen = 4
- right = 4 → window = "AABAB", maxCount = 3 → (5 - 3 = 2) > k → invalid → move left to 1 → window = "ABAB"
- window becomes valid again → maxLen still 4
- right = 5 → window = "ABABB", maxCount = 3 → (5 - 3 = 2) > k → move left to 2 → window = "BABB"
- still invalid → move left to 3 → window = "ABB"
- right = 6 → window = "ABBA", maxCount = 2 → (4 - 2 = 2) > k → move left to 4 → window = "BBA"
Answer remains 4 for the window "AABA"
Edge Cases
- All characters same: No need to replace anything, so answer is length of the string. Example: s = "AAAA", k = 2 → Output: 4
- No characters to replace (k = 0): Just look for the longest block of identical characters. Example: s = "ABAB", k = 0 → Output: 1
- Empty string: s = "", k = 2 → Output: 0
- k greater than string length: Can convert entire string. Example: s = "ABC", k = 5 → Output: 3
Final Thoughts
This problem is an excellent example of the sliding window technique. By keeping track of the most frequent character in the current window and adjusting the window when necessary, we ensure an efficient solution that runs in linear time. The key idea is that we don't need to actually perform replacements — just count if we could do it within k
changes.
Always test with edge cases like empty strings, k = 0, and strings with all the same characters to validate your logic.
Comments
Loading comments...