Shortest Stretch With Every Required Letter
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
0 <= len(text) <= 601 <= len(letters) <= 10textandlettershave lowercase letters only- the letters of
lettersare all different
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Sort ColorsMedium
- Stretches With a Sum in RangeMedium
- Subarrays With Small ProductMedium
- Three SumMedium
- Trim Both Ends to a TotalMedium
- Valid Palindrome IIMedium
- Valid Triangle NumberMedium
- Water Between the WallsMedium