Problems › Stacks & queues

Valid Parentheses

Difficulty: Easy · Topic: Stacks & queues

Given a string s containing only (), [] and {}, return True if every bracket is closed by the matching type in the correct order, else False.

Examples

is_valid("()")
→ true
is_valid("()[]{}")
→ true
is_valid("(]")
→ false

'(' is closed by ']'.

Target complexity: time O(n), space O(n)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Stacks & queues problems