Skip to content

Instantly share code, notes, and snippets.

@omokehinde
Last active June 2, 2022 00:11
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 omokehinde/3532e0f04ce05f6b419d6a50156cb91e to your computer and use it in GitHub Desktop.
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
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