Last active
September 27, 2019 06:27
-
-
Save abhinavjonnada82/bfcbea8eb119154b985fd73e0325e207 to your computer and use it in GitHub Desktop.
This file contains 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
# Balance Parantheses | |
def isValid(string): | |
balLis = [i for i in string ] | |
stack = [] | |
mapping = {")": "(", "}": "{", "]": "["} # Dictionary | |
for i in balLis: | |
if i == "(" or i == "[" or i == "{": | |
stack.append(i) | |
elif i ==")" or i == "]" or i == "}" and len(stack) > 0: | |
if stack[-1] == mapping[i]: | |
stack.pop() | |
else: | |
return (False) | |
else: | |
return (False) | |
if len(stack) == 0: | |
return(True) | |
else: | |
return (False) | |
s = "{" | |
print(isValid(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment