Solution by Python


  • 0
    P

    class Solution(object):
    def isMatch(self,ls,r):
    return (ls=="(" and r==")") or (ls=="[" and r=="]") or (ls=="{" and r=="}")
    def isValid(self, s):
    """
    :type s: str
    :rtype: bool
    """
    if len(s)==0:
    return True
    ls = []
    for item in s:
    len_ls = len(ls)
    if len_ls ==0 or not self.isMatch(ls[len_ls - 1], item):
    ls.append(item)
    else:
    ls.pop()
    return len(ls)==0


Log in to reply
 

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