Last active
June 2, 2022 00:11
-
-
Save omokehinde/3532e0f04ce05f6b419d6a50156cb91e to your computer and use it in GitHub Desktop.
Write a function that returns True or False if the string contains opening and closing brackets in the right order
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 isValid(s): | |
bracket_dic = { | |
"[":"]", | |
"{":"}", | |
"(":")" | |
} | |
open_bracket = [] | |
for i in s: | |
if i not in bracket_dic and len(open_bracket) == 0: return False | |
if i in bracket_dic: | |
open_bracket.append(i) | |
else: | |
if bracket_dic[open_bracket[len(open_bracket)-1]] == i: | |
open_bracket.pop() | |
else: return False | |
return len(open_bracket) == 0 | |
print(isValid("([{})]")) | |
print(isValid("({]})")) | |
print(isValid("([{}])")) | |
print(isValid("({[]})")) | |
print(isValid("()[]{}")) | |
print(isValid("}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment