41. Valid Parentheses
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.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Match nested types in reverse opening order
"([{}])"true