Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 10c7cd8

Browse files
committedJan 28, 2024
Runtime: 92 ms (Top 50.0%) | Memory: 60.10 MB (Top 50.0%)
1 parent 2377e1f commit 10c7cd8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
 
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Runtime: 92 ms (Top 50.0%) | Memory: 60.10 MB (Top 50.0%)
2+
3+
/**
4+
* @param {string} s
5+
* @param {string} t
6+
* @return {number}
7+
*/
8+
var minimumScore = function(S, T) {
9+
let s = S.split(''), t = T.split('');
10+
let lo = 0, hi = T.length;
11+
while (lo <= hi) {
12+
let m = Math.floor((lo + hi) / 2);
13+
if (check(s, t, m)) hi = m - 1;
14+
else lo = m + 1;
15+
}
16+
return hi + 1;
17+
};
18+
19+
var check = function(s, t, len) {
20+
let t_length = t.length, n = s.length;
21+
if (len >= t_length) return true; //delete whole t array
22+
let pos = new Array(t_length).fill(1000000000); //Greedy left matching
23+
let t_left_index = 0;
24+
for (let i = 0; i < n; i++) {
25+
if (t_left_index === t_length) break;
26+
if (t[t_left_index] === s[i]) {
27+
pos[t_left_index] = i;
28+
t_left_index++;
29+
}
30+
}
31+
if (t_left_index >= t_length - len) return true; //we can delete right subarray of length len
32+
let right_index_of_s = n - 1;
33+
for (let rp = t_length - 1; rp >= len; rp--) {
34+
while (right_index_of_s >= 0 && s[right_index_of_s] !== t[rp]) right_index_of_s--;
35+
if (right_index_of_s === -1) return false;
36+
let lp = rp - len - 1;
37+
if (lp === -1 || pos[lp] < right_index_of_s) return true;
38+
right_index_of_s--;
39+
}
40+
return false;
41+
};
42+
43+

0 commit comments

Comments
 (0)
Please sign in to comment.