FastPrepLongest Repeating Character Replacement
Problems/Longest Repeating Character Replacement

Longest Repeating Character Replacement

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report identifies this task and its central operation. This practice version states the deterministic interface, boundary behavior, and ordering rules needed for consistent execution while preserving the reported capability.

You are given a string s containing only uppercase English letters and an integer k. You may replace at most k characters in s with any other uppercase English letter.

Return the length of the longest substring that can contain only one repeated letter after at most k replacements.

Function

characterReplacement(s: String, k: int) → int

Examples

Example 1

s = "ABAB"k = 2return = 4

Replace both occurrences of A with B, or both occurrences of B with A. The entire string then contains one repeated letter.

Example 2

s = "AABABBA"k = 1return = 4

The substring AABA can become AAAA with one replacement, so a length of 4 is possible.

Constraints

  • 1 <= s.length <= 100000
  • s contains only uppercase English letters.
  • 0 <= k <= s.length

More Microsoft problems

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int characterReplacement(String s, int k) {
        // Write your code here.
    }
}
Checking account…