Skip to content
Decoder

Valid Parentheses

Easy

Problem

Given a string s made only of the characters ()[]{}, return true if every bracket is **closed by the same type of bracket, in the right order**. An opening bracket must be closed before any bracket opened earlier is closed.

Example 1

Input
"()[]{}"
Output
true

Example 2

Input
"{[]}"
Output
true

Example 3

Input
"([)]"
Output
false
Why
] closes while ( is still the latest open

Constraints

  • 0 <= s.length <= 10^4
  • s contains only the characters ()[]{}.
  • An empty string counts as valid.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.