1189. Maximum Number of Balloons

Source code notebook Author Update time

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^4
  • text consists of lower case English letters only.
# @lc code=start
using LeetCode

function max_num_of_ballons(text::String)::Int
    words = Dict{Char,Int}(s => 0 for s in "balon")
    for s in text
        if haskey(words, s)
            words[s] += 1
        end
    end
    return min(words['b'], words['a'], words['l'] ÷ 2, words['o'] ÷ 2, words['n'])
end
# @lc code=end
max_num_of_ballons (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.