Minimum Window Subsequence

IF
AlgoAxiomStaff Engineers
JSTS
Hard20 mins

Given strings s1 and s2, return the minimum contiguous substring of s1 such that s2 is a subsequence of the substring. If there is no such window, return an empty string "".

If there are multiple minimum-length windows, return the one that occurs first (leftmost).

Examples

Example 1:

Input: s1 = "abcdebdde", s2 = "bde"

Output: "bcde"

Explanation: "bcde" is the shortest substring of s1 where "bde" is a subsequence: b→d→e. Another candidate "bdde" has the same length but starts later.

Example 2:

Input: s1 = "jmeqksfrsdcmsiwvaovztaqenqlcvg", s2 = "u"

Output: ""

Explanation: The character 'u' does not exist in s1, so no valid window exists.

Example 3:

Input: s1 = "abcde", s2 = "ace"

Output: "abcde"

Explanation: The subsequence a→c→e spans the entire string. No shorter window contains all three characters in order.

Constraints

  • 1 <= s1.length <= 2 * 10^4
  • 1 <= s2.length <= 100
  • s1 and s2 consist of lowercase English letters.
Source: Sliding Window pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle