-
Notifications
You must be signed in to change notification settings - Fork 1
/
FreedomTrail.java
51 lines (41 loc) · 1.52 KB
/
FreedomTrail.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
private int ringLength = 0;
public int findRotateSteps(String ring, String key) {
ringLength = ring.length();
Map<Character, ArrayList<Integer>> ringMap = new HashMap<>();
int curHead = 0;
for (int i = 0; i < ring.length(); i++) {
Character chr = ring.charAt(i);
if (ringMap.containsKey(chr)) {
ringMap.get(chr).add(i);
} else {
ArrayList<Integer> indexList = new ArrayList<>();
indexList.add(i);
ringMap.put(chr, indexList);
}
}
Map<Integer, Integer> result = new HashMap<>();
result.put(0, 0);
for (int i = 0; i < key.length(); ++i) {
if (i > 0 && key.charAt(i) == key.charAt(i - 1)) {
continue;
}
result = processNext(result, ringMap.get(key.charAt(i)));
}
int totalSteps = Integer.MAX_VALUE;
for (int i : result.values()) {
totalSteps = Math.min(totalSteps, i);
}
return totalSteps + key.length();
}
private Map<Integer,Integer> processNext(Map<Integer,Integer> result, List<Integer> candidates) {
Map<Integer, Integer> nextResult = new HashMap<Integer, Integer>();
for (int i : candidates) {
int diff = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> entry : result.entrySet()) {
diff = Math.min(diff, Math.abs(i - entry.getKey()) + entry.getValue());
diff = Math.min(diff, ringLength - Math.abs(i - entry.getKey()) + entry.getValue());
}
nextResult.put(i, diff);
}
return nextResult;
}