Create a stack that houses the opening brackets, if you see a closing bracket then the top of the stack SHOULD have that corresponding opening bracket.
classSolution:defisValid(self, s:str)->bool: mapping ={"}":"{",")":"(","]":"["} stack = collections.deque()for i in s:# if closing bracketif i in mapping:# if the stacks most recent opening bracket matches this closing bracketif stack and stack[-1]== mapping[i]: stack.pop()else:returnFalseelse: stack.append(i)# not a closing bracket# stack only consists of opening brackets, so it should be emptyreturnnot stack