1189 「氣球」 的最大數量

2021-10-10 03:18:23 字數 1420 閱讀 4590

1189. 「氣球」 的最大數量

給你乙個字串 text,你需要使用 text 中的字母來拼湊盡可能多的單詞 「balloon」(氣球)。

字串 text 中的每個字母最多只能被使用一次。請你返回最多可以拼湊出多少個單詞 「balloon」。

示例 1:

輸入:text = 「nlaebolko」

輸出:1

示例 2:

輸入:text = 「loonbalxballpoon」

輸出:2

示例 3:

輸入:text = 「leetcode」

輸出:0

注意到"balloon"可以無序,所以此題的本質是計數,其中「l」和"o"出現兩次。

使用c ol

lect

ions

.cou

nter

()

collections.counter()

collec

tion

s.co

unte

r()

class

solution

:def

maxnumberofballoons

(self, text:

str)

->

int:

cut = collections.counter(text)

return

min(cut[

'b']

,cut[

'a']

,cut[

'n']

,cut[

'l']//2

,cut[

'o']//2

)

或者使用tex

t.co

unt(

)text.count()

text.c

ount

()

class

solution

:def

maxnumberofballoons

(self, text:

str)

->

int:

return

min(text.count(

'b')

, text.count(

'a')

, text.count(

'l')//2

, text.count(

'o')//2

, text.count(

'n')

)

1189 「氣球」 的最大數量

給你乙個字串 text,你需要使用 text 中的字母來拼湊盡可能多的單詞 balloon 氣球 字串 text 中的每個字母最多只能被使用一次。請你返回最多可以拼湊出多少個單詞 balloon 示例 1 輸入 text nlaebolko 輸出 1 示例 2 輸入 text loonbalxbal...

LeetCode 1189 「氣球」 的最大數量

給你乙個字串 text,你需要使用 text 中的字母來拼湊盡可能多的單詞 balloon 氣球 字串 text 中的每個字母最多只能被使用一次。請你返回最多可以拼湊出多少個單詞 balloon 示例 1 輸入 text nlaebolko 輸出 1 示例 2 輸入 text loonbalxbal...

leetcode1189 「氣球」 的最大數量

給你乙個字串 text,你需要使用 text 中的字母來拼湊盡可能多的單詞 balloon 氣球 字串 text 中的每個字母最多只能被使用一次。請你返回最多可以拼湊出多少個單詞 balloon 示例 1 輸入 text nlaebolko 輸出 1 示例 2 輸入 text loonbalxbal...