알고리즘 문제/Leetcode

1189. Maximum Number of Balloons

BEstyle 2023. 3. 29. 21:46

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 <= 104
  • text consists of lower case English letters only.

#

'''
1. 아이디어 :

2. 시간복잡도 :

3. 자료구조 :

'''


class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        s1="balloon"
        chars={}
        for c in text:
            if c not in chars:
                chars[c]=1
            else:
                chars[c]+=1

        if "l" in chars:
            chars["l"]=int(chars["l"]/2)
        if "o" in chars:
            chars["o"]=int(chars["o"]/2)

        ans=float("inf")
        for s in s1:
            if s not in chars:
                return 0
            else:
                ans=min(ans, chars[s])
        return ans