Description
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input 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.
Examples
s = "()"trueUsing a stack, Pushing '(' then pop it when ')' is encountered. The stack is empty at the end, confirming proper matching.
s = "()[]{}"trueEach opening bracket is immediately followed by its matching closing bracket: () matches, [] matches, {} matches. The stack empties correctly.
s = "(]"falseAfter pushing '(' onto the stack, encountering ']' which requires '[' on top. Since '(' is on top instead, the brackets are mismatched.
s = "([)]"falseBrackets must be closed in correct order. The ')' closes before ']'.
Constraints
- •
1 ≤ s.length ≤ 10⁴ - •
s consists of parentheses only '()[]{}'.