11
Given a string and a set of required characters, find the smallest substring that contains them all.
Tap to write answer
0 words | 0 charsPress Enter ↵ to reveal
Your Attempt
0 wordsRefined Model Answer
ReferenceI would use a sliding window with two pointers and a frequency map. I would expand the right side until the window contains all required characters, then shrink the left side as much as possible while keeping the window valid. The reason I choose this approach is that it processes each character a limited number of times, so it stays linear. I would also keep track of how many required characters are still missing, because that makes validity checks easier. Edge cases include no valid window, repeated required characters, and an empty input string.