Back to DSA

Longest Repeating Character Replacement

medium
Acceptance: 44%
StringsSliding Window

A string of uppercase English letters and an integer k are given. You may change up to k characters in the string to any other uppercase letter. After making at most k replacements, what is the length of the longest substring where every character is the same?

Examples

Example 1:
Input:s = "XYXY", k = 1
Output:3
Explanation: Change one 'Y' to 'X' (or vice versa) to obtain a run of three identical characters, e.g., 'XXX' within 'XXXY'.
Example 2:
Input:s = "AABABBA", k = 2
Output:5
Explanation: Replace two B's to get 'AAAAABA' or similar. The longest uniform segment is 5.

Hints

00:00
1234567