Using ascii code to verify brackets


  • 0
    J
    class Solution(object):
        def isValid(self, s):
            stack=[]
            for x in s:
                if x=="(" or x=="{" or x=="[":
                    stack.insert(0,x)
                else:
                    if len(stack)==0:
                        return False
                    if abs(ord(x)-ord(stack.pop(0)))>2:
                        return False
            return len(stack)==0
    

Log in to reply
 

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