Longest Repeating Character Replacement
Find the longest substring of identical letters achievable with at most k character replacements using a sliding window and max frequency tracker.
01The problem
You are given a string s consisting of only uppercase English letters and an integer k. You can choose any character of the string and change it to any other uppercase English character at most k times. Return the length of the longest substring containing the same letter you can get after performing the above operations.
| Input | Output | Why |
|---|---|---|
s = "ABAB", k = 2 | 4 | Replace the two 'A's with 'B's (or vice versa) to get "BBBB" |
s = "AABABBA", k = 1 | 4 | Replace the middle 'B' to form "AABA" → "AAAA" |
s = "AAAA", k = 2 | 4 | Already uniform, no changes needed |
s = "ABBB", k = 0 | 3 | k = 0 means no changes allowed; "BBB" has length 3 |
Constraints: 1 to 100,000 characters, 0 ≤ k ≤ length, uppercase English letters only.
Loading the rest of the lesson…