Problem statement

https://binarysearch.com/problems/Parentheses-Grouping/

Solution

Just iterate through string and check balance.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        bal, ans = 0, []
        for x in s:
            if bal == 0:
                ans += [[x]]
            else:
                ans[-1] += [x]
            bal += 1 if x == "(" else -1
        
        return ["".join(x) for x in ans]