AlgoMaster Logo

Maximum Number of Balloons

Last Updated: April 6, 2026

easy
3 min read

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.

The word "balloon" is interesting because not every letter appears the same number of times: b appears once, a appears once, l appears twice, o appears twice, and n appears once. So even if we have plenty of b's and a's, we need twice as many l's and o's to keep up.

The key observation is: the answer is bottlenecked by whichever required character is in shortest supply, after accounting for how many of that character each "balloon" needs.

Key Constraints:

  • 1 <= text.length <= 10^4 → With n up to 10,000, even O(n^2) would work fine, but O(n) is the natural fit here since we just need to count characters.
  • text consists of lowercase English letters only → We only need to track 26 possible characters (or even just the 5 that matter: b, a, l, o, n).

Approach 1: Character Frequency with Array

Intuition

The most direct way to think about this: count every character in the string, then check how many complete "balloon"s we can assemble.

Since "balloon" uses b(1), a(1), l(2), o(2), n(1), we just need to count these five characters in the input. For 'l' and 'o', we divide their counts by 2 since each "balloon" consumes two of each. The bottleneck character, the one that runs out first, determines our 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 approach is already optimal in time and space, but the code is a bit verbose with all the individual character lookups. What if we generalized the logic so it works for any target word, not just "balloon"?

Approach 2: Hash Map with Target Word Frequency

Intuition

Instead of hardcoding the five character checks, we can generalize the approach. Count the characters in both the input text and the target word "balloon", then for each character in the target, compute how many times its supply covers its demand. The minimum across all characters is our answer.

This approach is cleaner and would work for any target word, not just "balloon".

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