-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets2.py
More file actions
60 lines (46 loc) · 1.4 KB
/
brackets2.py
File metadata and controls
60 lines (46 loc) · 1.4 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 21:13:50 2017
@author: suewoonryu
solutions for : https://algospot.com/judge/problem/read/BRACKETS2
"""
class Stack(object) :
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __len__(self):
return len(self.items)
def __str__(self):
return ''.join(self.items)
class MatchingBrackets(object):
def __init__(self):
self.openbrackets = "({["
self.closebrackets = ")}]"
self.stack = Stack()
def pairOf(self, c):
for i in range(3):
if c == self.closebrackets[i]:
return self.openbrackets[i]
def isMatched(self, inputList):
for c in inputList :
if c in self.openbrackets:
self.stack.push(c)
else :
if len(self.stack)!=0 and self.pairOf(c) == self.stack.pop():
continue
else :
return False
if len(self.stack) == 0 :
return True
testcases = int(input())
for i in range(testcases):
inputList = input()
matchingBrackets = MatchingBrackets()
if matchingBrackets.isMatched(inputList) :
print('YES')
else :
print('NO')