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.

&nbsp;
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.


&nbsp;
Constraints:


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


class Solution:
    def minNumberOfFrogs(self, croakOfFrogs: str) -> int:
        c = r = o = a = k = max_frog_croak = present_frog_croak = 0
        # need to know, at particular point,
        # what are the max frog are croaking,

        for i, v in enumerate(croakOfFrogs):
            if v == 'c':
                c += 1
				# c gives a signal for a frog
                present_frog_croak += 1
            elif v == 'r':
                r += 1
            elif v == 'o':
                o += 1
            elif v == 'a':
                a += 1
            else:
                k += 1
				# frog stop croaking
                present_frog_croak -= 1

            max_frog_croak = max(max_frog_croak, present_frog_croak)
            # if any inoder occurs;
            if c < r or r < o or o < a or a < k:
                return -1

        # if all good, else -1
        if present_frog_croak == 0 and c == r and r == o and o == a and a == k:
            return max_frog_croak
        return -1
class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
        int[] index = new int[26];
        String corak = "croak";

        // Giving index to each characters
        for (int i = 0; i < corak.length(); ++i)
            index[corak.charAt(i) - 'a'] = i;

        int ans = 0, sum = 0;
        int[] count = new int[5];

        for (char c : croakOfFrogs.toCharArray()) {
            int i = index[c - 'a'];
            // If it is not 'c' it will decrease the sum
            if (c != 'c') {
                if (count[i - 1]-- <= 0) return -1;
                sum--;
            }
            // If it is not 'k' it will increase the sum
            if (c != 'k') {
                count[i]++;
                sum++;
            }
            ans = Math.max(ans, sum);
        }
        return sum == 0 ? ans : -1;
    }
}
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        unordered_map<char,int> mp;
        int size=croakOfFrogs.size();
        
        for(int i=0;i<size;i++)
        {
            mp[croakOfFrogs[i]]++;
            if(mp['c']<mp['r'] || mp['r']<mp['o'] || mp['o']<mp['a'] || mp['a']<mp['k'])
                return -1;
        }
        
        int count=mp[croakOfFrogs[0]];
        for(auto i:croakOfFrogs)
        {
            if(count!=mp[i])
                return -1;
        }
        
        int ans=0;
        int i=0;
        int temp=0;
        
        for(i=0;i<size;i++)
        {
            if(croakOfFrogs[i]=='c')
            {
                temp++;
            }
            else if(croakOfFrogs[i]=='k')
            {
                temp--;
            }
            if(temp>ans)
                ans=temp;
        }
        return ans;
    }
};
var minNumberOfFrogs = function(croakOfFrogs) {
    const croakArr = new Array(5).fill(0); //Array to store occurence of each char
    let overlap = 0;  //Store the number of overlaps
    for(let i = 0; i < croakOfFrogs.length; i++) {
        switch(croakOfFrogs[i]) {
            case 'c':
                croakArr[0] += 1;
                //Check if new start, is overlapping with others
                if((croakArr[0] - croakArr[4] - overlap) > 1) {
                    ++overlap;
                }
                break;
            case 'r':
                //Condition to check if r comes before c
                if(croakArr[0] <= croakArr[1]) return -1;
                croakArr[1] += 1;
                break;
            case 'o':
                //Condition to check if o comes before r
                if(croakArr[1] <= croakArr[2]) return -1;
                croakArr[2] += 1;
                break;
            case 'a':
                //Condition to check if a comes before o
                if(croakArr[2] <= croakArr[3]) return -1;
                croakArr[3] += 1;
                break;
            case 'k':
                //Condition to check if k comes before a
                if(croakArr[3] <= croakArr[4]) return -1;
                croakArr[4] += 1;
                break;
        }
    }
    //Check if all items of array have same count else return -1
    //If all items have same count return overlap + 1
    return (Math.max(...croakArr) === Math.min(...croakArr)) ? overlap + 1 : -1;
};
Minimum Number of Frogs Croaking