1189. Maximum Number of Balloons - Explanation

Problem Link

Description

You are 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.

Example 1:

Input: text = "nlaebolko"

Output: 1

Example 2:

Input: text = "loonbalxballpoon"

Output: 2

Example 3:

Input: text = "neetcode"

Output: 0

Constraints:

  • 1 <= text.length <= 10,000
  • text consists of lower case English letters only.

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Hash Map / Dictionary - Storing and retrieving key-value pairs for frequency counting
  • Character Frequency Counting - Counting occurrences of each character in a string
  • Finding Minimum Across Values - Determining the limiting factor from multiple constraints

1. Hash Map - I

Intuition

To form the word "balloon", we need specific counts of each letter: one each of 'b', 'a', 'n', and two each of 'l' and 'o'. The number of times we can spell "balloon" is limited by whichever required letter runs out first. By counting all letters in the text and then dividing by the required amounts, we find how many complete words we can form.

Algorithm

  1. Count the frequency of each character in the input text using a hash map.
  2. Create a hash map for the word "balloon" with the required count of each character.
  3. For each character in "balloon", calculate how many times it can satisfy the requirement by dividing the available count by the required count.
  4. Return the minimum across all characters, as this determines the bottleneck.
class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        countText = Counter(text)
        balloon = Counter("balloon")

        res = len(text)
        for c in balloon:
            res = min(res, countText[c] // balloon[c])
        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) since we have at most 2626 different characters.

2. Hash Map - II

Intuition

We can optimize by only counting the five relevant characters ('b', 'a', 'l', 'o', 'n') instead of all 26 letters. After counting, we adjust for 'l' and 'o' by dividing their counts by 2 since each "balloon" requires two of each. The minimum count then gives our answer directly.

Algorithm

  1. Iterate through the text and only count characters that appear in "balon" (the unique letters of "balloon").
  2. If fewer than 5 distinct characters are counted, return 0 (cannot form even one "balloon").
  3. Divide the counts of 'l' and 'o' by 2 to account for needing two of each.
  4. Return the minimum value among all five character counts.
class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        mp = defaultdict(int)
        for c in text:
            if c in "balon":
                mp[c] += 1

        if len(mp) < 5:
            return 0

        mp['l'] //= 2
        mp['o'] //= 2
        return min(mp.values())

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) since balloonballoon has 55 different characters.

Common Pitfalls

Forgetting Double Letters

The word "balloon" contains two ls and two os. A common mistake is treating all characters equally without dividing the counts of l and o by 2. This results in overcounting the number of possible words that can be formed.

Not Checking for Missing Characters

If any of the five required characters (b, a, l, o, n) is missing from the text, the answer is 0. Failing to handle this case (for example, by taking the minimum of an empty collection or dividing by zero) can cause runtime errors or incorrect results.