-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidParatheses.py
More file actions
49 lines (44 loc) · 1.3 KB
/
Copy pathvalidParatheses.py
File metadata and controls
49 lines (44 loc) · 1.3 KB
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
""" paran = {"(":0,")":0,"{":0,"}":0,"[":0,"]":0}
x = input()
def valid(x):
for i in range(len(x)):
paran[x[i]] = paran[x[i]] +1
if ((paran["("]+paran[")"])%2 ==0) and ((paran["{"]+paran["}"])%2 ==0) and ((paran["["]+paran["]"])%2 ==0):
return "true"
else:
return "false"
print(valid(x)) """
""" def isValid(s):
stack = []
dict = {"]":"[", "}":"{", ")":"("}
for char in s:
if char in dict.values():
stack.append(char)
#print(stack)
elif char in dict.keys():
if stack == [] or dict[char] != stack.pop():
return False
else:
return False
return stack == []
print(isValid(")(")) """
def valid(s):
stack = []
ans =[]
for i in range(len(s)):
if (s[i]=="(") or (s[i]=="[") or (s[i]=="{"):
stack.append(s[i])
print(stack)
else:
if(len(stack)==0):
return False
else:
temp = stack.pop()
print(temp)
if (s[i]==")" and temp == "(") or (s[i]=="]" and temp == "[") or (s[i]=="}" and temp == "{"):
ans.append(True)
else:
return False
if len(stack) == 0:
return True
print(valid("(()]"))