-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbalanced_brackets.py
39 lines (37 loc) · 1016 Bytes
/
balanced_brackets.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
# Program to find whether given input string has balanced brackets or not
def isBalanced(s):
a=[]
for i in range(len(s)):
if s[i]=='{' or s[i]=='[' or s[i]=='(':
a.append(s[i])
if s[i]=='}':
if len(a)==0:
return "NO"
else:
if a[-1]=='{':
a.pop()
else:
break
if s[i]==']':
if len(a)==0:
return "NO"
else:
if a[-1]=='[':
a.pop()
else:
break
if s[i]==')':
if len(a)==0:
return "NO"
else:
if a[-1]=='(':
a.pop()
else:
break
if len(a)==0:
return "YES"
else:
return "NO"
inp = input('Enter your query string: ')
#sample input: {)[](}]}]}))}(())(
print(isBalanced(inp))