20. Valid Parentheses
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for c in s:
if c in ['(', '[', '{']:
stack.append(c)
else:
if len(stack) == 0:
return False
top = stack.pop()
if c == ')':
if top != '(':
return False
elif c == ']':
if top != '[':
return False
elif c == '}':
if top != '{':
return False
return len(stack) == 0Last updated