当前位置:网站首页>LeetCode:1189. The maximum number of "balloons" -- simple

LeetCode:1189. The maximum number of "balloons" -- simple

2022-07-06 21:44:00 Kinght_ one hundred and twenty-three

subject

1189. “ balloon ” Maximum number of
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

Tips :

  • 1 <= text.length <= 10^4
  • text All consist of lowercase English letters

Their thinking

  • Statistics .
  • One balloon By a b, One a, Two l, Two o, One n.
  • So we need to take l and o Number of divided by 2, Then make statistics .

Code

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        dic = Counter(ch for ch in text if ch in "balloon")
        dic['o'] //= 2
        dic['l'] //= 2
        return min(dic.values()) if len(dic) == 5 else 0

Running results

原网站

版权声明
本文为[Kinght_ one hundred and twenty-three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131122066118.html