Python solution (beats 100%)


  • 0
    H
    class Solution(object):
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            if len(s)%2 is 1:
                return False
            dic={
                "(":")",
                "[":"]",
                "{":"}"
                }
            stack=[]
            for i in s:
                if i == '(' or i == '{' or i == '[':
                    stack.append(i)
                else:
                    if len(stack) != 0 and dic[stack.pop()] != i:
                        return False
            return len(stack) == 0
    

  • 0
    P

    Ok solution but not very pythonic.


Log in to reply
 

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