Valid Parentheses

Stacks & Monotonic Stacks, problem 1 of 7

Valid Parentheses

Easy

LC #20

stackstringmatching

Not attempted yet

You get a string s made only of the characters ( ) [ ] { }. Return True if it is valid:

  • every opener is closed by a closer of the same type,
  • brackets close in the correct order (the most recently opened one closes first),
  • every closer has a matching opener.

Example 1

Input: s = "()"
Output: True

Example 2

Input: s = "()[]{}"
Output: True

Example 3

Input: s = "(]"
Output: False
Explanation: ( is closed by the wrong type.

Example 4

Input: s = "([])"
Output: True

Constraints

  • 1 <= len(s) <= 10^5
  • s contains only ()[]{}

Python

Loading draft…

Test results

10 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

4 examples, 6 hidden

Run examples, then submit all tests.