Share simple python with try except method


  • 6
    A
      class Solution:
        # @return a boolean
        def isValid(self, s):
            d = {')':'(', ']':'[', '}':'{',}
            stack = []
            for x in s:
                if x=='(' or x=='[' or x=='{':
                    stack.append(x)
                else:
                    try:
                        if d[x] != stack.pop():
                            return False
                    except :
                        return False
            return not stack

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.