Problem statement

https://binarysearch.com/problems/Characters-in-Each-Bracket-Depth/

Solution

Keep balance of brackets and use counter.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        d = Counter()
        bal, mx = 0, 0
        for x in s:
            if x == "(": bal += 1
            if x == ")": bal -= 1
            if x == "X": d[bal] += 1
            mx = max(mx, bal)
        return [d[i] for i in range(1, mx + 1)]