oneby7

Sliding Window Longest or shortest window

177. Minimum Window Subsequence

Hard

You are given two strings, s and t. Return the shortest substring of s that contains t as a subsequence.

A substring is a block of letters that sit next to each other in s. A block contains t as a subsequence when the letters of t appear in it in the same order, though not necessarily next to each other. For t = "ace", the block "abcde" works: it has an a, later a c, and later an e. The block "eca" does not. Each letter of t needs its own position, so t = "oo" needs two os.

If several substrings share the shortest length, return the one that starts first. If no substring works, return the empty string. Answers are shown in double quotes, so an empty answer shows as "".

Example 1

Input: s = "programming", t = "rmn"

Output: "rammin"

Explanation: "rammin" has an r, then an m, then an n, and no shorter block does. The block that starts at the first r, "rogrammin", works too, but it is longer.

Example 2

Input: s = "cabbage", t = "ba"

Output: "ba"

Explanation: "ab" at index 1 has both letters, but in the wrong order. The shortest block with a b and then an a is "ba", at index 3.

Example 3

Input: s = "stone", t = "nest"

Output: ""

Explanation: Every letter of t is in s, but no s comes after the n and the e. No block contains t in order, so the answer is the empty string.

Check your understanding

Work out the answer for this input before you write any code.

s = "availability", t = "ali"

Constraints

  • 1 ≤ s.length ≤ 105
  • 1 ≤ t.length ≤ 100
  • s and t hold lowercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.