Skip to content

Instantly share code, notes, and snippets.

@chengfang
Last active September 6, 2024 01:06
Show Gist options
  • Save chengfang/7fd6ed260d0c572d2d61b54dc58092ad to your computer and use it in GitHub Desktop.
Save chengfang/7fd6ed260d0c572d2d61b54dc58092ad to your computer and use it in GitHub Desktop.
balanced parentheses

implement a java method and c function to check if all parentheses and brackets are balanced, i.e., there are same number of opening and closing paraentheses and brackets, every opening one has a subsequent matching closing one. The input string may contain multiple types of parentheses/brackets, such as () <> [] {}. For example,

positve cases

(a)
(a())
(bbb)()
(()())(())
abc

<a>
<a<>>
<bbb><>
<<><>><<>>

{([<abc>])}

negative cases

{
)
)(
(((}}}
(((abc))
( abc [ xyz ) 123 ]  // overlapping pairs, use stack to record all opening chars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment