You are given a 0-indexed string hamsters where hamsters[i] is either:
-
'H'
, meaning there is a hamster at index
i
, or
-
'.'
, meaning index
i
is empty.
You will add some number of food buckets at the empty indices to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index i can be fed if you place a food bucket at index i - 1 and/or at index i + 1.
Return the minimum number of food buckets you should place at empty indices to feed all the hamsters, or -1 if it is impossible to feed all of them.
Function Signature
def minimum_buckets(hamsters: str) -> int:
Rules
-
A bucket can only go at an index holding
'.'
.
-
One bucket at index
j
feeds a hamster at
j - 1
and a hamster at
j + 1
at the same time.
-
A hamster at index 0 has no left neighbor, and a hamster at the last index has no right neighbor.
-
If the string has no hamsters, the answer is 0.
Constraints
-
1 <= len(hamsters) <= 10^5
-
hamsters[i]
is either
'H'
or
'.'
.
Examples
Input: hamsters = "H..H"
Output: 2
The first hamster can only use index 1 and the last can only use index 2, so both buckets are needed.
Input: hamsters = ".H.H."
Output: 1
A single bucket at index 2 sits next to both hamsters.
Input: hamsters = ".HHH."
Output: -1
The hamster at index 2 has hamsters on both sides, so no bucket can be placed next to it.