Valid Parentheses - Problem
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.
Input & Output
Example 1 — Valid Nested
$
Input:
s = "()"
›
Output:
true
💡 Note:
Simple pair: opening parenthesis followed by closing parenthesis
Example 2 — Mixed Valid
$
Input:
s = "()[{}]"
›
Output:
true
💡 Note:
All brackets properly matched: () pair, then [{}] where {} is nested inside []
Example 3 — Invalid Order
$
Input:
s = "(]"
›
Output:
false
💡 Note:
Mismatched types: opening parenthesis ( cannot be closed by square bracket ]
Constraints
- 1 ≤ s.length ≤ 104
- s consists of parentheses only '()[]{}'.
Visualization
Tap to expand
Understanding the Visualization
1
Input
String containing only bracket characters
2
Process
Check if brackets are properly paired and nested
3
Output
Return true if valid, false otherwise
Key Takeaway
🎯 Key Insight: Use a stack to track opening brackets and match them with closing brackets in LIFO order
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code