Valid Parentheses
Given a string 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.
Note that an empty string is also considered valid.
Function Signature:
def is_valid_parentheses(s: str) -> bool:
"""
Determine if the input string contains valid parentheses.
Parameters:
- s: Input string.
Returns:
- bool: True if the parentheses are valid, False otherwise.
"""
# Your code here
Example:
# Input: s = "()"
# Output: True
# Input: s = "()[]{}"
# Output: True
# Input: s = "(]"
# Output: False
# Input: s = "([)]"
# Output: False
# Input: s = "{[]}"
# Output: True
Note:
- An empty string is considered valid.
- The input string may contain characters other than parentheses.
Instructions:
- Write the
is_valid_parentheses
function to solve the problem. - Implement your solution in Python.
- Provide clear comments in your code.
- Discuss the time and space complexity of your solution.
As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊