main leetcode-practice/valid_parentheses.py

9 lines252 B

def is_valid(s):
pairs = {")": "(", "]": "[", "}": "{"}
stack = []
for ch in s:
if ch in pairs.values():
stack.append(ch)
elif not stack or stack.pop() != pairs[ch]:
return False
return not stack