# 给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。
#
# 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。
#
#
#
# 示例 1:
#
#
#
# 输入:text = "nlaebolko"
# 输出:1
#
#
# 示例 2:
#
#
#
# 输入:text = "loonbalxballpoon"
# 输出:2
#
#
# 示例 3:
#
# 输入:text = "leetcode"
# 输出:0
#
#
#
#
# 提示:
#
#
# 1 <= text.length <= 10^4
# text 全部由小写英文字母组成
#
# Related Topics 哈希表 字符串 计数 👍 68 👎 0
# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
cnt = Counter(text)
return min(cnt['b'], cnt['a'], cnt['l']//2, cnt['o']//2, cnt['n'])
# leetcode submit region end(Prohibit modification and deletion)
用counter两行搞定。
该博客介绍了一个Python问题,即如何在给定字符串中找到最多能组合成单词'balloon'的次数。通过使用collections.Counter类,可以简洁地计算出每个字母的出现次数,并找出限制组合数量的最少字母。例如,输入字符串'nlaebolko'能组合出1个'balloon',而'loonbalxballpoon'则能组合出2个。

被折叠的 条评论
为什么被折叠?



