[leetcode] 1189. Maximum Number of Balloons

网友投稿 792 2022-10-01

[leetcode] 1189. Maximum Number of Balloons

[leetcode] 1189. Maximum Number of Balloons

Description

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^4text consists of lower case English letters only.

分析

题目的意思是:给定字符串,求能构成’balloon’的最大数目,思路也很直接,统计词频就行了。

代码

class Solution: def maxNumberOfBalloons(self, text: str) -> int: d=collections.defaultdict(int) for ch in text: d[ch]+=1 cnt=float('inf') for ch in "balon": if(ch=='o' or ch=='l'): cnt=min(cnt,d[ch]//2) else: cnt=min(cnt,d[ch]) return cnt

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于mybatisPlus yml配置方式
下一篇:Could not find the required component ‘costmap_2d‘
相关文章

 发表评论

暂时没有评论,来抢沙发吧~