forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanced-brackets.py
41 lines (36 loc) · 963 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
40
41
#!/bin/python3
import sys
def isBalanced(s):
stack = []
for letter in s:
if letter == '{':
stack.append(1)
elif letter == '[':
stack.append(2)
elif letter == '(':
stack.append(3)
elif letter == '}':
if len(stack) == 0:
return False
if stack.pop() != 1:
return False
elif letter == ']':
if len(stack) == 0:
return False
if stack.pop() != 2:
return False
elif letter == ')':
if len(stack) == 0:
return False
if stack.pop() != 3:
return False
return len(stack) == 0
if __name__ == "__main__":
t = int(input().strip())
for a0 in range(t):
s = input().strip()
result = isBalanced(s)
if result is True:
print('YES')
else:
print('NO')