Skip to content

Instantly share code, notes, and snippets.

@crondaemon
Created September 14, 2021 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crondaemon/5fd002835f2d70bd10202e8650a037bd to your computer and use it in GitHub Desktop.
Save crondaemon/5fd002835f2d70bd10202e8650a037bd to your computer and use it in GitHub Desktop.
balloons
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