Maximum Number of Balloons (LeetCode 1189): The Bottleneck Letter

Given a string text, how many times can you form the word "balloon" using each character at most once? For text = "loonbalxballpoon" the answer is 2.

Count, then divide by need

from collections import Counter

def max_number_of_balloons(text):
    have = Counter(text)
    need = Counter("balloon")   # b:1 a:1 l:2 o:2 n:1
    return min(have[c] // k for c, k in need.items())

Each copy of "balloon" needs one b, one a, two l, two o and one n. The number of full copies is limited by whichever letter runs out first, so take the minimum of have // need across those five letters. O(n) time, O(1) space (fixed alphabet).

What to say out loud

This "resource bottleneck" pattern also shows up in Ransom Note (383) and Rearrange Characters to Make Target String (2287).

For live practice on explaining patterns like this, try Linkjob.ai, an AI interview assistant. (Disclosure: I work on Linkjob AI.)