当前位置:网站首页>Leetcode daily question solution: 1189 Maximum number of "balloons"

Leetcode daily question solution: 1189 Maximum number of "balloons"

2022-07-06 00:58:00 Xiaoxinxin's Xiaozhai

Give you a string text, You need to use text To piece together as many words as possible "balloon"( balloon ).

character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together "balloon". Example 1:

Input :text = "nlaebolko"
Output :1
Example 2:

Input :text = "loonbalxballpoon"
Output :2
Example 3:

Input :text = "leetcode"
Output :0
 

Their thinking : Statistics first text in b a l o n The number of these five letters , I borrowed it here python Of Counter function , Then check the dictionary a The length of The length is less than 5 Words It must be impossible to gather enough balon So the output 0 If it is greater than 5 Words Put the l and o Divide the number of two by 2 So I beg b a l o n The minimum value of these five letters can be obtained balloon The minimum value of .

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        a = Counter(i for i in text if i in "balon") # Calculation text in  b a l o n  The number of these five letters 
        if len(a)<5:        # If a The length of is less than 5 It means that the length is not enough , Must output as 0
            return 0
        a["l"]= a["l"] // 2  # Because in balloon in  l o  Both letters have two   So I will  l  and  o Divided by 2 
        a["o"]= a["o"] // 2
        return min(a.values())  # Then seek  b a l o n The minimum of these five letters   That is, you can get balloon Minimum number of 

原网站

版权声明
本文为[Xiaoxinxin's Xiaozhai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140158450605.html