Skip to content

Instantly share code, notes, and snippets.

@DavidPu
Last active February 23, 2017 06:03
Show Gist options
  • Save DavidPu/ad163a183896e969b6c929b4ed4642a5 to your computer and use it in GitHub Desktop.
Save DavidPu/ad163a183896e969b6c929b4ed4642a5 to your computer and use it in GitHub Desktop.
def check_string(s):
mstack = []
bFind = False
matches = {')': '(', '}': '{', ']': '['}
for c in s:
if c in matches.values():
bFind = True
mstack.append(c)
elif c in matches.keys():
if len(mstack) == 0 or mstack[-1] != matches[c]:
return False
mstack.pop()
else:
print("unexpected character")
return False
return bFind and len(mstack) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment