My python solution


  • 7
    H
     class Solution:
            # @param {string} s
            # @return {boolean}
            def isValid(self, s):
                stack=[]
                for i in s:
                    if i in ['(','[','{']:
                        stack.append(i)
                    else:
                        if not stack or {')':'(',']':'[','}':'{'}[i]!=stack[-1]:
                            return False
                        stack.pop()
                return not stack

  • 0
    X

    This is brilliant. It should be the top answer to this question. Better than the .replace answer which actually has O(n^2) complexity


  • 0
    H

    @hliu94 the input "[" can not pass the test


Log in to reply
 

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