Skip to content

Instantly share code, notes, and snippets.

@Abdur-rahmaanJ
Created March 20, 2023 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abdur-rahmaanJ/4a2e0e771724c8be6759c793dd2657d5 to your computer and use it in GitHub Desktop.
Save Abdur-rahmaanJ/4a2e0e771724c8be6759c793dd2657d5 to your computer and use it in GitHub Desktop.
def isbalanced(string):
stack = []
for char in string:
if char == '(':
stack.append(char)
elif char == ')':
if not stack: # stack empty trying to remove
return False
else:
stack.pop()
if stack: # end reached but stack not empty
return False
return True
print('((()))', isbalanced('((()))'))
print('()(())', isbalanced('()(())'))
print('((((', isbalanced('(((('))
print('))))', isbalanced('))))'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment