-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1419.java
More file actions
35 lines (35 loc) · 1.06 KB
/
Copy pathSolution1419.java
File metadata and controls
35 lines (35 loc) · 1.06 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
class Solution1419 {
public int minNumberOfFrogs(String croakOfFrogs) {
if (croakOfFrogs.length() % 5 != 0) return -1;
int[] hash = new int[4];
Map<Character, Integer> map = new HashMap<>() {{
put('c', 0);
put('r', 1);
put('o', 2);
put('a', 3);
}};
int res = 0;
for (int i = 0; i < croakOfFrogs.length(); i++) {
char c = croakOfFrogs.charAt(i);
if (c != 'k') {
hash[map.get(c)]++;
// hash 数组应该是逐渐递减的
int t = hash[0];
for (int j = 1; j < 4; j++) {
if (hash[j] > t) return -1;
t = hash[j];
}
} else {
int t = hash[0];
res = Math.max(res, t);
for (int j = 0; j < 4; j++) {
hash[j]--;
}
}
}
for (int i = 0; i < 4; i++) {
if (hash[i] > 0) return -1;
}
return res;
}
}