Longest Repeating Character Replacement - Problem

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

example_1.py โ€” Basic Case
$ Input: s = "ABAB", k = 2
โ€บ Output: 4
๐Ÿ’ก Note: Replace the two 'A's with 'B's (or vice versa) to get "BBBB" or "AAAA". The entire string becomes the same character with exactly 2 replacements.
example_2.py โ€” Partial Replacement
$ Input: s = "AABABBA", k = 1
โ€บ Output: 4
๐Ÿ’ก Note: Replace one 'B' with 'A' in the substring "AABA" to get "AAAA" (length 4). This is better than replacing one 'A' with 'B' to get "BBB" (length 3).
example_3.py โ€” No Replacement Needed
$ Input: s = "AAAA", k = 0
โ€บ Output: 4
๐Ÿ’ก Note: The string already consists of the same character, so no replacements are needed. The entire string length is 4.

Visualization

Tap to expand
๐Ÿ” Sliding Window VisualizationString: "AABABBA", k = 1Positions:AABABBA0123456Step 1: Window [0,2]A A BLRFrequencies:A: 2, B: 1Max freq = 2Replacements = 3-2 = 1 โ‰ค k โœ“Step 2: Expand to [0,3]A A B ALRFrequencies:A: 3, B: 1Max freq = 3Replacements = 4-3 = 1 โ‰ค k โœ“๐ŸŽฏ Result: Maximum window length = 4
Understanding the Visualization
1
Start Small
Begin with a tiny window at the start of the string
2
Expand Greedily
Keep growing the window as long as replacements โ‰ค k
3
Track the Winner
Always know which character appears most in your window
4
Shrink When Needed
If window becomes invalid, slide the left side
5
Remember the Best
Keep track of the largest valid window seen
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique transforms an O(nยณ) brute force solution into an elegant O(n) algorithm by maintaining a dynamic window that expands and contracts based on the validity condition.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through string with two pointers, each character visited at most twice

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Hash map stores at most 26 uppercase English characters (constant space)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of only uppercase English letters
  • 0 โ‰ค k โ‰ค s.length
Asked in
Meta 45 Amazon 38 Microsoft 32 Google 28
42.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen