-
Notifications
You must be signed in to change notification settings - Fork 0
/
678.py
43 lines (38 loc) · 1.17 KB
/
678.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
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution:
def checkValidString(self, s: str) -> bool:
if len(s)==0:
return True
list1=[]
for char in s:
if char=='(':
list1.append(char)
continue
if char=='*':
list1.append('*')
continue
if char==')':
if '(' in list1:
i=len(list1)-1
while(list1[i]!='('):
i=i-1
list1.pop(i)
elif '*' in list1:
i=len(list1)-1
while(list1[i]!='*'):
i=i-1
list1.pop(i)
else:
return False
continue
if len(list1)==0:
return True
list2=[]
for i in range(len(list1)-1,-1,-1):
if list1[i]=='*':
list2.append(list1[i])
if list1[i] == '(':
if '*' in list2:
list2.remove('*')
else:
return False
return True