Skip to content

Latest commit

 

History

History
124 lines (104 loc) · 3.5 KB

1419. Minimum Number of Frogs Croaking.md

File metadata and controls

124 lines (104 loc) · 3.5 KB

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 croak in the given string.

A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of 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.

Example 4:

Input: croakOfFrogs = "croakcroa"
Output: -1

Constraints:

  • 1 <= croakOfFrogs.length <= 10^5
  • All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.

Solution

  • mine
    • Java

      Runtime: 8 ms, faster than 83.76%, Memory Usage: 39.9 MB, less than 85.81% of Java online submissions

      //O(N)time O(1)space
      public int minNumberOfFrogs(String croakOfFrogs) {
          if (croakOfFrogs == null || croakOfFrogs.length() < 5) {
              return -1;
          }
          int[] record = new int[5];
          int res = 0;
          char[] arr = croakOfFrogs.toCharArray();
          for (char c : arr) {
              switch (c) {
                  case 'c':
                      record[0] = record[0] + 1;
                      break;
                  case 'r':
                      record[1] = record[1] + 1;
                      break;
                  case 'o':
                      record[2] = record[2] + 1;
                      break;
                  case 'a':
                      record[3] = record[3] + 1;
                      break;
                  case 'k':
                  default:
                      record[4] = record[4] + 1;
                      break;
              }
              //require c >= r >= o >= a >= k 
              if (record[0] < record[1] || record[1] < record[2]
                      || record[2] < record[3] || record[3] < record[4]) {
                  return -1;
              }
              //max(c - k)
              res = Math.max(res, record[0] - record[4]);
          }
          int t = record[0];
          //require  c=r=o=a=k
          for(int i : record){
              if(t != i){
                  return -1;
              }
          }
          return res;
      }
      

  • the most votes
    • Runtime: 10 ms, faster than 73.73%, Memory Usage: 39.7 MB, less than 95.19% of Java online submissions
      public int minNumberOfFrogs(String croakOfFrogs) {
          int cnt[] = new int[5];
          int frogs = 0, max_frogs = 0;
          for (var i = 0; i < croakOfFrogs.length(); ++i) {
              var ch = croakOfFrogs.charAt(i);
              var n = "croak".indexOf(ch);
              ++cnt[n];
              if (n == 0)
                  max_frogs = Math.max(max_frogs, ++frogs);
              else if (--cnt[n - 1] < 0)
                  return -1;
              else if (n == 4)
                  --frogs;
          }
          return frogs == 0 ? max_frogs : -1;    
      }