Skip to content

Commit 0a9d46a

Browse files
authored
Balanced_Parenthesis
Initial File
1 parent 5773510 commit 0a9d46a

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Balanced_Parenthesis

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def arePairs(open,close):
2+
if open=='[' and close==']':
3+
return True
4+
if open=='{' and close=='}':
5+
return True
6+
if open=='(' and close==')':
7+
return True
8+
return False
9+
10+
def Balanced(A):
11+
stack=[]
12+
for i in range(len(A)):
13+
if A[i]=='[' or A[i]=='{' or A[i]=='(':
14+
stack.append(A[i])
15+
elif A[i] == ']' or A[i] == '}' or A[i] == ')':
16+
if arePairs(stack[-1],A[i] or len(stack)!=0):
17+
stack.pop()
18+
else:
19+
return False
20+
if len(stack)==0:
21+
return True
22+
else:
23+
return False
24+
25+
26+
A="[{())(}]"
27+
print(Balanced(A))
28+
29+
30+
31+

0 commit comments

Comments
 (0)