Maximum Number of Balloons (LeetCode 1189): Solution and Explanation

Easy
Hash Table
String
Counting

Use the characters of text, each at most once, to spell the word balloon as many times as possible, and return that number.

Intuition

Because characters can be taken from anywhere in text, their order is irrelevant. All that matters is how many of each letter you have.

The word balloon uses five distinct letters: b, a and n once each, and l and o twice each. Think of five bins, one per letter. Every word you build takes one item from the b, a and n bins and two items from the l and o bins.

A bin holding c letters can feed at most c // need words. The bin that runs out first limits everything else, so the answer is the minimum of these five caps.

The doubled letters are the usual trap: with plenty of b, a, o and n but only three l's, you can build just one word, because the second word needs two more l's.

Approach: Count letters, take the tightest cap

  1. Count each lowercase letter in text with an array of 26 counters.
  2. Write down the need per word: b 1, a 1, l 2, o 2, n 1.
  3. Start with best = len(text) and lower it to count[letter] // need for each of the five letters.
  4. Return best.

Step-by-step example

text = "nlaebolko"
  1. Tally: b 1, a 1, l 2, o 2, n 1. The letters e and k are counted but unused.
  2. b: 1 // 1 = 1, so best = 1.
  3. a: 1 // 1 = 1, l: 2 // 2 = 1, o: 2 // 2 = 1, n: 1 // 1 = 1. None is smaller.
  4. Every bin supports exactly one word, so return 1.

Code: Python, Java, C++

1class Solution:
2    def maxNumberOfBalloons(self, text: str) -> int:
3        count = [0] * 26
4        for c in text:
5            count[ord(c) - ord('a')] += 1
6        need = {'b': 1, 'a': 1, 'l': 2, 'o': 2, 'n': 1}
7        best = len(text)
8        for ch, k in need.items():
9            best = min(best, count[ord(ch) - ord('a')] // k)
10        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass to count the letters, then five constant-time checks.

Space

O(1)

A fixed array of 26 counters, regardless of the text length.

Other approaches

Simulate word by word (time O(n), space O(1))

Repeatedly try to remove the letters of one balloon from the counts until some letter runs out. It gives the same answer but loops once per word, which is up to n / 7 rounds.

Generalized counter for any target word (time O(n + m), space O(1))

Count the target word too, then take the minimum of count_text[c] // count_target[c] over its letters. This handles any word, and it is the same pattern as Ransom Note with a multiplier.

Common mistakes

  • Forgetting that l and o are needed twice per word. Taking the plain minimum of the counts overestimates the answer.
  • Using float division and rounding. Use integer floor division, since half a word does not count.
  • Initializing the minimum to 0 instead of a large value, which always returns 0.

Frequently asked questions

Why does the order of letters in text not matter?

Each character can be used once from any position, so text is effectively a bag of letters. Only the counts decide how many words fit.

Can I use a hash map instead of an array?

Yes. A Counter or dictionary gives the same result. An array of 26 is a little faster and uses fixed memory.

How would this change for a different target word?

Count the target word's letters as the need, then take the minimum of available // need over every letter of the target. Balloon is just one hard-coded instance.