Skip to content

Commit 607dc5e

Browse files
authored
Create 2565-subsequence-with-the-minimum-score.js
1 parent 453f076 commit 607dc5e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {number}
5+
*/
6+
var minimumScore = function(s, t) {
7+
let sl = s.length, tl = t.length, k = tl - 1;
8+
const dp = new Array(tl).fill(-1);
9+
for (let i = sl - 1; i >= 0 && k >= 0; --i) {
10+
if (s.charAt(i) === t.charAt(k)) dp[k--] = i;
11+
}
12+
let res = k + 1;
13+
for (let i = 0, j = 0; i < sl && j < tl && res > 0; ++i) {
14+
if (s.charAt(i) === t.charAt(j)) {
15+
while(k < tl && dp[k] <= i) k++
16+
j++
17+
res = Math.min(res, k - j);
18+
}
19+
}
20+
21+
return res;
22+
};

0 commit comments

Comments
 (0)