Maximum Number of Balloons - Problem

Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.

You can use each character in text at most once. Return the maximum number of instances that can be formed.

Input & Output

Example 1 — Basic Case
$ Input: text = "nlaebolko"
Output: 1
💡 Note: We have: n=1, l=2, a=1, e=1, b=1, o=2, k=1. For 'balloon' we need: b=1, a=1, l=2, o=2, n=1. We can form exactly 1 balloon.
Example 2 — Multiple Balloons
$ Input: text = "loonbalxballpoon"
Output: 2
💡 Note: We have: l=4, o=4, n=3, b=2, a=2, x=1, p=1. For 'balloon' we need 1b+1a+2l+2o+1n each. We can form 2 balloons: min(2,2,4/2,4/2,3) = 2.
Example 3 — Insufficient Characters
$ Input: text = "leetcode"
Output: 0
💡 Note: We have: l=1, e=3, t=1, c=1, o=1, d=1. Missing 'b', 'a', 'n' and insufficient 'l', 'o'. Cannot form any balloon.

Constraints

  • 1 ≤ text.length ≤ 104
  • text consists of lowercase English letters only

Visualization

Tap to expand
Maximum Number of Balloons INPUT Input String: n l a e b o l k o Target Word: "balloon" b a l l o o n Characters Needed: b:1, a:1, l:2, o:2, n:1 Input Value: text = "nlaebolko" ALGORITHM STEPS 1 Count Characters Count freq of each char 2 Check Required b,a,l,l,o,o,n needed 3 Divide by Need l,o need /2 (appear 2x) 4 Find Minimum Min limits total balloons Frequency Analysis Char Have Need Ratio b 1 1 1 a 1 1 1 l 2 2 1 o 2 2 1 n 1 1 1 FINAL RESULT We can form 1 "balloon": b a l l o o n Output: 1 OK Key Insight: The bottleneck determines the answer. Count frequency of each character in the input, then for 'l' and 'o' (which appear twice in "balloon"), divide their counts by 2. The minimum ratio across b, a, l/2, o/2, n is the answer. TutorialsPoint - Maximum Number of Balloons | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~10 min Avg. Time
890 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