20. Valid Parentheses
solution
class Solution:
def isValid(self, s: str) -> bool:
anchor = {
")": "(",
"]": "[",
"}": "{"
}
stack = []
for sub in s:
if sub in ['(', '[', '{']:
stack.append(sub)
else:
if not stack or anchor[sub] != stack.pop(-1):
return False
else:
continue
if not stack:
return True
else:
return Falsefollow up-括号类
Last updated