Longest Repeating Character Replacement
MediumExtra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on the hints if you get stuck.
Question
You have a string of lowercase letters and a number k. You're allowed to swap out up to k letters in the string for any other lowercase letter you choose.
After making your swaps, find the length of the longest block of identical letters you can create 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" to turn the last five letters into "bbbbb", a block of length 5.
Input: input = "abcde", k = 1
Output: 2
With only one swap available, the best you can do is turn two neighboring letters into a match, such as swapping "b" for "a" to get "aa".
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Take a moment to understand the problem and think of your approach before you start coding.