{x}
blog image

Minimum Number of Frogs Croaking

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'.

1419. Minimum Number of Frogs Croaking

Problem Description

Given a string croakOfFrogs representing a combination of "croak" sounds from different frogs, determine the minimum number of different frogs needed to produce the given string. A valid "croak" is a sequence of 'c', 'r', 'o', 'a', 'k'. Frogs must croak sequentially, and if the input string is not a valid combination of "croak"s, return -1.

Solution Approach: Counting and Simulation

This problem can be efficiently solved using a counting approach combined with simulation. The core idea is to track the number of frogs at each stage of the croaking process ('c', 'r', 'o', 'a', 'k').

  1. Validity Check: If the length of croakOfFrogs is not divisible by 5 (the length of "croak"), it's impossible to form valid "croaks", so return -1.

  2. Counting Array: We'll use an array cnt of size 5 to store the counts of each character ('c', 'r', 'o', 'a', 'k'). cnt[i] represents the number of frogs currently at stage i of their croak (0 for 'c', 1 for 'r', etc.).

  3. Active Frogs: A variable x will track the number of currently active frogs (those that have started but not finished their croak). The maximum value of x during the simulation represents the minimum number of frogs needed.

  4. Iteration: Iterate through the input string:

    • If the character is 'c', increment x (a new frog starts) and update the maximum number of active frogs.
    • If the character is 'r', 'o', 'a', or 'k', check if there are enough frogs in the previous stage (cnt[i-1] > 0). If not, return -1 (invalid sequence). Decrement the count of the previous stage and, if it's 'k', decrement x (a frog finishes its croak).
  5. Final Check: After the iteration, if x is 0, all frogs have finished; return the maximum number of active frogs. Otherwise, return -1 (incomplete croaks).

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of croakOfFrogs. We iterate through the string once.
  • Space Complexity: O(1). We use a fixed-size array cnt and a few integer variables.

Code Implementation

Here's the code in several programming languages:

Python

class Solution:
    def minNumberOfFrogs(self, croakOfFrogs: str) -> int:
        n = len(croakOfFrogs)
        if n % 5 != 0:
            return -1
        cnt = [0] * 5
        ans = x = 0
        croak_map = {'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4}
        for c in croakOfFrogs:
            i = croak_map[c]
            cnt[i] += 1
            if i == 0:
                x += 1
                ans = max(ans, x)
            else:
                if cnt[i - 1] == 0:
                    return -1
                cnt[i - 1] -= 1
                if i == 4:
                    x -= 1
        return ans if x == 0 else -1
 

Java

class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
        int n = croakOfFrogs.length();
        if (n % 5 != 0) return -1;
        int[] cnt = new int[5];
        int ans = 0, x = 0;
        for (char c : croakOfFrogs.toCharArray()) {
            int i = "croak".indexOf(c);
            cnt[i]++;
            if (i == 0) {
                x++;
                ans = Math.max(ans, x);
            } else {
                if (cnt[i - 1] == 0) return -1;
                cnt[i - 1]--;
                if (i == 4) x--;
            }
        }
        return x == 0 ? ans : -1;
    }
}

C++

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        int n = croakOfFrogs.length();
        if (n % 5 != 0) return -1;
        vector<int> cnt(5, 0);
        int ans = 0, x = 0;
        for (char c : croakOfFrogs) {
            int i = "croak".find(c);
            cnt[i]++;
            if (i == 0) {
                x++;
                ans = max(ans, x);
            } else {
                if (cnt[i - 1] == 0) return -1;
                cnt[i - 1]--;
                if (i == 4) x--;
            }
        }
        return x == 0 ? ans : -1;
    }
};

These implementations follow the algorithm described above, providing a concise and efficient solution to the problem. Remember to adapt the code to your preferred programming language and environment.