Imagine you're a detective looking through a long document (string s1) trying to find the shortest paragraph that contains all the clues you need in the exact order specified by your clue sequence (string s2).
Given two strings s1 and s2, find the minimum contiguous substring of s1 such that s2 is a subsequence of this substring. A subsequence means the characters appear in the same order but don't need to be consecutive.
Goal: Return the shortest window in s1 that contains s2 as a subsequence. If multiple windows have the same minimum length, return the leftmost one. If no such window exists, return an empty string.
Example: If s1 = "abcdebdde" and s2 = "bde", the answer is "bcde" because it's the shortest substring where we can find 'b', 'd', 'e' in that order.
Input & Output
Visualization
Time & Space Complexity
m iterations of the outer loop, each doing O(n) work to scan s1
Only using constant extra space for pointers and variables
Constraints
- 1 โค s1.length โค 2 ร 104
- 1 โค s2.length โค 100
- s1 and s2 consist of lowercase English letters only
- If no valid window exists, return empty string
- If multiple minimum windows exist, return the leftmost one