Last active
August 29, 2015 14:11
-
-
Save neoneo40/2a8af127f76d1401a06d to your computer and use it in GitHub Desktop.
Mismatched Brackets - https://algospot.com/judge/problem/read/BRACKETS2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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