Python Stack One Pass


  • 0
    class Solution(object):
        def isValid(self, s):
            stack = []
            dic = {')': '(', ']': '[', '}': '{'}
            for c in s:
                if c == '(' or c == '[' or c == '{':
                    stack.append(c)
                elif stack and dic[c] == stack[-1]:
                    stack.pop()
                else:
                    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.