Longest Repeating Character Replacement
Learn how to solve the 'Longest Repeating Character Replacement' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
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 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.
Write a function characterReplacement(s: str, k: int) -> int.
- •1 <= len(s) <= 10^5
- •s consists of only uppercase English letters
- •0 <= k <= len(s)
Examples
s = "ABAB", k = 2
4
Replace the two 'A's with 'B's or vice versa to get "BBBB" or "AAAA". The longest substring is 4.
s = "AABABBA", k = 1
4
Replace the 'B' at index 3 with 'A' to get "AAAAABA". The longest substring of same characters starting from index 0 is "AAAA" with length 4.
Need a Hint?
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.