727. Minimum Window Subsequence
https://leetcode.com/problems/minimum-window-subsequence/
Input:
S = "abcdebdde", T = "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 T in the window must occur in order.class Solution:
def minWindow(self, S: str, T: str) -> str:
M, N, res = len(S), len(T), ''
dp = [[math.inf] * (M + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(1, M + 1):
if T[i - 1] == S[j - 1]:
dp[i][j] = 1 if i == 1 else dp[i - 1][j - 1] + 1
else:
dp[i][j] = dp[i][j - 1] + 1
mi = min(dp[-1])
if mi == math.inf: return ''
for i in range(1, M + 1):
if dp[-1][i] == mi:
return S[i - mi: i]Last updated