Skip to content

Instantly share code, notes, and snippets.

@neoneo40
Last active August 29, 2015 14:11
Show Gist options
  • Save neoneo40/2a8af127f76d1401a06d to your computer and use it in GitHub Desktop.
Save neoneo40/2a8af127f76d1401a06d to your computer and use it in GitHub Desktop.
class Stack:
def __init__(self):
self.storage = []
def is_empty(self):
return len(self.storage) == 0
def push(self, value):
self.storage.append(value)
def pop(self):
return self.storage.pop()
def top(self):
return self.storage[-1]
def well_matched(string_):
opening = ['(', '{', '[']
closing = [')', '}', ']']
open_stack = Stack()
for ch in string_:
if ch in opening:
open_stack.push(ch)
else:
if open_stack.is_empty():
return 'NO'
top = open_stack.top()
if opening.index(top) != closing.index(ch):
return 'NO'
open_stack.pop()
if open_stack.is_empty():
return 'YES'
return 'NO'
if __name__ == '__main__':
n = int(raw_input())
for i in range(n):
string_ = raw_input()
print well_matched(string_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment