Longest Repeating Character Replacement
MediumThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
You have a string of lowercase letters and a number k. You can swap up to k letters in the string for any other lowercase letter you like.
After your swaps, find the length of the longest block of matching letters you can make anywhere in the string.
Input: input = "abab", k = 2
Output: 4
Swap both "b" letters for "a" (or both "a" letters for "b") to turn the whole string into one block of matching letters.
Input: input = "aabccbb", k = 2
Output: 5
Swap the two "c" letters for "b". That turns the last five letters into "bbbbb", a block of length 5.
Input: input = "abcde", k = 1
Output: 2
With only one swap, the best you can do is make two neighboring letters match. For example, you can swap "b" for "a" to get "aa".
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Remember to understand the problem and plan your approach before you start coding.