Skip to content
Katabench
Try free

Minimum Window Substring (Length)

Hard About 35 min Free, no account needed

Length of the shortest window of text covering every required character.

The brief

Given strings text and chars, return the length of the shortest contiguous window of text that contains every character of chars (including duplicates), or 0 when no such window exists.

The performance bar

The obvious approach expands a fresh window from every starting position, recounting as it goes, O(n2). In the hidden test the only valid window spans nearly all of a 40,000-character string, so the quadratic version is too slow. You will need an approach where the work stays close to O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return the length of the shortest window of text covering all of chars (with multiplicity); 0 if none.
    public int MinWindowLength(string text, string chars)
    {
        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.
Hash maps and sets
Trade memory for constant-time lookups, membership tests and frequency counts.

Related katas