Longest repeating character replacement Problem

Longest repeating character replacement Problem — ExecCode Medium DSA Practice

Solve the Longest repeating character replacement problem on ExecCode. Free online medium DSA practice in Sliding Window. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an uppercase string s and an integer k, return the length of the longest substring that can be changed into one repeated character by replacing at most k characters. In a window, the best target letter is the most frequent letter already present. The number of replacements needed is window length minus maxFreq. Expand right while tracking character counts, and shrink left whenever replacements needed becomes greater than k.

Examples

Input {"s": "AABABBA", "k": 1}; Output 4. Input {"s": "ABAB", "k": 2}; Output 4. Input {"s": "AAAA", "k": 0}; Output 4

Constraints

1 ≤ s.length ≤ 10⁵ s consists of only uppercase English letters 0 ≤ k ≤ s.length

Practice Longest repeating character replacement free on ExecCode. Browse DSA problems, topic map, and placement guides.