forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_488.java
60 lines (53 loc) · 2.13 KB
/
_488.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
52
53
54
55
56
57
58
59
60
package com.fishercoder.solutions;
public class _488 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98
* Two layer of recursion, pretty cool!
*/
int maxcount = 6; // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5"
public int findMinStep(String board, String hand) {
int[] handCount = new int[26];
for (int i = 0; i < hand.length(); ++i) {
++handCount[hand.charAt(i) - 'A'];
}
int result = dfs(board + "#", handCount); // append a "#" to avoid special process while j==board.length, make the code shorter.
return result == maxcount ? -1 : result;
}
private int dfs(String s, int[] handCount) {
s = removeConsecutive(s);
if (s.equals("#")) {
return 0;
}
int result = maxcount;
int need = 0;
for (int i = 0, j = 0; j < s.length(); ++j) {
if (s.charAt(j) == s.charAt(i)) {
continue;
}
need = 3 - (j - i); //balls need to remove current consecutive balls.
if (handCount[s.charAt(i) - 'A'] >= need) {
handCount[s.charAt(i) - 'A'] -= need;
result = Math.min(result, need + dfs(s.substring(0, i) + s.substring(j), handCount));
handCount[s.charAt(i) - 'A'] += need;
}
i = j;
}
return result;
}
//remove consecutive balls longer than 3
private String removeConsecutive(String board) {
for (int i = 0, j = 0; j < board.length(); ++j) {
if (board.charAt(j) == board.charAt(i)) {
continue;
}
if (j - i >= 3) {
return removeConsecutive(board.substring(0, i) + board.substring(j));
} else {
i = j;
}
}
return board;
}
}
}