Find the shortest substring that contains another string as a subsequence

Quick Overview

Find the shortest contiguous substring of one string that contains a second string as a subsequence, returning the left-most window when lengths tie. It tests dynamic programming over two strings, the order of in-place updates, and careful tie handling.

Find the shortest substring that contains another string as a subsequence

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given strings `s1` and `s2`, return the shortest contiguous substring (window) of `s1` such that `s2` is a subsequence of that window. If there is no window in `s1` that contains `s2` as a subsequence, return the empty string `""`. If there are multiple windows of the minimum length, return the one with the left-most starting index. ### Function Signature ```python def min_window_subsequence(s1: str, s2: str) -> str: ``` ### Rules - `s2` is a subsequence of a window when every character of `s2` appears in the window in the same order as in `s2`, with any number of other characters between them. - When several windows share the minimum length, return the one that starts at the smallest index of `s1`. - Return `""` when no window works. ### Constraints - `1 <= len(s1) <= 20000` - `1 <= len(s2) <= 100` - `s1` and `s2` consist of lowercase English letters. ### Examples ```text Input: s1 = "abcdebdde", s2 = "bde" Output: "bcde" ``` Both "bcde" (indices 1 to 4) and "bdde" (indices 5 to 8) contain b, d, e in that order, and both have length 4. No window of length 3 works, so the tie rule picks "bcde", which starts further left. ```text Input: s1 = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl", s2 = "u" Output: "" ``` The letter "u" does not appear in `s1`, so no valid window exists. ```text Input: s1 = "fgbaxab", s2 = "ab" Output: "ab" ``` The window "ba" at indices 2 and 3 holds both letters in the wrong order. The shortest window with an "a" followed later by a "b" is "ab" at indices 5 and 6.

Overview: Find the shortest contiguous substring of one string that contains a second string as a subsequence, returning the left-most window when lengths tie. It tests dynamic programming over two strings, the order of in-place updates, and careful tie handling.

ServiceNow logo
ServiceNow
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given strings s1 and s2, return the shortest contiguous substring (window) of s1 such that s2 is a subsequence of that window.

If there is no window in s1 that contains s2 as a subsequence, return the empty string "".

If there are multiple windows of the minimum length, return the one with the left-most starting index.

Function Signature

def min_window_subsequence(s1: str, s2: str) -> str:

Rules

  • s2 is a subsequence of a window when every character of s2 appears in the window in the same order as in s2 , with any number of other characters between them.
  • When several windows share the minimum length, return the one that starts at the smallest index of s1 .
  • Return "" when no window works.

Constraints

  • 1 <= len(s1) <= 20000
  • 1 <= len(s2) <= 100
  • s1 and s2 consist of lowercase English letters.

Examples

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

Both "bcde" (indices 1 to 4) and "bdde" (indices 5 to 8) contain b, d, e in that order, and both have length 4. No window of length 3 works, so the tie rule picks "bcde", which starts further left.

Input: s1 = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl", s2 = "u"
Output: ""

The letter "u" does not appear in s1, so no valid window exists.

Input: s1 = "fgbaxab", s2 = "ab"
Output: "ab"

The window "ba" at indices 2 and 3 holds both letters in the wrong order. The shortest window with an "a" followed later by a "b" is "ab" at indices 5 and 6.