My solution In python


  • 0
    P
    class Solution:
        # @return a boolean
        def isValid(self, s):
            openBrac=set(['(','{','['])
            bracDict={')':'(','}':'{',']':'['}
            openStack=[]
            for letter in s:
                if letter in openBrac:
                    openStack.append(letter)
                elif letter in bracDict:
                    if len(openStack) <1:
                        return False
                    brac=openStack.pop()
                    if bracDict[letter] != brac:
                        return False
            if len(openStack)>0:
                return False
            return True
    

    Is there any better way to solve problem in python?


Log in to reply
 

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