Maximum Number of Balloons - Problem

Imagine you're organizing a birthday party and want to spell out the word "balloon" using alphabet magnets. You have a collection of letter magnets represented by a string text, where each character can only be used once.

Your goal is to determine the maximum number of complete instances of the word "balloon" you can form using the available letters.

Example: If you have the letters "nlaebolko", you can form exactly 1 instance of "balloon" because you have all the required letters: b(1), a(1), l(2), o(2), n(1).

Note: The word "balloon" requires: 1 'b', 1 'a', 2 'l's, 2 'o's, and 1 'n'.

Input & Output

example_1.py โ€” Basic Case
$ Input: text = "nlaebolko"
โ€บ Output: 1
๐Ÿ’ก Note: The string contains exactly the letters needed for one "balloon": b(1), a(1), l(2), o(2), n(1). We can form exactly 1 complete instance.
example_2.py โ€” Multiple Balloons
$ Input: text = "loonbalxballpoon"
โ€บ Output: 2
๐Ÿ’ก Note: We have b(2), a(2), l(4), o(4), n(2). The limiting factors are b, a, and n with 2 each. We can form min(2,2,4//2,4//2,2) = 2 balloons.
example_3.py โ€” Insufficient Letters
$ Input: text = "leetcode"
โ€บ Output: 0
๐Ÿ’ก Note: The string doesn't contain the required letters for "balloon" (missing b, a, n). We cannot form any complete instance.

Visualization

Tap to expand
๐ŸŽˆ Birthday Party Letter Magnet ChallengeAvailable Letters: n-l-a-e-b-o-l-k-oBALLOONBALLOON Requirements:B ร— 1, A ร— 1, L ร— 2, O ร— 2, N ร— 1Available: B(1), A(1), L(2), O(2), N(1)Capacity: min(1, 1, 2/2, 2/2, 1) = 1 balloon๐ŸŽˆ BALLOON ๐ŸŽˆ1 complete word formed!All letters used exactly once๐ŸŽฏ Strategy: Count โ†’ Calculate โ†’ Find MinimumTime Complexity: O(n) - Single pass through lettersSpace Complexity: O(1) - Fixed size character count
Understanding the Visualization
1
Inventory Check
Count all your available letter magnets: separate them into piles by letter type
2
Requirement Analysis
Remember that BALLOON needs: 1ร—B, 1ร—A, 2ร—L, 2ร—O, 1ร—N
3
Calculate Capacity
For each letter type, determine how many complete balloon words it can support
4
Find the Bottleneck
The letter type with the smallest capacity determines your maximum balloon count
5
Form Complete Words
Use the bottleneck number to form that many complete BALLOON words
Key Takeaway
๐ŸŽฏ Key Insight: The limiting character (considering required quantities) determines the maximum number of complete words. Count once, calculate ratios, find the minimum!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string to count characters, plus constant time to calculate result

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need to store counts for 5 specific characters (b,a,l,o,n), regardless of input size

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค text.length โ‰ค 104
  • text consists of lowercase English letters only
  • Each character in text can be used at most once
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen