Dashboard
#14. Longest Repeating Character ReplacementMediumSliding Window

You are given a string s consisting of only uppercase english characters and an integer k. You can choose up to k characters of the string and replace them with any other uppercase English character.

After performing at most k replacements, return the length of the longest substring which contains only one distinct character.

Example 1:

Input: s = "XYYX", k = 2

Output: 4

Explanation: Either replace the 'X's with 'Y's, or replace the 'Y's with 'X's.

Example 2:

Input: s = "AAABABB", k = 1

Output: 5

Constraints:

  • 1 <= s.length <= 100,000
  • 0 <= k <= s.length
  • s consists of only uppercase english characters.
Examples
Example 1:
Input: s = "XYYX", k = 2
Output: 4
Example 2:
Input: s = "AAABABB", k = 1
Output: 5
Constraints
  • `1 <= s.length <= 100,000`
  • `0 <= k <= s.length`
  • `s` consists of only uppercase english characters.
Prerequisites
Target: O(N) time • O(1) spaceHash Table / String / Sliding Window
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.