Balanced Braces Algorithm

Posted on Posted in Java

Typical problem scenario:

  • For every opening brace (i.e., (, {, or [), there is a matching closing brace (i.e., ), }, or ]) of the same type (i.e., ( matches ), { matches }, and [ matches ]). An opening brace must appear before (to the left of) its matching closing brace. For example, ]{}[ is not balanced.
  • No unmatched braces lie between some pair of matched braces. For example, ({[]}) is balanced, but {[}] and [{)] are not balanced.

Algorithm used:

Possible implementation: 

Leave a Reply