[Leet Code] Maximum Number Of Balloons

Matthew Boyd
2 min readFeb 16, 2021

--

Leetcode: https://leetcode.com/problems/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.

Example 1:

Input: text = "nlaebolko"
Output: 1

Example 2:

Input: text = "loonbalxballpoon"
Output: 2

Example 3:

Input: text = "leetcode"
Output: 0

Constraints:

  • 1 <= text.length <= 10^4
  • text consists of lower case English letters only.

Solution:

class Solution(object):
def maxNumberOfBalloons(self, text):
"""
:type text: str
:rtype: int
"""

b = text.count("b")
a = text.count("a")
l = text.count("l")
o = text.count("o")
n = text.count("n")
print b, a,l,o,n
if b == 0 or a == 0 or l == 0 or o ==0 or n == 0:
return 0
else:
return min(b,a,l /2, o/2,n)

Explanation:

We want to count all of the unique characters.

We want to check if any of the values are 0, then the result will be 0, if not, we take all the characters (making sure to divide the l and o by 2, as they occur twice each in the word balloon and we get the minimum value of this. This ensure that if there are 11 o’s in the word, but only 2 b’s, we can still only make balloon twice.

--

--

Matthew Boyd
Matthew Boyd

Written by Matthew Boyd

Learning, and posting my findings!

No responses yet