← All problems

Longest Repeating Character Replacement

medium

Given a string s and an integer k, you may replace up to k characters with any other uppercase letter. Return the length of the longest substring you can make consist of a single repeated letter.

Track the count of the most frequent letter inside the window — once (window size - that count) exceeds k, too many characters would need replacing, so shrink from the left.

Example

Input: s = "ABAB", k = 2
Output: 4        // replace both B's (or both A's)
Loading...