Skip to content
Katabench
Try free

Longest Repeating Character Replacement

Medium About 20 min Free, no account needed

Longest uniform run you can create with k character replacements.

The brief

Given a string s of uppercase letters and an integer k, you may replace at most k characters. Return the length of the longest substring consisting of one repeated letter you can produce.

The performance bar

A window qualifies when windowLength - mostFrequentLetterInWindow <= k. The obvious approach re-counts letter frequencies for every window it tries, O(n2). The hidden test is a 40,000-character string whose best window spans nearly all of it, so it's too slow. You will need a solution that runs in O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return the longest one-letter run achievable with at most k replacements.
    public int CharacterReplacement(string s, int k)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Sliding window
Grow and shrink a contiguous range while maintaining its state incrementally.

Related katas