Straightforward python solution


  • 0
    T
    class Solution:
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            
            stack = []
            
            matching = { "(" : ")", "[" : "]", "{" : "}"}
            
            for c in s:
                if c in matching.keys():
                    stack.append(c)
                elif c in matching.values():
                    if len(stack) == 0:
                        return False
                    if matching[stack[-1]] != c:
                        return False
                    else:
                        stack.pop()
                        
            return len(stack) == 0
    

Log in to reply
 

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