-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidParenthesis.py
62 lines (48 loc) · 1.63 KB
/
validParenthesis.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution:
def checkValidString(self, s: str) -> bool:
stack=[]
for i in range(0,len(s)):
if s[i]=='(' or s[i]=='*':
stack.append(s[i])
else:
if len(stack)==0 :
return False
stars=0
flag=0
while len(stack)!=0:
ele=stack[-1]
stack.pop()
if ele=='*':
stars+=1
if ele=='(':
flag=1
break
if len(stack)==0 and flag!=1:
if stars!=0:
stars-=1
while stars!=0:
stack.append('*')
stars-=1
print(stack)
print("I am out")
stars=0
openb=0
if(list(set(stack))==['*']):
return True
while True:
if(len(stack)==0) or (list(set(stack))==['*']):
return True
stars=0
while len(stack)!=0:
ele=stack[-1]
stack.pop()
if ele=='(':
break
stars+=1
if stars==0:
return False
stars-=1
while stars!=0:
stars-=1
stack.append('*')
print(stack)