DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Sliding Window

Longest Repeating Character Replacement

**Problem Statement:** You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return the length of the longest substring containing the same letter you can get after performing the above operations. **Hint / Expected Approach:** Window size − max freq ≤ k **Edge Cases to Consider:** - (1) k = 0 (no replacement) - (2) k ≥ string length - (3) All same characters

Examples

Example 1
Input: "ABAB", 2
Output: 4
Example 2
Input: "AABABBA", 1
Output: 4
Example 3
Input: "A", 0
Output: 1

Constraints

  • ▪1 <= data.length <= 10^5
  • ▪Memory complexity must be O(1) or O(k)
  • ▪Ensure non-overlapping pointer access

Watch Out For Edge Cases

  • ▪k = 0 (no replacement)
  • ▪k ≥ string length
  • ▪All same characters
Frequently Asked At
GoogleAmazonMeta