LC678 - Valid Parenthesis String
Problem
Example
Solution
Greedy
def checkValidString(self, s: str) -> bool:
leftMin = 0
leftMax = 0
for char in s:
if char == '(':
leftMin += 1
leftMax += 1
elif char == ')':
leftMin -= 1
leftMax -= 1
# char is **
else:
leftMin -= 1
leftMax += 1
# Reset to 0 because wilds can be left unused
if leftMin < 0:
leftMin = 0
# more rights than left + wild
if leftMax < 0:
return False
# True unless left > right
if leftMin == 0:
return True
return False
Last updated