Longest Single-Letter Run in a Doc After K Character Edits

Last updated: November 29, 2026

Quick Overview

Given an uppercase string and an integer k, you may replace at most k characters with any letter. Return the length of the longest substring that can be made of one repeated letter.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given an uppercase string and an integer k, you may replace at most k characters with any letter. Return the length of the longest substring that can be made of one repeated letter.

A common Google onsite sliding-window question where the trick is the window validity test: length minus the most frequent letter count must be at most k. The interviewer probes whether the candidate understands why the max frequency does not need to be decreased when the window shrinks.

What the Interviewer Expects
  • States the validity condition `window - max_freq <= k`
  • Uses a 26-letter count array
  • Explains why a stale max frequency still gives the right answer
  • Moves the left edge by one when invalid
  • States O(n) time
Key Topics to Cover
Sliding Window
Frequency Counting
Two Pointers
Amortized Analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the alphabet is all of Unicode?
  • Return the substring and the letter it becomes.
  • What if each replacement has a cost depending on the letter?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: string s of uppercase letters, integer k >= 0.
  • Output: maximum length of a substring that can become a single repeated letter using at most k replacements....
High-Level Design

A window can become all one letter with len - max_count replacements, where max_count is the count of its most common letter. Expand right, update the count of the new letter and max_count. If...


Submit Your Answer
Markdown supported