Valid Parentheses

Given a string s containing only the characters '(', ')', '{', '}', '[', and ']', determine if the input string is valid.

A string is valid if:

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

Return true if s is valid, otherwise return false.

Example 1
Inputs = "()"
Outputtrue
The single pair of parentheses is properly opened and closed.
Example 2
Inputs = "()[]{}"
Outputtrue
Each bracket type is properly opened and closed in order.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of parentheses only: '(', ')', '{', '}', '[' and ']'

Asked at 117 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate