-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.py
More file actions
28 lines (26 loc) · 776 Bytes
/
brackets.py
File metadata and controls
28 lines (26 loc) · 776 Bytes
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
def is_matched(expression):
"""
Finds if expression is valid in terms of opened/closed brackets
"""
brackets = []
oppening_bracket = ['(', '{', '[']
matching_bracket = {'(': ')', '{': '}', '[': ']'}
# {[()]}
for bracket in expression:
if bracket in oppening_bracket:
brackets.append(bracket)
else:
try:
last_bracket = brackets.pop()
if matching_bracket[last_bracket] != bracket:
return False
except IndexError:
return False
return len(brackets) == 0
t = int(input().strip())
for a0 in range(t):
expression = input().strip()
if is_matched(expression) == True:
print("YES")
else:
print("NO")