165. Valid Parentheses (String Version)

EasyStringString

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

A string is valid if:
1. Open brackets are closed by the same type of bracket.
2. Open brackets are closed in the correct order.
3. Every close bracket has a corresponding open bracket.

Input: A string s containing only '(', ')', '{', '}', '[', ']'.

Output: Return 'true' if the string is valid, otherwise 'false'.

Examples

Example 1
Input: ()
Output: true
Explanation: Single matching pair → valid.
Example 2
Input: ()[]{}
Output: true
Explanation: Three separate pairs, all matching → valid.
Example 3
Input: (]
Output: false
Explanation: '(' cannot be closed by ']' → invalid.

Constraints

Asked by

BloombergHCLTechIBMDeloitteAmazonMeta
Solve this problem in the editor →