Skip to content

Instantly share code, notes, and snippets.

@shoark7
Last active September 17, 2019 01:51
Show Gist options
  • Save shoark7/3cd95a278d1a6c59d94bf0ab016c15b2 to your computer and use it in GitHub Desktop.
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.
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