You may change at most k characters of s to any other uppercase letter. Return the length of the longest substring made of a single repeated letter you can get.
Grow the window on the right. If it would need more than k replacements (length − max count > k), slide the left edge forward. maxCount never has to decrease: only a larger maxCount can produce a longer answer.
1function characterReplacement(s: string, k: number): number {2const count = new Map<string, number>();3let lo = 0, maxCount = 0, best = 0;4for (let hi = 0; hi < s.length; hi++) {5count.set(s[hi], (count.get(s[hi]) ?? 0) + 1);6maxCount = Math.max(maxCount, count.get(s[hi])!);7if (hi - lo + 1 - maxCount > k) {8count.set(s[lo], count.get(s[lo])! - 1);9lo++;10}11best = Math.max(best, hi - lo + 1);12}13return best;14}
Empty window.
Space: play/pause · ←/→: step