Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #727: Minimum Window Subsequence

In this guide, we solve Leetcode #727 Minimum Window Subsequence in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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 "".

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: String, Dynamic Programming, Sliding Window

Intuition

We are looking for a contiguous region that satisfies a constraint, which is a classic sliding-window signal.

Expanding and shrinking the window lets us maintain validity without restarting the scan.

Approach

Grow the window with a right pointer, and shrink from the left only when the constraint is violated.

Track the best window as you go to keep the solution linear.

Steps:

  • Expand the right end of the window.
  • While invalid, move the left end to restore constraints.
  • Update the best window found.

Example

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.

Python Solution

class Solution: def minWindow(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) f = [[0] * (n + 1) for _ in range(m + 1)] for i, a in enumerate(s1, 1): for j, b in enumerate(s2, 1): if a == b: f[i][j] = i if j == 1 else f[i - 1][j - 1] else: f[i][j] = f[i - 1][j] p, k = 0, m + 1 for i, a in enumerate(s1, 1): if a == s2[n - 1] and f[i][n]: j = f[i][n] - 1 if i - j < k: k = i - j p = j return "" if k > m else s1[p : p + k]

Complexity

The time complexity is O(m×n)O(m \times n)O(m×n), and the space complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(m×n)O(m \times n)O(m×n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy