Skip to content

Instantly share code, notes, and snippets.

@abhinavjonnada82
Last active September 27, 2019 06:27
Show Gist options
  • Save abhinavjonnada82/bfcbea8eb119154b985fd73e0325e207 to your computer and use it in GitHub Desktop.
Save abhinavjonnada82/bfcbea8eb119154b985fd73e0325e207 to your computer and use it in GitHub Desktop.
# 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