Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
Example 1:
Input: s = "()" Output: true
Example 2:
Input: s = "()[]{}" Output: true
Example 3:
Input: s = "(]" Output: false
Example 4:
Input: s = "([)]" Output: false
Example 5:
Input: s = "{[]}" Output: true
Constraints:
1 <= s.length <= 104
s
consists of parentheses only '()[]{}'
.The core challenge of this problem is to ensure that every opening bracket has a corresponding closing bracket and that they are correctly nested. This problem is significant in various applications such as parsing expressions in compilers, validating mathematical expressions, and ensuring the correctness of nested structures in programming languages.
Potential pitfalls include mismatched brackets, incorrect nesting, and unbalanced brackets.
To solve this problem, we can use a stack data structure. The stack helps us keep track of the opening brackets encountered, and we can match them with the closing brackets as we traverse the string.
A naive solution might involve checking each pair of brackets, but this would be inefficient and complex to implement. This approach is not optimal due to its high time complexity.
The optimized solution involves using a stack to keep track of the opening brackets. As we traverse the string, we push each opening bracket onto the stack. When we encounter a closing bracket, we check if it matches the top of the stack. If it does, we pop the stack; otherwise, the string is invalid.
def isValid(s: str) -> bool:
# Dictionary to hold the mappings of closing to opening brackets
bracket_map = {')': '(', '}': '{', ']': '['}
# Stack to keep track of opening brackets
stack = []
# Traverse each character in the string
for char in s:
# If the character is a closing bracket
if char in bracket_map:
# Pop the top element from the stack if it's not empty, otherwise assign a dummy value
top_element = stack.pop() if stack else '#'
# Check if the popped element matches the corresponding opening bracket
if bracket_map[char] != top_element:
return False
else:
# If it's an opening bracket, push it onto the stack
stack.append(char)
# If the stack is empty, all brackets were matched correctly
return not stack
The time complexity of this solution is O(n) because we traverse the string once. The space complexity is also O(n) due to the stack used to store opening brackets.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
assert isValid("()") == True
assert isValid("()[]{}") == True
assert isValid("(]") == False
assert isValid("([)]") == False
assert isValid("{[]}") == True
assert isValid("") == True
assert isValid("(((((((())))))))") == True
assert isValid("(((((((()))))))") == False
When approaching such problems, consider using data structures like stacks that can help manage nested structures. Practice similar problems to develop a deeper understanding of stack operations and their applications.
Understanding and solving the "Valid Parentheses" problem is crucial for mastering stack-based algorithms. This problem is a common interview question and has practical applications in parsing and validating nested structures.