#C5383. Longest Repeating Character Replacement
Longest Repeating Character Replacement
Longest Repeating Character Replacement
You are given a string s
and an integer k
. Your task is to determine the length of the longest substring you can obtain from s
by replacing at most k
characters so that all the characters in the substring are the same.
For a substring of length L with a most frequent character count of maxCount, the condition for it to be valid after at most k
replacements is:
\(L - \text{maxCount} \le k\)
If the condition is violated, you need to shrink the window. This problem can be efficiently solved using a sliding window approach.
inputFormat
The input is given via stdin in two lines:
- The first line contains the string
s
. - The second line contains the integer
k
, representing the maximum number of allowed character replacements.
outputFormat
Output a single integer representing the length of the longest substring that can be obtained after at most k
replacements, printed to stdout.
AABABBA
1
4