forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
30 lines (30 loc) · 863 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def checkValidString(self, s: str) -> bool:
n = len(s)
left, asterisk = 0, 0
for i in range(n):
if s[i] == "(":
left += 1
elif s[i] == ")":
if left > 0:
left -= 1
elif asterisk > 0:
asterisk -= 1
else:
return False
else:
asterisk += 1
right, asterisk = 0, 0
for i in range(n - 1, -1, -1):
if s[i] == ")":
right += 1
elif s[i] == "(":
if right > 0:
right -= 1
elif asterisk > 0:
asterisk -= 1
else:
return False
else:
asterisk += 1
return True