Find the minimum number of frogs that can produce an interleaved croak string

Quick Overview

Given a string of interleaved croak sounds, find the minimum number of frogs that could have produced it, or return -1 when the letters cannot be split into complete croaks. It tests greedy stage counting in one pass over a string and careful validation of letter order.

Find the minimum number of frogs that can produce an interleaved croak string

Company: Zoox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given the string `croak_of_frogs`, which is a combination of the string `"croak"` from different frogs. Multiple frogs can croak at the same time, so several `"croak"` strings are mixed together. A valid `"croak"` means a frog prints the five letters `'c'`, `'r'`, `'o'`, `'a'` and `'k'` in that order. A frog has to print all five letters to finish a croak. Return the minimum number of different frogs needed to finish all the croaks in the given string. If the string is not a combination of valid croaks, return `-1`. ### Function Signature ```python def min_number_of_frogs(croak_of_frogs: str) -> int: ``` ### Rules - Every character of the string belongs to exactly one croak, and the letters of each croak appear in the string in the order `c`, `r`, `o`, `a`, `k`. - A frog makes one croak at a time. After it prints `'k'` it may start a new croak with a later `'c'`. - Return `-1` if the characters cannot be split into complete croaks: a letter has no frog ready for it, or some croak is still unfinished at the end of the string. ### Constraints - `1 <= len(croak_of_frogs) <= 10^5` - `croak_of_frogs` consists only of the characters `'c'`, `'r'`, `'o'`, `'a'` and `'k'`. ### Examples ```text Input: croak_of_frogs = "croakcroak" Output: 1 ``` One frog croaks twice in a row. It finishes the first croak at index 4, before the second `'c'` arrives at index 5. ```text Input: croak_of_frogs = "crcoakroak" Output: 2 ``` The first frog can use indices 0, 1, 3, 4, 5 and the second frog indices 2, 6, 7, 8, 9. The `'c'` at index 2 arrives while the first croak is still in progress, so one frog is not enough. ```text Input: croak_of_frogs = "croakcrook" Output: -1 ``` The string has three `'o'` characters but only two `'r'` characters, so the `'o'` at index 8 has no frog waiting for it.

Overview: Given a string of interleaved croak sounds, find the minimum number of frogs that could have produced it, or return -1 when the letters cannot be split into complete croaks. It tests greedy stage counting in one pass over a string and careful validation of letter order.

Zoox logo
Zoox
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given the string croak_of_frogs, which is a combination of the string "croak" from different frogs. Multiple frogs can croak at the same time, so several "croak" strings are mixed together.

A valid "croak" means a frog prints the five letters 'c', 'r', 'o', 'a' and 'k' in that order. A frog has to print all five letters to finish a croak.

Return the minimum number of different frogs needed to finish all the croaks in the given string. If the string is not a combination of valid croaks, return -1.

Function Signature

def min_number_of_frogs(croak_of_frogs: str) -> int:

Rules

  • Every character of the string belongs to exactly one croak, and the letters of each croak appear in the string in the order c , r , o , a , k .
  • A frog makes one croak at a time. After it prints 'k' it may start a new croak with a later 'c' .
  • Return -1 if the characters cannot be split into complete croaks: a letter has no frog ready for it, or some croak is still unfinished at the end of the string.

Constraints

  • 1 <= len(croak_of_frogs) <= 10^5
  • croak_of_frogs consists only of the characters 'c' , 'r' , 'o' , 'a' and 'k' .

Examples

Input: croak_of_frogs = "croakcroak"
Output: 1

One frog croaks twice in a row. It finishes the first croak at index 4, before the second 'c' arrives at index 5.

Input: croak_of_frogs = "crcoakroak"
Output: 2

The first frog can use indices 0, 1, 3, 4, 5 and the second frog indices 2, 6, 7, 8, 9. The 'c' at index 2 arrives while the first croak is still in progress, so one frog is not enough.

Input: croak_of_frogs = "croakcrook"
Output: -1

The string has three 'o' characters but only two 'r' characters, so the 'o' at index 8 has no frog waiting for it.