Identical Letters
InterviewTime limit2sMemory limit1024 MB
Given a lowercase string and an integer m, find the longest run of one repeated letter you can form by deleting at most m letters.
- Level
Medium4 of 10
- Topics
- Sliding window, String, Two pointers
- Solved
- No attempts yet
Problem
Computer scientists are always interested in streaks of identical letters, so they look for the longest consecutive run of the same letter in a string.
Given a string of lowercase letters and an integer , find the maximum number of consecutive identical letters in when you remove up to letters from the string. You do not have to remove exactly letters.
Input
The first line contains the string , which contains only lowercase letters. Its length is at least and at most .
The second line contains an integer (), the maximum number of letters you can remove.
Output
Print the maximum number of consecutive identical letters that you can get after removing up to letters from the string.
Hint
For the first sample, you can remove the two letters at positions 10 and 11.
For the second sample, you can remove the letters at positions 3, 4, 6, 10, and 11.
For the third sample, you can create a run of two a's or two y's, so the answer is 2.