AlgoMaster Logo

Maximum Number of Balloons

easyFrequency4 min readUpdated June 23, 2026

Understanding the Problem

We need to figure out how many times we can spell out "balloon" using the characters available in the input string. Each character in the string can only be used once.

Not every letter in "balloon" appears the same number of times: b appears once, a appears once, l appears twice, o appears twice, and n appears once. So having plenty of b's and a's is not enough on its own. We need twice as many l's and o's to match them.

This makes the answer a bottleneck problem. We can build one "balloon" for every copy of each required character we have, after dividing the l and o counts by 2. Whichever character runs out first caps the total.

Key Constraints:

  • 1 <= text.length <= 10^4 → Counting characters in a single pass is O(n), which is comfortable at this size. There is no need for anything fancier.
  • text consists of lowercase English letters only → We only need to track 26 possible characters, and only 5 of them (b, a, l, o, n) affect the answer.

Approach 1: Character Frequency with Array

Intuition

Count every character in the string, then check how many complete "balloon"s those characters can assemble.

Since "balloon" uses b(1), a(1), l(2), o(2), n(1), only these five characters matter. For 'l' and 'o', we divide their counts by 2 because each "balloon" consumes two of each. The character that runs out first determines the answer.

Algorithm

  1. Create a frequency array of size 26 (one slot per lowercase letter) and count every character in the input string.
  2. Look up the counts for b, a, l, o, and n.
  3. Divide the count of 'l' by 2 and the count of 'o' by 2 (integer division), since each "balloon" needs two of each.
  4. Return the minimum of these five values.

Example Walkthrough

text
1Count all characters in "loonbalxballpoon"
0
l
1
o
2
o
3
n
4
b
5
a
6
l
7
x
8
b
9
a
10
l
11
l
12
p
13
o
14
o
15
n
counting
charCounts (relevant)
1Building frequency array from string...
1/8

Code

This is already optimal in time and space. The one drawback is that the five hardcoded character lookups only work for "balloon". The next approach expresses the same logic in terms of the target word itself, so it generalizes to any word.

Approach 2: Hash Map with Target Word Frequency

Intuition

Instead of hardcoding the five character checks, count the characters in both the input text and the target word "balloon". Then for each character the target needs, divide its supply (count in the text) by its demand (count in the target). The minimum across all those divisions is the answer. The only change from Approach 1 is that the per-character divisor comes from the target word instead of being written out by hand, so the same code handles any target word.

Algorithm

  1. Build a frequency map for the input string.
  2. Build a frequency map for the word "balloon".
  3. For each character in "balloon"'s frequency map, divide the input count by the required count.
  4. Return the minimum of all these divisions.

Example Walkthrough

text
1Count characters in "nlaebolko"
0
n
1
l
2
a
3
e
4
b
5
o
6
l
7
k
8
o
counting
textCount
1Building frequency map for input text...
1/9

Code