def check_balance(brackets):
check = 0
for bracket in brackets:
if bracket == ‘[’:
check += 1
elif bracket == ']':
check -= 1
if check < 0:
break
return check == 0
print(check_balance(bracket_string))
I found this code has a problem when string is bracket_string = ‘[]]][[’. it should be balance but because of this line of code:
if check < 0:
break
Course: https://www.educative.io/courses/python-fundamentals-for-programmers
Lesson: https://www.educative.io/courses/python-fundamentals-for-programmers/qVEJXGPPzvy