Skip to content

Instantly share code, notes, and snippets.

@delihiros
Created February 19, 2020 09:48
Show Gist options
  • Save delihiros/413f3dd72c849190e893902b05aed077 to your computer and use it in GitHub Desktop.
Save delihiros/413f3dd72c849190e893902b05aed077 to your computer and use it in GitHub Desktop.
def is_valid(s):
stack = []
for c in s:
if c == '(' or c == '[':
stack.append(c)
elif c == ')':
if not stack or stack[-1] != '(':
return False
stack.pop()
elif c == ']':
if not stack or stack[-1] != '[':
return False
stack.pop()
return not stack
t1 = '(()(()))' # => True
t2 = '(()(())))' # => False
t3 = '(([]))' # => True
t4 = '(([)])' # => False
print(is_valid(t1))
print(is_valid(t2))
print(is_valid(t3))
print(is_valid(t4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment