Imagine you have a string of uppercase English letters and a magic wand that can transform any character into any other uppercase letter. You can use this wand at most k times.
Your goal is to create the longest possible substring where all characters are identical. For example, with string "AABABBA" and k = 1, you could change one 'B' to 'A' to get "AAABABA", giving you a substring "AAA" of length 3.
What's the length of the longest substring of identical characters you can achieve?
This is a classic sliding window problem that tests your ability to efficiently track character frequencies while maintaining an optimal window size.
Input & Output
Visualization
Time & Space Complexity
Single pass through string with two pointers, each character visited at most twice
Hash map stores at most 26 uppercase English characters (constant space)
Constraints
- 1 โค s.length โค 105
- s consists of only uppercase English letters
- 0 โค k โค s.length