def validbrackets(s):
openers = "({[<"
closers = ")}]>"
counts = [0,0,0,0]
oldopeners = []
for i in range(len(s)):
for j in range(len(openers)):
if s[i] == openers[j]:
counts[j] += 1
oldopeners.append(j)
elif s[i] == closers[j]:
if len(oldopeners) == 0 or j != oldopeners.pop():
return 0
counts[j] -= 1
for j in counts:
if j < 0:
return 0
for cnt in counts:
if cnt != 0:
return 0
return 1
print validbrackets("<{[()]([[[]]])}>")
print validbrackets("<{[(]([[[]]])}>")
print validbrackets("<{[(]([[[]]])}>)")
print validbrackets("<hello (world)[]>")