Valid Parentheses

Question

Given a string s containing just the characters (, ),{, }, [, and ], return whether it is valid.

An input string is valid if all of following are true:

  • Open brackets are closed by the same type of brackets.
  • Open brackets are closed in the correct order.
  • Every close bracket has a corresponding open bracket of the same type.

Input: s = ()[]{}

Output: True

Input: s = [()]{}

Output: True

Input: s = (]

Output: False

Input: s = ([)]

Output: False

Input: s = [()]{}

Output: True

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

Which of the following is valid?
{[()}]
[{{}}()]()
[{(()}]
]}{[

Take a moment to understand the problem and think of your approach before you start coding.