Take an input string with X opening brackets [ and Y closing brackets ], in a random order.
Determine if the generated string of brackets is balanced, that is it consists of pairs of opening/closing brackets in the correct order with no matched opening and closing pairs.
Do not worry about input other than brackets and empty string.
Examples
(empty) “” OK
[] OK
[][] OK
[[]] OK
[[[][]]] OK
][ FAIL
][][ FAIL
[][]][ FAIL
Add parentheses () to the validation. Brackets and parentheses can be combined at any way while they respect the opening/closing rule.
Examples
[) FAIL
()[] OK
[(]) FAIL
([()]) OK
Add curly braces {} to the validation. They can be combined following the same previous rules
Examples
[]{ FAIL
[]{}() OK
[(){]} FAIL
[({})] OK
Also validate the order.
- Parentheses can only contain parentheses or nothing
- Brackets can contain brackets, parentheses or nothing
- Curly braces can contain curly braces, brackets, parentheses or nothing
Examples
([()]) FAIL
[]{}() OK
[(){}] FAIL
[()[]] OK
{[()]} OK