Longest Single-Letter Substring After at Most k Letter Replacements
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: A string problem: given uppercase letters and a budget of k single-letter changes, find the longest contiguous substring that can be turned into one repeated letter. It tests efficient substring scanning with letter counts and careful reasoning about when a candidate substring stays valid.
Read the full Microsoft Software Engineer interview experience this question came from
Constraints
- 1 <= len(s) <= 100000
- s contains only the uppercase letters 'A' to 'Z'
- 0 <= k <= len(s)
- The result is an integer from 1 to len(s) inclusive
Examples
Input: ('ABBCB', 1)
Expected Output: 4
Input: ('AAAB', 0)
Expected Output: 3
Hints
- For one fixed substring, the fewest operations needed equals its length minus the count of its most frequent letter.
- If a substring can be made uniform within k operations, so can every substring inside it. Think about a window that grows from the right and only moves its left edge when it becomes infeasible.
- The alphabet has only 26 letters, so per-letter counts for the current window are cheap to maintain.