Leetcode 1189. Maximum Number of Balloons [Python]

年夜雪

关注

阅读 65

2022-02-17

把素材string转化为char和其个数对应的dict。遍历balloon,对每个字符在dic中的value-1,直到某个字符的储备=0的时候,返回一共遍历了几次balloon。

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        dic = collections.Counter(text)
        count = 0
        res = 0
        string = 'balloon'
        while True:
            for char in string:
                if dic[char] != 0:
                    dic[char] -= 1
                else:
                    return res
            res += 1
        return res
            

精彩评论(0)

0 0 举报