DSA AnimatorDSA animations
Maximum Number of Balloons LC #1189 Easy Frequency Map · Min Bottleneck
Problem

Given a string text, return the maximum number of instances of the word "balloon" that can be formed using letters in text. Each letter can only be used once.

Example 1
Input: text = "nlaebolko"
Output: 1
Example 2
Input: text = "loonbalxballpoon"
Output: 2
Constraints: 1 ≤ text.length ≤ 10⁴ | lowercase English letters only
Try Examples
Custom:
Approach
Count b, a, l, o, n in text — divide l & o by 2
"balloon" needs b×1 a×1 l×2 o×2 n×1. Answer = min of effective counts. The scarcest letter is the bottleneck. O(n) time, O(1) space.
Text Character Strip
b
×1
a
×1
l
×2
o
×2
n
×1
Letter Counters & Bottleneck
b
0
a
0
l
0
raw 0
o
0
raw 0
n
0
min(b,a,l÷2,o÷2,n)
—
Scan text to count letters
Variables
i
—
char
—
b
0
a
0
ans
—
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
"balloon" needs: b×1 a×1 l×2 o×2 n×1
2
Count frequency of each required letter in text
3
l and o appear twice in "balloon" → divide counts by 2
4
Return min(freq[b], freq[a], freq[l]/2, freq[o]/2, freq[n])
Time
O(n)
Space
O(1)
Why This Works

Each "balloon" consumes one b, one a, two l's, two o's, and one n. The number you can spell is limited by whichever letter runs out first — the bottleneck. Dividing l and o by 2 converts raw counts into "how many balloons" each letter supports.

How to solve Maximum Number of Balloons

LeetCode 1189 · Easy · Strings · Pattern: Frequency Count

Why the obvious approach is slow

Building the word over and over, and removing letters each time, is slow and messy. Counting the letters answers it directly.

The key idea

Count each letter of the text. Balloon needs one b, one a, two l, two o and one n. So divide the counts of l and o by 2, and the answer is the smallest result.

Remember

How many words can I build means the scarcest letter decides. Divide by how many times the word needs each letter.

Run the animation above on your own input, or watch the 1:06 video explanation of Maximum Number of Balloons with a dry run and Java code.

More Strings problems