g0501_0600.s0514_freedom_trail.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0514_freedom_trail;
// #Hard #String #Dynamic_Programming #Depth_First_Search #Breadth_First_Search
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public class Solution {
public int findRotateSteps(String ring, String key) {
List[] indexs = new List[26];
for (int i = 0; i < ring.length(); i++) {
int num = ring.charAt(i) - 'a';
if (indexs[num] == null) {
indexs[num] = new ArrayList<>();
}
indexs[num].add(i);
}
int[][] dp = new int[101][101];
return find(ring, 0, key, 0, dp, indexs);
}
private int find(String ring, int i, String key, int j, int[][] cache, List[] indexs) {
if (j == key.length()) {
return 0;
}
if (cache[i][j] != 0) {
return cache[i][j];
}
int ans = Integer.MAX_VALUE;
List targets = indexs[key.charAt(j) - 'a'];
for (int t : targets) {
int delta = Math.abs(i - t);
delta = Math.min(delta, Math.abs(ring.length() - delta));
ans = Math.min(ans, 1 + delta + find(ring, t, key, j + 1, cache, indexs));
}
cache[i][j] = ans;
return ans;
}
}