my short python solution


  • 0
    G
    class Solution(object):
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            if len(s) % 2 != 0:
                return False
            tmp = []
            for c in s:
                if c == '(' or c == '[' or c == '{':
                    tmp.append(c)
                else:
                    if len(tmp) == 0 or abs(ord(c) - ord(tmp.pop())) > 2:
                        return False
            return len(tmp) == 0
    

Log in to reply
 

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