FunctionRacerProblemsRace

Shortest Stretch With Every Required Letter

MediumTwo pointersPython, JavaScript, TypeScript, Java, C++

letters is a string of distinct lowercase letters. Return the length of the shortest contiguous piece of text that contains every letter of letters at least once. Extra letters inside the piece are fine, and the order of the letters does not matter.

Return 0 if no piece contains them all, including when text is empty.

Example 1

text "xaybzcabc"
letters "abc"
returns 3

The pieces cab (positions 5 to 7) and abc (positions 6 to 8) both have all three letters; nothing shorter does.

Example 2

text "aaa"
letters "ab"
returns 0

There is no b anywhere.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems