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 <= 105
croakOfFrogs
is either 'c'
, 'r'
, 'o'
, 'a'
, or 'k'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you're trying to figure out how many frogs need to be singing at the same time to make a given croaking sound. The brute force method is like carefully listening to the sound and figuring out, at each point, the most frogs you could possibly need to make that sound up to that moment.
Here's how the algorithm would work step-by-step:
def minimum_number_of_frogs(croak_of_frogs): maximum_frogs_needed = 0
current_frogs_croaking = 0
croak_index = {
'c': 0,
'r': 1,
'o': 2,
'a': 3,
'k': 4
}
croak_counts = [0] * 5
for char in croak_of_frogs:
if char not in croak_index:
return -1
index = croak_index[char]
croak_counts[index] += 1
# Check for invalid croak sequence
if index > 0 and croak_counts[index] > croak_counts[index - 1]:
return -1
if char == 'c':
# A new frog starts croaking
current_frogs_croaking += 1
maximum_frogs_needed = max(maximum_frogs_needed, current_frogs_croaking)
elif char == 'k':
# A frog finishes croaking
current_frogs_croaking -= 1
# Check if all frogs finished croaking
if current_frogs_croaking != 0:
return -1
# Verify that all characters appeared the same number of times
if not all(count == croak_counts[0] for count in croak_counts):
return -1
return maximum_frogs_needed
The key is to track the progress of each 'croak' independently and reuse frogs as soon as they become available. We're essentially managing a pool of frogs, allocating them to ongoing croaks and freeing them when a croak is complete.
Here's how the algorithm would work step-by-step:
def min_number_of_frogs(croak_of_frogs: str) -> int:
croak_counts = {'c': 0, 'r': 0, 'o': 0, 'a': 0, 'k': 0}
frogs_in_use = 0
min_frogs_needed = 0
for char in croak_of_frogs:
if char == 'c':
croak_counts['c'] += 1
frogs_in_use += 1
min_frogs_needed = max(min_frogs_needed, frogs_in_use)
elif char == 'r':
if croak_counts['c'] <= croak_counts['r']:
return -1
croak_counts['r'] += 1
elif char == 'o':
if croak_counts['r'] <= croak_counts['o']:
return -1
croak_counts['o'] += 1
elif char == 'a':
if croak_counts['o'] <= croak_counts['a']:
return -1
croak_counts['a'] += 1
elif char == 'k':
if croak_counts['a'] <= croak_counts['k']:
return -1
croak_counts['k'] += 1
# Frog is now available to croak again.
frogs_in_use -= 1
# Ensure all croaks are complete.
if not (croak_counts['c'] == croak_counts['r'] == \
croak_counts['o'] == croak_counts['a'] == croak_counts['k']):
return -1
return min_frogs_needed
Case | How to Handle |
---|---|
Null or empty input string | Return -1 immediately as no croaks can be formed. |
Input string length is not a multiple of 5 | Return -1 because a valid croak must have all 5 characters. |
The first character is not 'c' | Return -1 because every croak must start with 'c'. |
The order of characters is invalid (e.g., 'craok') | Maintain a count of each character and verify that the order is always c->r->o->a->k, returning -1 if not. |
More 'r's than 'c's, or similar imbalances between characters | Return -1 if at any point the count of a character is greater than the count of the character before it in 'croak'. |
Input string contains characters other than 'c', 'r', 'o', 'a', 'k' | Return -1 because we only allow characters that form the word croak. |
A croak sequence is started but not finished before another croak sequence starts. | Increment frogs when 'c' is encountered and decrement when 'k' is encountered, tracking the maximum concurrent frog count. |
After processing the entire string, some croaks are unfinished | Return -1 if the counts of 'c', 'r', 'o', 'a', and 'k' are not equal at the end. |