125. Longest Repeating Character Replacement

Medium · String

You are given a string `s` of uppercase letters and an integer `k` representing the maximum number of character replacements you can make. Find the length of the longest substring that consists of a single repeated character, after making at most `k` replacements.

For example, in "ABAB" with k=2, you can replace the two 'B's to get "AAAA" (length 4). In "AABABBA" with k=1, you can replace one 'B' to get "AAAA" (length 4).

Input: An array `[s, k]` where `s` is a string of uppercase letters and `k` is a non-negative integer.

Return: A single integer — the length of the longest substring of repeated characters achievable with at most `k` replacements.

Examples

Example 1
Input: ["ABAB", 2]
Output: 4
Explanation: Replace the two Bs (or two As) to make "AAAA"
Example 2
Input: ["AABABBA", 1]
Output: 4
Explanation: Best 4-window is "ABBA" → "AAAA" or "BBBB" with one replacement

Constraints