Minimum Window Subsequence - Problem

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

example_1.py โ€” Basic Example
$ Input: s1 = "abcdebdde", s2 = "bde"
โ€บ Output: "bcde"
๐Ÿ’ก Note: The substring "bcde" is the shortest window in s1 that contains "bde" as a subsequence. We can pick 'b' at index 1, 'd' at index 3, and 'e' at index 4.
example_2.py โ€” No Valid Window
$ Input: s1 = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl", s2 = "u"
โ€บ Output: ""
๐Ÿ’ก Note: There is no character 'u' in s1, so no substring can contain s2 as a subsequence.
example_3.py โ€” Multiple Windows
$ Input: s1 = "abcdebdde", s2 = "bd"
โ€บ Output: "bcd"
๐Ÿ’ก Note: Multiple valid windows exist: "bcd" (length 3), "bcde" (length 4), "bcdeb" (length 5), etc. We return "bcd" as it's the shortest.

Visualization

Tap to expand
๐Ÿ•ต๏ธ Detective's Evidence HuntPolice Report: "abcdebdde"Evidence: "bde"Step 1: Expand Search ๐Ÿ”"abcdeb"๐Ÿ‘๐Ÿ”Found all evidence: bโœ“ dโœ“ eโœ“Step 2: Shrink to Minimum ๐ŸŽฏ"bcde"๐ŸŽฏ๐Ÿ”โœ… Minimum Evidence Section Found!Continue searching from next position for potentially shorter sections...
Understanding the Visualization
1
๐Ÿ” Expand Search Area
Move your magnifying glass right until you've found all clues in sequence
2
๐ŸŽฏ Shrink to Minimum
Contract from the left while keeping all clues visible
3
๐Ÿ“ Record Best Result
Save the smallest valid section and continue searching
4
๐Ÿ”„ Repeat Process
Move to next position and repeat until entire document scanned
Key Takeaway
๐ŸŽฏ Key Insight: The two-phase approach (expand-then-shrink) ensures we find all possible minimum windows efficiently without redundant checking!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mยทn)

m iterations of the outer loop, each doing O(n) work to scan s1

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for pointers and variables

n
2n
โœ“ Linear Space

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
Asked in
Google 28 Amazon 22 Microsoft 15 Meta 12
34.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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