> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Window Subsequence Python Solution

> Tested Python solution for LeetCode 727 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 727, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/minimum-window-subsequence/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 727   # by problem number
lcpy gen -s minimum_window_subsequence   # by problem name
```

## Problem

Given strings `s1` and `s2`, return the minimum contiguous substring part of `s1`, so that `s2` is a subsequence of the part.

If there is no such window in `s1` that covers all characters in `s2`, return the empty string `""`. If there are multiple such minimum-length windows, return the one with the **left-most starting index**.

A subsequence of a string is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

### Examples

```
Input: s1 = "abcdebdde", s2 = "bde"
Output: "bcde"
Explanation: "bcde" is the answer because it occurs before "bdde" which has the same length.
"deb" is not a smaller window because the elements of s2 in the window must occur in order.
```

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

### Constraints

* 1 \<= s1.length \<= 2 \* 10^4
* 1 \<= s2.length \<= 100
* s1 and s2 consist of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_window_subsequence/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_window_subsequence/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(n)
    def min_window(self, s1: str, s2: str) -> str:
        m, n = len(s1), len(s2)
        start, best = 0, m + 1
        # dp[j] = smallest index i such that s1[:i] contains s2[:j] as a suffix subsequence
        prev = [0] * (n + 1)
        for i in range(1, m + 1):
            cur = [0] * (n + 1)
            for j in range(1, n + 1):
                if s1[i - 1] == s2[j - 1]:
                    cur[j] = i if j == 1 else prev[j - 1]
                else:
                    cur[j] = prev[j]
            if cur[n] and i - cur[n] + 1 < best:
                best = i - cur[n] + 1
                start = cur[n] - 1
            prev = cur
        return "" if best > m else s1[start : start + best]
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.