Watch 10 video solutions for Minimum Number of Frogs Croaking, a medium level problem involving String, Counting. This walkthrough by Pepcoding has 3,016 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 105croakOfFrogs is either 'c', 'r', 'o', 'a', or 'k'.Problem Overview: You receive a string representing overlapping frog sounds where each frog croaks the sequence "croak". Multiple frogs may croak simultaneously, so characters can interleave. The task is to verify the sequence is valid and compute the minimum number of frogs required to produce it.
Approach 1: Counter Technique by Stages (O(n) time, O(1) space)
Track how many frogs are currently at each stage of the word "croak". Maintain counters for characters c, r, o, a, and k. As you iterate through the string, move frogs from one stage to the next: when you see r, a frog must previously have produced c; when you see o, it must follow r, and so on. If a required previous stage does not exist, the sequence is invalid.
Each c either starts a new frog or reuses one that just finished with k. Track the number of active frogs (those mid‑croak) and update the maximum seen during traversal. That maximum represents the minimum frogs needed. Because only five counters are maintained and each character is processed once, the algorithm runs in O(n) time with O(1) space.
This approach works well because the croak sequence has a fixed order. Instead of tracking each frog individually, you track counts of frogs in each stage. Problems involving ordered character transitions often fall into string processing with lightweight counting techniques.
Approach 2: Hashmap Technique with Active Frogs (O(n) time, O(1) space)
Maintain a hashmap that records how many frogs are currently waiting for the next character in the croak sequence. For example, frogs that just produced c expect r, frogs at r expect o, and so on. Iterate through the string and update the map as frogs advance through the sequence.
If the current character cannot be matched with a frog waiting for that stage, the string is invalid. When a frog reaches k, its croak completes and that frog becomes available for reuse. Track how many frogs are active simultaneously to determine the minimum number required. The hashmap only stores counts for the five stages, so the memory usage stays constant.
This version models the lifecycle of each croak more explicitly. The logic is slightly easier to reason about when debugging because each step maps directly to the next expected character. It uses the same linear scan pattern commonly seen in hash map based tracking problems.
Recommended for interviews: The counter-by-stages solution is usually preferred. It demonstrates that you recognized the fixed "croak" progression and optimized the tracking to constant space. Explaining the invalid sequence checks and how active frogs determine the final answer shows strong control over string state transitions.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counter Technique by Stages | O(n) | O(1) | Best overall solution when the sequence order is fixed and you only need stage counts |
| Hashmap with Active Frogs | O(n) | O(1) | Useful when modeling state transitions explicitly or extending the logic to longer sequences |