File: maximum-number-of-balloons/solution.py

Date: 2026-06-06

Time: 17:38

maximum-number-of-balloons/solution.py

Purpose

This file solves LeetCode #1189 — Maximum Number of Balloons. Given a string of lowercase English letters, it determines how many times the word "balloon" can be spelled using the available characters. It owns both the solution and its inline test suite.

Key Components

maxnumberof_balloons(text: str) -> int — The sole solver function. It counts character frequencies in text, then returns the minimum number of complete "balloon" instances those frequencies can support.

The word "balloon" requires: b(1), a(1), l(2), o(2), n(1). The function expresses this directly by floor-dividing l and o counts by 2 while taking the other counts as-is, then returning the min across all five.

TestMaxNumberOfBalloons — Nine test cases covering LeetCode examples, edge cases (empty string, no matching characters, partial characters), and the critical case where a doubled letter (l) is the bottleneck.

Patterns

Counter-based frequency analysis — the standard idiom for "how many times can I form word X from string Y." Rather than building a Counter for "balloon" and computing min(textcount[c] // ballooncount[c]), this solution hardcodes the target frequencies. That's simpler for a fixed target word but wouldn't generalize (see rearrange-characters-to-make-target-string/solution.py for the generalized version).

Implicit zero from CounterCounter returns 0 for missing keys, so there's no need for .get(c, 0). If text has no 'b', count['b'] is 0 and min(...) correctly returns 0.

Dependencies

Imports: collections.Counter (frequency counting), unittest (inline tests).

Imported by: The massive Imported By list is misleading — those are test files across the entire repo that likely share a common test runner or import pattern, not files that actually call maxnumberofballoons. The real consumer is maximum-number-of-balloons/testsolution.py.

Flow

1. Counter(text) builds a frequency map in O(n) time, one pass.

2. min(...) computes the bottleneck across the five required characters. Each lookup is O(1).

3. The // 2 on l and o accounts for "balloon" needing two of each.

Total: O(n) time, O(1) space (the counter has at most 26 keys).

Invariants

Error Handling

None. The function trusts its input. An empty string yields 0 naturally through Counter's default-zero behavior. Non-string inputs would raise at the Counter call.