AceYourInterviews
Checking sign-in…

41. Valid Parentheses

IntermediateTyped unmatched-opening stack

Check whether every opening bracket is closed by the matching bracket type and whether pairs are nested correctly. All brackets in the string must participate in a complete matching.

Having equal opening and closing counts is insufficient. A closing bracket must match the most recent opening bracket that has not yet been closed.

Example 1

Input

"([{}])"

Output

true

Explanation: The innermost curly pair closes before square and round pairs.

Example 2

Input

"([)]"

Output

false

Explanation: The round closer cannot close the currently unmatched square opening.

Constraints

  • 0 <= s.length <= 105
  • s contains only the bracket characters ()[]{}.
Hints
Hint 1

Which unmatched opening must the next closer match?

Hint 2

What information does a count lose when bracket types differ?

Hint 3

Push openings, match the top on each closer, and require emptiness at completion.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Match nested types in reverse opening order

"([{}])"
true