Find the length of the longest substring that can be turned into a string of identical characters by replacing at most k characters.
Problem
You are given a string s consisting of uppercase English letters and an integer k.
In one move, you may choose any character in a substring and replace it with any other uppercase English letter.
Return the length of the longest substring that can be converted into a string where every character is the same after performing at most k replacements.
Goal
Find the maximum possible length of a contiguous substring that can be made uniform using no more than k changes.
Input Format
- A string
sof uppercase English letters. - An integer
krepresenting the maximum number of replacements allowed.
Output Format
- Return a single integer: the maximum length of a valid substring.
Constraints
1 <= s.length <= $10^{5}$0 <= k <= s.lengthscontains only uppercase English lettersA-Z.
Example 1
Input
s = "ABAB", k = 2
Output
4
Explanation
Replace both A characters with B, or both B characters with A, so the whole string becomes uniform.
Example 2
Input
s = "AABABBA", k = 1
Output
4
Explanation
One optimal choice is the substring AABA. Replace one B with A to make it AAAA.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.