Longest Repeating Character Replacement
You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
Examples
s = "ABAB", k = 24s = "AABABBA", k = 14Constraints
1 <= s.length <= 10^5s consists of only uppercase English letters0 <= k <= s.length
š” Hints
Hint 1
Use a sliding window and track the frequency of each character inside the window.
Hint 2
The key insight: windowSize - maxFreqInWindow <= k means the window is valid.
Hint 3
If invalid, shrink from the left.
Algorithm Complexity
Official asymptotic Big-O runtime and auxiliary memory analysis
Sliding window tracking character frequencies. Window is valid if (windowSize - maxFreq) <= k. If invalid, shrink left. Track maximum valid window size.
The right window boundary advances across string of length n while the left boundary only slides forward, visiting each char at most twice.
A frequency count map/array stores at most 26 uppercase English letters, consuming O(1) fixed space.
š Real-World Use Cases
Finding the longest segment of a DNA strand that can be made uniform with at most k mutations ā directly maps to this problem.
š Dry Run
Initialize. Input: 'AABABBA', k=1.