- We start the problem by creating a mapping:
translation = {
"(": ")",
"[": "]",
"{": "}"
}- Then we add every opening character to an array:
if char in translation:
stack.append(char)- Then we deal with errors on other occasions:
elif char in translation.values(): # If closing character
if not stack or translation[stack.pop()] != char:
return False
else: # If another character
return FalseIf the stack is empty or if the element that closes is different from the one that opens, we return false