Problem
You are given the string croakOfFrogs
, which represents a combination of the string "croak"
from different frogs, that is, multiple frogs can croak at the same time, so multiple "croak"
are mixed.
**Return the minimum number of *different* frogs to finish all the croaks in the given string.**
A valid "croak"
means a frog is printing five letters 'c'
, 'r'
, 'o'
, 'a'
, and 'k'
sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of a valid "croak"
return -1
.
Example 1:
Input: croakOfFrogs = "croakcroak"
Output: 1
Explanation: One frog yelling "croak" twice.
Example 2:
Input: croakOfFrogs = "crcoakroak"
Output: 2
Explanation: The minimum number of frogs is two.
The first frog could yell "crcoakroak".
The second frog could yell later "crcoakroak".
Example 3:
Input: croakOfFrogs = "croakcrook"
Output: -1
Explanation: The given string is an invalid combination of "croak" from different frogs.
Constraints:
1 <= croakOfFrogs.length <= 10^5
croakOfFrogs
is either'c'
,'r'
,'o'
,'a'
, or'k'
.
Solution (Java)
class Solution {
public int minNumberOfFrogs(String s) {
int ans = 0;
int[] f = new int[26];
for (int i = 0; i < s.length(); i++) {
f[s.charAt(i) - 'a']++;
if (s.charAt(i) == 'k' && checkEnough(f)) {
reduce(f);
}
if (!isValid(f)) {
return -1;
}
ans = Math.max(ans, getMax(f));
}
return isEmpty(f) ? ans : -1;
}
private boolean checkEnough(int[] f) {
return f['c' - 'a'] > 0
&& f['r' - 'a'] > 0
&& f['o' - 'a'] > 0
&& f[0] > 0
&& f['k' - 'a'] > 0;
}
public void reduce(int[] f) {
f['c' - 'a']--;
f['r' - 'a']--;
f['o' - 'a']--;
f[0]--;
f['k' - 'a']--;
}
private int getMax(int[] f) {
int max = 0;
for (int v : f) {
max = Math.max(max, v);
}
return max;
}
private boolean isEmpty(int[] f) {
for (int v : f) {
if (v > 0) {
return false;
}
}
return true;
}
private boolean isValid(int[] f) {
if (f['r' - 'a'] > f['c' - 'a']) {
return false;
}
if (f['o' - 'a'] > f['r' - 'a']) {
return false;
}
if (f[0] > f['o' - 'a']) {
return false;
}
return f['k' - 'a'] <= f[0];
}
}
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).