-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid parethesis.py
37 lines (27 loc) · 948 Bytes
/
valid parethesis.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
class Solution:
def isValid(self, s: str) -> bool:
stack=[]
dic={')':'(','}':'{',']':'['}
opening="({["
closing=")}]"
output=False
if len(s)%2!=0:
output=False
for i in s:
if len(stack)==0 and i in opening:
stack.append(i)
continue
if len(stack)!=0 and i in opening:
stack.append(i)
if len(stack)==0 and i in closing:
output=False
break
if len(stack)!=0 and i in closing :
if stack.pop()!=dic[i] :
output=False
break
else:
output=True
if len(stack)!=0:
output=False
return output