Created
March 20, 2023 10:56
-
-
Save Abdur-rahmaanJ/4a2e0e771724c8be6759c793dd2657d5 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
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