Longest Repeating Character Replacement
Medium Sliding Window
s is a string of upper case English letters, and k is a whole number.
You may change any letter of s into any other upper case letter, at most k times in all.
Return the length of the longest stretch of s, taken without gaps, that can be made of one letter repeated with those changes.
Example 1
Input: s = "BCBBACB", k = 1
Output: 4
Changing the C at index 1 into a B gives BBBB at the start, 4 letters long. Every stretch of 5 letters holds at least two letters that differ from its most common one, so one change is not enough for it.
Example 2
Input: s = "MMNNNM", k = 0
Output: 3
Example 3
Input: s = "ZZYZZYZ", k = 2
Output: 7
Constraints
1 ≤ s.length ≤ 10⁵sholds only upper case English letters, A to Z0 ≤ k ≤ s.length
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Sliding Window
- Longest Substring Without Repeating CharactersAI round · Medium
- Sliding Window MaximumAI round · Hard
- Minimum Window SubstringAI round · Hard
- Minimum Size Subarray SumAI round · Medium
- Longest Substring with At Most K Distinct CharactersMedium