31. Valid Parentheses

Easy · String

Given a string containing only parentheses characters ('(', ')', '{', '}', '[', ']'), 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. Every closing bracket is preceded by a matching opening bracket. 3. Brackets are closed in the correct order (no interleaving).

Return true if the string is valid, false otherwise.

Examples

Example 1
Input: "()"
Output: true
Explanation: A single pair of parentheses is valid.
Example 2
Input: "([{}])"
Output: true
Explanation: All three types of brackets are properly nested and closed in the correct order.
Example 3
Input: "([)]"
Output: false
Explanation: The brackets are interleaved incorrectly: the square bracket closes before the parenthesis.

Constraints