Search⌘ K
AI Features

Solution Review: Balanced Brackets

Explore the solution to the balanced brackets problem by understanding how to use loops and conditional logic to track bracket pairs. This lesson helps you grasp fundamental coding techniques for checking balance in sequences and provides a detailed, step-by-step code explanation.

We'll cover the following...

Solution

...
Python 3.10.4
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
bracket_string = '[[[[]]'
print(check_balance(bracket_string))
...