DEV Community

Discussion on: Check if the chain has the same amount

Collapse
 
coreyja profile image
Corey Alexander

Ruby Solution!

def character_counts(str)
  str.chars.each_with_object(Hash.new(0)) { |char, hash| hash[char] += 1 }
end

def check(str)
  str.downcase!
  char_count = character_counts(str)
  char_count['o'] == char_count['x']
end

irb(main):003:0> check('xoxo')
=> true
irb(main):004:0> check('xaabboccddxeeffo')
=> true
irb(main):005:0> check('x')
=> false
irb(main):006:0> check('ooooox')
=> false