Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: open brackets are closed by the same type, and open brackets are closed in the correct order.
Examples
Example 1 Input:s = "()"
Output:true
Example 2 Input:s = "()[]{}"
Output:true
Example 3 Input:s = "(]"
Output:false
Constraints
▪1 <= s.length <= 10^4
▪s consists of parentheses only.
Hints
Hint 1Show
Use a stack data structure.
Hint 2Show
Push opening brackets onto the stack.
Hint 3Show
When you see a closing bracket, check if it matches the top of the stack.
Starter Code
Solution.py
1def is_valid(s: str) -> bool:
2 # Your code here
3 pass
Solution
Solve the problem first before reviewing the solution!