Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 4, 2019 14:47
Show Gist options
  • Save imedadel/8ac3ac455c01a91a34b6a5b5d53cd609 to your computer and use it in GitHub Desktop.
Save imedadel/8ac3ac455c01a91a34b6a5b5d53cd609 to your computer and use it in GitHub Desktop.
def isBalanced(s):
stack = [s[0]]
for c in s[1:]:
if len(stack) == 0:
stack.append(c)
elif (c == ")" and stack[-1] == "(") or (c == "}" and stack[-1] == "{") or (c == "]" and stack[-1] == "["):
stack.pop()
else:
stack.append(c)
return "YES" if len(stack) == 0 else "NO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment