Blind 75
14 / 75Free practice

Longest Repeating Character Replacement

Medium · Sliding window

Jump to code

For a string of uppercase English letters and a budget k, find the longest substring that can become one repeated letter after changing at most k positions.

Examples

Example 1

s = "ABAB"
k = 2

Output: 4

Example 2

s = "AABABBA"
k = 1

Output: 4

Constraints

  • 1 <= s.length <= 1000
  • 0 <= k <= s.length
Need a hint?

Required replacements equal window length minus its largest letter count.

Asked in real interviews

Candidates reported versions of Longest Repeating Character Replacement from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 9 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "ABAB",
  2
]
Expected
4
Case 2
Input
[
  "AABABBA",
  1
]
Expected
4
Case 3
Input
[
  "AAAA",
  0
]
Expected
4
Case 4
Input
[
  "ABCD",
  1
]
Expected
2
Case 5
Input
[
  "AABBB",
  2
]
Expected
5

Drafts are saved per language in this browser when storage is available.