15% off all plans & roadmaps Use code
See plansLeetCode 424 · Solution & editorial
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.
Longest Repeating Character Replacement is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
The reference solution runs in O(n) time and O(1) space.
Longest Repeating Character Replacement has been reported in interviews at Google, Amazon, Wish.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Longest Repeating Character Replacement is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.