13 Line Python solution with string.find()


  • 0
    D
    class Solution(object):
        def isValid(self, s):
            Parentheses = []
            ParenthesesMatching = { "[" : "]", "{" : "}", "(" : ")" }
            for ch in s:
                if "[{(".find(ch) != -1:
                    Parentheses.append(ch)
                else:
                    if (not Parentheses) or (ch != ParenthesesMatching.get(Parentheses[-1], -1)):
                        return False
                    else:
                       Parentheses.pop()
            return False if Parentheses else True
    

    Use "[{(".find(ch) to wrap up nested if statement


Log in to reply
 

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