Skip to content

Commit 5055c4a

Browse files
committed
Runtime: 7 ms (Top 81.4%) | Memory: 45.10 MB (Top 27.91%)
1 parent 52a628a commit 5055c4a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Runtime: 7 ms (Top 81.4%) | Memory: 45.10 MB (Top 27.91%)
2+
3+
// separate string s into two parts and match subsequence forward and backward separately
4+
class Solution {
5+
public int minimumScore(String s, String t) {
6+
int m = s.length(), n = t.length();
7+
int[] left = new int[m];
8+
for (int i = 0, j = 0; i < m; i++) {
9+
if (j < n && s.charAt(i) == t.charAt(j)) {
10+
++j;
11+
}
12+
left[i] = j;
13+
}
14+
int[] right = new int[m];
15+
for (int i = m - 1, j = n - 1; i >= 0 ; i--) {
16+
if (j >= 0 && s.charAt(i) == t.charAt(j)) {
17+
--j;
18+
}
19+
right[i] = j;
20+
}
21+
int min = Math.min(n - left[m - 1], right[0] + 1);
22+
for (int i = 0; i + 1 < m; i++) {
23+
min = Math.min(min, Math.max(0, right[i + 1] - left[i] + 1));
24+
}
25+
return min;
26+
}
27+
}

0 commit comments

Comments
 (0)