20. Valid Parentheses
(
[
{
}
]
)
stack
empty
reading…
read '('read
1class Solution:2 def isValid(self, s: str) -> bool:3 stack = []4 pairs = {')': '(', ']': '[', '}': '{'}5 for char in s:6 if char in pairs:7 if stack and stack[-1] == pairs[char]:8 stack.pop()9 else:10 return False11 else:12 stack.append(char)13 return not stack