< Back




1189. Maximum Number of Balloons

Given a string, how many times can we create the word "balloon" from the string. To solve this we
just use the Counter class from the collections library in Python. We acquire a count of each
character in the string. Then we find the minimum count of each character in the word "balloon",
accounting for "l" and "o", which need to be accounted for twice - we do this by divinding their
counts by 2.

The minimum count for any particular character in the string "balloon" determines the number of times
we can create "balloon" from the given input.

The solution is as follows:


  from collections import Counter

  class Solution:
      def maxNumberOfBalloons(self, text: str) -> int:
          count = Counter(text)
          return min(count["b"], count["a"], count["l"] // 2, count["o"] // 2, count["n"])


_ Time Complexity:

  O(n) - We count all occurrences of character in the input.

_ Space Complexity:

  O(n) - We store the occurrence of each character in the input.