Blind 75
16 / 75Free practice

Valid Parentheses

Easy · Stack

Jump to code

Given a string made of (), [] and {}, decide whether every opener has a matching closer in the correct nested order. The empty string is valid.

Examples

Example 1

s = "()"

Output: true

Example 2

s = "()[]{}"

Output: true

Constraints

  • 1 <= s.length <= 10^4
  • s consists only of parentheses characters ()[]{}
Need a hint?

The next closing bracket must match the most recent unmatched opener.

Asked in real interviews

Candidates reported versions of Valid Parentheses from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "()"
]
Expected
true
Case 2
Input
[
  "()[]{}"
]
Expected
true
Case 3
Input
[
  "(]"
]
Expected
false
Case 4
Input
[
  "("
]
Expected
false
Case 5
Input
[
  "{[]}"
]
Expected
true

Drafts are saved per language in this browser when storage is available.