readable python solution

  • 0
    def isValid(self, s):
            :type s: str
            :rtype: bool
            open_ = {"{": "}", "(":")", "[": "]"}
            stack = []
            for ch in s:
                if ch not in open_:
                    if not stack:
                        # there isn't a character to close this one
                        return False
                    opener = stack.pop()
                    if ch != open_[opener]:
                        # the closing character doesn't match
                        return False
            # make sure all opening characters were used 
            return len(stack) == 0

Log in to reply

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