EasyStack & Queue
Valid Parentheses
Check if parentheses are valid
Solution Approach
Use stack to match opening and closing brackets. Check each closing matches top of stack.
Implementation
def isValid(s):
stack = []
pairs = {"(": ")", "[": "]", "{": "}"}
for char in s:
if char in pairs:
stack.append(char)
else:
if not stack or pairs[stack.pop()] != char:
return False
return len(stack) == 0Complexity Analysis
Time Complexity
O(n)Space Complexity
O(n)Key Learning Points
LIFO structureMatch pairs efficientlyUseful for bracket problems
Related Problems to Practice
Valid ParenthesesMin StackDaily Temperatures
Complexity
Time:O(n)
Space:O(n)
Asked at
GoogleAmazonFacebook