-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
31 lines (31 loc) · 1.02 KB
/
Solution.java
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
class Solution {
public int findRotateSteps(String ring, String key) {
int m = key.length(), n = ring.length();
List<Integer>[] pos = new List[26];
Arrays.setAll(pos, k -> new ArrayList<>());
for (int i = 0; i < n; ++i) {
int j = ring.charAt(i) - 'a';
pos[j].add(i);
}
int[][] f = new int[m][n];
for (var g : f) {
Arrays.fill(g, 1 << 30);
}
for (int j : pos[key.charAt(0) - 'a']) {
f[0][j] = Math.min(j, n - j) + 1;
}
for (int i = 1; i < m; ++i) {
for (int j : pos[key.charAt(i) - 'a']) {
for (int k : pos[key.charAt(i - 1) - 'a']) {
f[i][j] = Math.min(
f[i][j], f[i - 1][k] + Math.min(Math.abs(j - k), n - Math.abs(j - k)) + 1);
}
}
}
int ans = 1 << 30;
for (int j : pos[key.charAt(m - 1) - 'a']) {
ans = Math.min(ans, f[m - 1][j]);
}
return ans;
}
}