Z20

Longest Repeating Character Replacement

Staff / Principal Engineer
Target Time: 40 - 50 Minutes
Type: NUMERIC

Problem Statement & Objective

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

Function Signature (Python)

character_replacement(s: str, k: int) -> int

Input & Output Contract

Output Rule:Maximum substring length achievable.

System Constraints

1 <= s.length <= 10^5
s consists of only uppercase English letters
0 <= k <= s.length

Example Execution Walkthrough

s = "AABABBA", k = 1
result: 4 (replace 'B' at index 2 to get "AAAA")

Example Test Scenarios (3)

Example 1
Input:["ABAB",2]
Expected Output:4
Example 2: 'AABA' ya da 'ABBA'
Input:["AABABBA",1]
Expected Output:4
Example 3: Değişimsiz
Input:["AAAA",0]
Expected Output:4
Loading Python Editor...
Input Parameters
[
  "ABAB",
  2
]
Expected Output
4