题目描述:

给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 “balloon”(气球)。
字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 “balloon”。

示例:

示例 1:

输入:text = “nlaebolko”

输出:1

示例 2:

输入:text = “loonbalxballpoon”

输出:2

示例 3:

输入:text = “leetcode”

输出:0

解答代码:

  1. class Solution:
  2. def maxNumberOfBalloons(self, text: str) -> int:
  3. countList = []
  4. wordSet = set("balloon")
  5. for string in wordSet:
  6. if string == "l" or string == "o":
  7. countList.append(text.count(string) // 2)
  8. else:
  9. countList.append(text.count(string))
  10. return min(countList)

解答思路:

遍历set,注意l和n要除2。