from collections import deque
def isValid(s: str) -> bool:
q, d = deque(), {'(': ')', '[': ']', '{': '}'}
for c in s:
if c in ['(', '[', '{']:
q.append(c)
else:
if (not q) or d[q.pop()] != c:
return(False)
return(not q) # check that q is empty ie that there is no open parenthis left