Straightforward well named python soltuion


  • 0
    S

    I've first written it down on a piece of paper and realized that we have to watch for two major issues:

    1. We keep track of the opened type of brackets and if the next found closing bracket doesn't match the last found opening Bracket, we invalidate our result.
    2. If we find a closing bracket, but no opening bracket was found, we invalidate our result.
    class Solution(object):
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            openBrackets = []
            closeMapping = {")":"(", "]":"[", "}":"{"}
            for i in s:
                if i in closeMapping.values():
                    openBrackets.append(i)
                elif i in closeMapping.keys():
                    if openBrackets and openBrackets.pop() == closeMapping[i]:
                        continue
                    else:
                        return False
            return not bool(openBrackets)
    

Log in to reply
 

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