Ctrl K

How Many Frogs in the Pond

Medium
A microphone left by a pond overnight writes every sound down as a letter. Every frog calls the same way: it says the five letters c, r, o, a, k in exactly that order. Frogs call at the same time, so their letters are interleaved in one string recording, yet each frog's own letters keep their order. Once a frog finishes a call, it may start another one. Return the smallest number of frogs that could have produced this recording. If the recording cannot be split entirely into complete croak calls, return -1.
Example 1
recording="ccrorakoak" → 2

The second frog starts its croak before the first one has finished, so two of them sound at once: c·ro·ak··· and ·c··r··oak.

Example 2
recording="croakcroak" → 1

The same frog can croak twice in a row.

Example 3
recording="croakc" → -1

The last croak stops after its first letter, so the recording is impossible and the answer is -1.

Constraints
  • 1 ≤ len(recording) ≤ 10⁵
  • recording contains only the letters c, r, o, a, k
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Hint for a Digit Guess MediumCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumDomain Visit Rollup Medium
loading Python…
Input
recording="ccrorakoak"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 199 tests on the server, hidden ones included.