2
def checkValidString(self, s: str) -> bool:7
) # indexes of all unmatched left right parens and all '*'9
for i, c in enumerate(s):10
if c == "(": # we just append left paren's index12
elif c == ")": # we check if we can find a match of left paren13
if left and left[-1] < i:17
else: #'*' case we just add the postion19
if not left and not right:22
return False # no star to save the string, return False23
l, r = 0, len(star) - 124
# O(n) since star will be length less than n25
# Note: left, right,and star are always kept in ascending order! And for any i in left, j in right, i > j, or they would have been matched in the previous for loop.30
): # we keep using right most star to match with right most '('34
return False # even the right most '*' can not match a '(', we can not fix the string.36
if right[0] > star[l]:41
if not left and not right:42
return True # if after some fix, all matched, we return True