Skip to content

Commit 329147b

Browse files
authored
Update 727-minimum-window-subsequence.js
1 parent dc07b82 commit 329147b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

727-minimum-window-subsequence.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
/**
2+
* @param {string} s1
3+
* @param {string} s2
4+
* @return {string}
5+
*/
6+
const minWindow = function (s1, s2) {
7+
const S = s1,T=s2
8+
let m = T.length, n = S.length;
9+
let dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
10+
for (let j = 0; j <= n; j++) {
11+
dp[0][j] = j + 1;
12+
}
13+
for (let i = 1; i <= m; i++) {
14+
for (let j = 1; j <= n; j++) {
15+
if (T.charAt(i - 1) == S.charAt(j - 1)) {
16+
dp[i][j] = dp[i - 1][j - 1];
17+
} else {
18+
dp[i][j] = dp[i][j - 1];
19+
}
20+
}
21+
}
22+
23+
let start = 0, len = n + 1;
24+
for (let j = 1; j <= n; j++) {
25+
if (dp[m][j] != 0) {
26+
if (j - dp[m][j] + 1 < len) {
27+
start = dp[m][j] - 1;
28+
len = j - dp[m][j] + 1;
29+
}
30+
}
31+
}
32+
return len == n + 1 ? "" : S.substring(start, start + len);
33+
}
34+
35+
// another
36+
137
/**
238
* @param {string} s1
339
* @param {string} s2

0 commit comments

Comments
 (0)