Introduction
LeetCode 1189, Maximum Number of Balloons, is a beginner-friendly string and frequency-counting problem that teaches an important interview concept:
Count only the characters that matter and determine the limiting factor.
The challenge is to find how many times the word "balloon" can be formed using the characters available in a given string. Each character can be used at most once.
This problem is an excellent introduction to:
- HashMap frequency counting
- Character frequency analysis
- Greedy counting techniques
- Finding bottleneck characters
Problem Statement
Problem Link -: Maximum Number of Balloons
Given a string:
Determine the maximum number of times the word:
can be formed.
Each character from the original string can only be used once.
Return the maximum number of complete instances of the word.
Example 1
Input
Output
Explanation
Available characters:
Exactly one "balloon" can be formed.
Example 2
Input
Output
Explanation
The string contains enough characters to create:
Two complete words can be formed.
Example 3
Input
Output
Explanation
The required characters for "balloon" are not all present.
Key Observation
The target word is:
Character requirements:
| Character | Required Count |
| b | 1 |
| a | 1 |
| l | 2 |
| o | 2 |
| n | 1 |
Notice:
These characters become important because they require more occurrences than the others.
Approach 1: Simulate Building Balloons
Idea
- Count occurrences of relevant characters.
- Continuously check whether a complete "balloon" can be formed.
- If possible:
- Increment answer.
- Remove required characters.
- Repeat until impossible.
Java Solution
Dry Run
Input
Frequency Count
First Balloon
Consume:
Answer:
Second Balloon
Consume again:
Answer:
No more balloons can be formed.
Final result:
Can We Optimize Further?
Yes.
Your solution repeatedly removes characters to form balloons.
But we don't actually need simulation.
Instead, we can directly calculate the maximum number of balloons.
Approach 2: Frequency Division (Optimal)
Key Insight
Suppose frequencies are:
Since:
Possible balloons:
The smallest value determines the answer:
Therefore:
Optimized Java Solution
Approach 3: HashMap + Minimum Formula
If you prefer HashMaps:
Count frequencies and apply the same minimum formula.
This keeps the logic intuitive while avoiding repeated simulation.
Complexity Analysis
Simulation Approach
Time Complexity
The second loop runs at most n times.
Space Complexity
Only a few characters are stored.
Optimal Frequency Division Approach
Time Complexity
Single traversal.
Space Complexity
Fixed-size frequency array.
Interview Discussion
An interviewer may ask:
Why divide l and o by 2?
Because the word contains:
and requires:
for every single occurrence.
Therefore:
Common Mistakes
Forgetting Duplicate Letters
Many candidates count:
and forget that:
are required.
Using Total Frequency Only
Having:
does not allow even one balloon.
Simulating Unnecessarily
While simulation works, the minimum-frequency formula is cleaner and more efficient.
Key Takeaways
- This problem is based on frequency counting.
- Only five characters matter:
- b
- a
- l
- o
- n
- Characters l and o require special handling because they appear twice.
- The optimal solution uses a frequency array and a minimum calculation.
- Understanding bottleneck resources is a common interview pattern.
Conclusion
LeetCode 1189: Maximum Number of Balloons is a simple but powerful frequency-counting problem. While a simulation-based approach works correctly, the most elegant solution comes from recognizing that the answer is determined by the limiting character count.
Mastering this pattern will help you solve many string, HashMap, and counting-based interview questions efficiently.




