https://leetcode.com/problems/maximum-number-of-balloons/
你會得到一個字串text,你要用字串裡的字母組合出"balloon",每個字串裡的字母只能用一次,請回傳能組出幾個"balloon"
只要計算字串裡有幾個'b'、'a'、'l'、'o'、'n',在把這5個字母的出現次數除以組合所需的次數,並回傳除出來最小的那個就好
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
balloon = {'b':0, 'a':0, 'l':0, 'o':0, 'n':0}
for i in text:
if i in balloon:
balloon[i] += 1
return min(balloon['b'], balloon['a'], balloon['l'] // 2, balloon['o'] // 2, balloon['n'])
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
balloon = Counter(text)
return min(balloon['b'], balloon['a'], balloon['l'] // 2, balloon['o'] // 2, balloon['n'])
今天覺得很睏了,感謝LeetCode出了一題簡單又好講的題目
大家晚安