Created
November 4, 2019 14:47
-
-
Save imedadel/8ac3ac455c01a91a34b6a5b5d53cd609 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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