#2086

Minimum Number of Food Buckets to Feed the Hamsters

specialist · 900 · lc medium +31 · verified · 48% accepted · 572 likes · top 33%

Description

A string hamsters contains 'H' (hamster) and '.' (empty slot). Place food buckets in empty slots so that each hamster has at least one bucket immediately adjacent (at index i - 1 or i + 1). Return the minimum number of buckets needed, or -1 if it is impossible to feed every hamster.

Example 1:

Input: hamsters = "H..H"
Output: 2
Explanation: We place two food buckets at indices 1 and 2.
It can be shown that if we place only one food bucket, one of the hamsters will not be fed.

Example 2:

Input: hamsters = ".H.H."
Output: 1
Explanation: We place one food bucket at index 2.

Example 3:

Input: hamsters = ".HHH."
Output: -1
Explanation: If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.

Code

1
2
3