Microverse Challenge

Balanced Brackets

There are 3 kinds of Brackets: [] {} (). Given a String of characters, check if all the brackets in the String are Balanced. A string is balanced if all the start and end brackets are in a correct order so they match each other. Here are some balanced Strings:

  • {}
  • (hello)[world]
  • [({}{}{})([])]

Here are some Unbalanced ones:

  • (hello - no ending )
  • ([)] - The [ is improperly enclosed in the ().
  • )( - There's an ending ) without a ( before it.

Return true if a line is balanced and false otherwise.

Examples

The following are 3 test cases:

puts balanced_brackets?('(hello)[world]')
# => true

puts balanced_brackets?('([)]')
# => false

puts balanced_brackets?('[({}{}{})([])]')
# => true