Valid Parentheses
Learn how to solve the 'Valid Parentheses' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.
Write a function isValid(s: str) -> bool.
- •1 <= len(s) <= 10^4
- •s consists of parentheses only: '()[]{}'
Examples
s = "()"
True
A single pair of matching parentheses is valid.
s = "()[]{}"True
Three pairs of matching brackets, each closed in order.
s = "(]"
False
Opening '(' is closed by ']' which is the wrong type.
Need a Hint?
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.