-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrackets_checker.py
47 lines (41 loc) · 1.46 KB
/
Brackets_checker.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
'''
Created on Jun 13, 2018
@author: rajat.arora07
'''
def chk_parenthesis(bracket_string):
str1=''
count=0
for ele in bracket_string:
# For every opening bracket,count is increased and str is updated.
if(ele=='(' or ele=='[' or ele=='{'):
count+=1
str1+=ele
#prvs=str1[len(str1)-1]
# For every closing bracket,it is checked with last charecter of str1. #If it is the same charecter,count is decreased.
elif(ele==')'):
if(len(str1)==0 or str1[len(str1)-1]!='('):
return False
str1=str1[:len(str1)-1]
count-=1
elif(ele==']'):
if(len(str1)==0 or str1[len(str1)-1]!='['):
return False
count-=1
str1=str1[:len(str1)-1]
elif(ele=='}'):
if(len(str1)==0 or str1[len(str1)-1]!='{'):
return False
str1=str1[:len(str1)-1]
count-=1
#If at any point of time, count is less than zero i.e. more brackets are closed then that were opened then return False.
if(count==-1):
return False
#print str1,count
#If on completely traversing the input,if there is any bracket open the return False.
if(count!=0):
return False
return 'balanced'
t=int(input())
for i in range(t):
str1=input()
print(chk_parenthesis(str1))