Minimum Number of Frogs Croaking LeetCode solution in Java

1
class Solution {
2
public int minNumberOfFrogs(String croakOfFrogs) {
3
int[] index = new int[26];
4
String corak = "croak";
5

6
// Giving index to each characters
7
for (int i = 0; i < corak.length(); ++i) index[corak.charAt(i) - 'a'] = i;
8

9
int ans = 0, sum = 0;
10
int[] count = new int[5];
11

12
for (char c : croakOfFrogs.toCharArray()) {
13
int i = index[c - 'a'];
14
// If it is not 'c' it will decrease the sum
15
if (c != 'c') {
16
if (count[i - 1]-- <= 0) return -1;
17
sum--;
18
}
19
// If it is not 'k' it will increase the sum
20
if (c != 'k') {
21
count[i]++;
22
sum++;
23
}
24
ans = Math.max(ans, sum);
25
}
26
return sum == 0 ? ans : -1;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0