Longest Repeating Character Replacement

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given a string s and an integer k, you can choose any character of the string and change it to any other uppercase English letter. 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

Example 1:

Input: s = "ABAB", k = 2

Output: 4

Explanation: Replace the two A's with B's (or the two B's with A's) to get a substring of 4 identical characters.

Example 2:

Input: s = "AABABBA", k = 1

Output: 4

Explanation: Replace the B at index 3 with A to get "AAAAABA", giving the substring "AAAA" of length 4.

Example 3:

Input: s = "ABBB", k = 2

Output: 4

Explanation: Replace the A at index 0 with B to get "BBBB". Only 1 change is needed, which is within the allowed k = 2.

Constraints

  • 1 <= s.length <= 10⁵
  • s consists of only uppercase English letters.
  • 0 <= k <= s.length
Source: Sliding Window pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle