Valid Parentheses (Simple)

Medium~15 min

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

A string is valid if:

  1. Every opening bracket has a corresponding closing bracket of the same type.
  2. Brackets are closed in the correct order.
  3. Every closing bracket has a corresponding opening bracket of the same type.

An empty string is considered valid.

Examples

Example 1
Input: s = "()"
Output: true
Explanation: Single pair of matching parentheses.
Example 2
Input: s = "()[]{}"
Output: true
Explanation: Three pairs of matching brackets, each closed in order.
Example 3
Input: s = "(]"
Output: false
Explanation: Opening ( does not match closing ].
Example 4
Input: s = "([)]"
Output: false
Explanation: Brackets are not closed in the correct order.
Example 5
Input: s = "{[]}"
Output: true
Explanation: Nested brackets are properly matched.

Constraints

  • 0 <= s.length <= 10^4
  • s consists only of characters ()[]{}
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run