LeetCode: Maximum Number of Balloons
Question
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.
Solution
def max_number_of_balloons(text)
text_dict = Hash.new(0)
text.chars.each do |chr|
if chr == "b" || chr == "a" || chr == "l" || chr == "o" || chr == "n"
text_dict[chr] += 1
end
end
text_dict.each_pair do |k,v|
if k == "l" || k == "o"
text_dict[k] = v/2
end
end
text_dict.values.uniq.min
end
Comments