-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
62 lines (54 loc) · 1.37 KB
/
Solution.cs
File metadata and controls
62 lines (54 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Solution
{
public int MinimumScore(string s, string t)
{
int n = t.Length;
int[] leftmost = BuildLeft(s, t);
int[] rightmost = BuildRight(s, t);
int ret = n;
for (int i = 0; i <= n; i++)
{
int low = i, high = n;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (leftmost[i] < rightmost[mid])
{
ret = Math.Min(ret, mid - i);
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
return ret;
}
int[] BuildLeft(string s, string t)
{
int[] leftmost = new int[t.Length + 1];
leftmost[0] = -1;
int j = 0;
for (int i = 0; i < t.Length; i++)
{
while (j < s.Length && s[j] != t[i]) j++;
leftmost[i + 1] = j;
j++;
}
return leftmost;
}
int[] BuildRight(string s, string t)
{
int[] rightmost = new int[t.Length + 1];
rightmost[^1] = s.Length;
int j = s.Length - 1;
for (int i = t.Length - 1; i >= 0; i--)
{
while (j >= 0 && s[j] != t[i]) j--;
rightmost[i] = j;
j--;
}
return rightmost;
}
}