Minimum Window Subsequence - Problem

Given two strings s1 and s2, return the minimum contiguous substring of s1 such that s2 is a subsequence of this substring.

If no such window exists in s1 that covers all characters of s2, return an empty string "".

If there are multiple such minimum-length windows, return the one with the leftmost starting index.

Note: A subsequence is a sequence that can be derived from another sequence by deleting some (not necessarily consecutive) elements without changing the order of the remaining elements.

Input & Output

Example 1 — Basic Case
$ Input: s1 = "adbec", s2 = "abc"
Output: "adbec"
💡 Note: The minimum window substring "adbec" contains "abc" as a subsequence: a→d→b→e→c matches a→b→c by taking positions 0, 2, 4.
Example 2 — No Valid Window
$ Input: s1 = "adc", s2 = "abc"
Output: ""
💡 Note: There is no subsequence "abc" in "adc" because 'b' is missing, so return empty string.
Example 3 — Multiple Windows
$ Input: s1 = "abacbcab", s2 = "abc"
Output: "abc"
💡 Note: Multiple valid windows exist: "abacbc" and "abc". We return "abc" as it's shorter and leftmost among equal-length options.

Constraints

  • 1 ≤ s1.length ≤ 2000
  • 1 ≤ s2.length ≤ 100
  • s1 and s2 consist of lowercase English letters only

Visualization

Tap to expand
Minimum Window Subsequence ProblemInput String s1:adbecTarget Subsequence s2:abcFinding minimum window: "adbec"Result Window: "adbec" (length 5)Contains "abc" as subsequence at positions 0→2→4
Understanding the Visualization
1
Input Analysis
Given s1 and target subsequence s2
2
Find Valid Windows
Identify all substrings containing s2 as subsequence
3
Return Minimum
Select shortest window, leftmost if tied
Key Takeaway
🎯 Key Insight: Use expand-contract sliding window technique to efficiently find minimum valid windows
Asked in
Google 25 Facebook 20 Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~35 min Avg. Time
865 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen