Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Longest Repeating Character Replacement

Medium
sliding-windowstringhash-table

You are given a string s of uppercase English letters and an integer k. You may replace at most k characters with any other letter. Return the length of the longest substring containing a single repeated letter achievable this way.

Input

  • Line 1: string s
  • Line 2: integer k

Output: the maximum length.

Example 1
Input:
ABAB
2
Output:
4

Replace both A's (or both B's) → AAAA.

Example 2
Input:
AABABBA
1
Output:
4

Replace the middle A → AABBBBA, window BBBB.

Constraints

  • 1 ≤ s.length ≤ 10^5
  • 0 ≤ k ≤ s.length
  • Uppercase English letters only.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.