Created
September 14, 2021 09:55
-
-
Save crondaemon/5fd002835f2d70bd10202e8650a037bd to your computer and use it in GitHub Desktop.
balloons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Balloon | |
EXPECTED = { | |
'b' => 1, | |
'a' => 1, | |
'l' => 2, | |
'o' => 2, | |
'n' => 1 | |
} | |
def initialize | |
@current = Hash.new(0) | |
end | |
def add_letter(c) | |
return true if !EXPECTED.keys.include?(c) | |
return false if @current[c] == EXPECTED[c] | |
@current[c] += 1 | |
end | |
def complete? | |
@current == EXPECTED | |
end | |
end | |
def max_number_of_balloons(text) | |
balloons = [] | |
text.each_char do |c| | |
added = false | |
balloons.each do |balloon| | |
if balloon.add_letter(c) | |
added = true | |
break | |
end | |
end | |
if added == false | |
balloon = Balloon.new | |
balloon.add_letter(c) | |
balloons << balloon | |
end | |
end | |
balloons.select{ |balloon| balloon.complete? }.count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment