관리 메뉴

솜씨좋은장씨

[leetCode] 1189. Maximum Number of Balloons (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1189. Maximum Number of Balloons (Python)

솜씨좋은장씨 2020. 9. 20. 00:33
728x90
반응형

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

from collections import Counter

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        cnt = collections.Counter(text)
        
        ballon_key = ["b", "a", "l", "o", "n"]
        
        for key in ballon_key:
            if key not in list(cnt.keys()):
                return 0
        
        return min(cnt['b'],cnt['a'],cnt['l']//2,cnt['o']//2,cnt['n'])

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments