Last active
September 17, 2019 01:51
-
-
Save shoark7/3cd95a278d1a6c59d94bf0ab016c15b2 to your computer and use it in GitHub Desktop.
Let's check if a given parenthesis string is in a valid form.
This file contains 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
def is_right_parenthesis(parens): | |
OPENERS = '([{' | |
CLOSERS = ')]}' | |
MAP = dict(zip(CLOSERS, OPENERS)) # { ')': '(', ']': '[', '}': '{' } 와 동일 | |
stack = [] | |
for p in parens: | |
if p in OPENERS: | |
stack.append(p) | |
else: | |
if not stack: | |
return False | |
opener = stack.pop() | |
if opener != MAP[p]: | |
return False | |
return not stack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment