Simple Python solution with forward and reverse pass


  • 0
    Z
    class Solution(object):
        def checkValidString(self, s):
            """
            :type s: str
            :rtype: bool
            """
            def check(s, para):
                t, total = 0, 0
                for i, ch in enumerate(s):
                    if ch == para[0]: t += 1
                    elif ch == para[1]: t -= 1
                    else: total += 1
                    if t < 0 and total+t < 0: return False
                if t > 0 and t-total > 0: return False
                return True
            return check(s, ['(', ')']) and check(s[::-1], [')', '('])
    

Log in to reply
 

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