Skip to content

Practice Valid Parentheses

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#20
LeetCode Problem

Valid Parentheses

Problem Statement

A string contains only (), [], and {} brackets. Decide whether all brackets form a valid nesting: each closing bracket must match the type of the most recent unmatched opening bracket, and no bracket may remain unmatched. Adjacent complete groups are allowed. Practice approach: Push opening brackets on a stack. For a closer, reject an empty stack or a mismatched top; otherwise pop. Accept only if the stack is empty after the scan. Time and worst-case space are O(n).

Source: Valid Parentheses on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "()"
Output
true

# Example 2

Input
s = "()[]{}"
Output
true

# Example 3

Input
s = "(]"
Output
false

# Example 4

Input
s = "([])"
Output
true

# Example 5

Input
s = "([)]"
Output
false

Constraints

1 <= s.length <= 10000
s contains only (, ), [, ], {, and }.

Related LeetCode Problems

Practice more problems similar to Valid Parentheses.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.