Depth If Balanced
s is made of the brackets (), [] and {} only. It is balanced when every closing bracket closes the most recently opened bracket that is still open, the two are of the same kind, and nothing stays open at the end.
If s is balanced, return its nesting depth: the largest number of brackets that are open at the same moment. If s is not balanced, return -1. The empty string is balanced and has depth 0.
Example 1
Everything matches. Inside the outer ( one more bracket opens, so two are open at once.
Example 2
) arrives while [ is the most recently opened bracket, so the string is not balanced.
Example 3
Nothing is ever open, and an empty string counts as balanced.
Constraints
0 <= len(s) <= 60scontains only the characters()[]{}
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.