Balanced Brackets

Published by Matt in

Write a function that takes a string of brackets and checks whether they are balanced or not. The sequence is balanced if:

  1. It contains no unmatched brackets.
  2. The subset of brackets enclosed within the confines of a matched pair of brackets is also balanced.

Examples

is_balanced("{[()]}") ➞ true

is_balanced("[()]{}") ➞ true

is_balanced("{[([)]]}") ➞ false

Notes

Return nil if no input is given.

Watch a quick demo on how Edabit works.