1419. Minimum Number of Frogs Croaking LeetCode Solution

In this guide, you will get 1419. Minimum Number of Frogs Croaking LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Frogs Croaking problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Number of Frogs Croaking solution in C++
  4. Minimum Number of Frogs Croaking solution in Java
  5. Minimum Number of Frogs Croaking solution in Python
  6. Additional Resources
1419. Minimum Number of Frogs Croaking LeetCode Solution image

Problem Statement of 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.

See also  987. Vertical Order Traversal of a Binary Tree LeetCode Solution

Constraints:

1 <= croakOfFrogs.length <= 105
croakOfFrogs is either 'c', 'r', 'o', 'a', or 'k'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1419. Minimum Number of Frogs Croaking LeetCode Solution in C++

class Solution {
 public:
  int minNumberOfFrogs(string croakOfFrogs) {
    const string kCroak = "croak";
    int ans = 0;
    int frogs = 0;
    vector<int> count(5);

    for (const char c : croakOfFrogs) {
      ++count[kCroak.find(c)];
      for (int i = 1; i < 5; ++i)
        if (count[i] > count[i - 1])
          return -1;
      if (c == 'c')
        ++frogs;
      else if (c == 'k')
        --frogs;
      ans = max(ans, frogs);
    }

    return frogs == 0 ? ans : -1;
  }
};
/* code provided by PROGIEZ */

1419. Minimum Number of Frogs Croaking LeetCode Solution in Java

class Solution {
  public int minNumberOfFrogs(String croakOfFrogs) {
    final String kCroak = "croak";
    int ans = 0;
    int frogs = 0;
    int[] count = new int[5];

    for (final char c : croakOfFrogs.toCharArray()) {
      ++count[kCroak.indexOf(c)];
      for (int i = 1; i < 5; ++i)
        if (count[i] > count[i - 1])
          return -1;
      if (c == 'c')
        ++frogs;
      else if (c == 'k')
        --frogs;
      ans = Math.max(ans, frogs);
    }

    return frogs == 0 ? ans : -1;
  }
}
// code provided by PROGIEZ

1419. Minimum Number of Frogs Croaking LeetCode Solution in Python

class Solution:
  def minNumberOfFrogs(self, croakOfFrogs: str) -> int:
    kCroak = 'croak'
    ans = 0
    frogs = 0
    count = [0] * 5

    for c in croakOfFrogs:
      count[kCroak.index(c)] += 1
      if any(count[i] > count[i - 1] for i in range(1, 5)):
        return -1
      if c == 'c':
        frogs += 1
      elif c == 'k':
        frogs -= 1
      ans = max(ans, frogs)

    return ans if frogs == 0 else -1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.