a neat python solution


  • 0
    O
    class Solution(object):
        def isValid(self, s):
            all_left = ['(','{','[']
            all_right = [')','}',']']
            stack = []
            """
            :type s: str
            :rtype: bool
            """
            for c in s:
                if c in all_left:
                    stack.append(c)
                if c in all_right:
                    if len(stack) == 0:
                        return False
                    left = stack.pop()
                    if all_left.index(left) != all_right.index(c):
                        return False
            return len(stack) ==0
    

Log in to reply
 

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