Solution in Python


  • 0
    class Solution(object):
        def isValid(self, s):
            
            if len(s) % 2 != 0:
                return False
            #STEP 1.
            sign_map = {"(":")","[":"]","{":"}"}
            left = ["(","[","{"]
            right = [")","]","}"]
            l = []
            
            #STEP 2.
    
            flag = False
            if s[0] in right:
                return False
            
            for item in s:
       
                if item in left:
                    l.append(item)
                else:
                    if sign_map[l.pop()] == item:
                        flag = True
                    else:
                        return False
            if len(l) != 0:
                return False
            return flag
    

Log in to reply
 

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