Problem statement

https://leetcode.com/problems/number-of-good-ways-to-split-a-string/

Solution

Use counters for the left and for the right parts.

Complexity

It is O(n) for time and O(26) for space.

Code

class Solution:
    def numSplits(self, s):
        c1, c2 = Counter(), Counter(s)
        l1, l2 = 0, len(c2)
        n, ans = len(s), 0
        for x in s:
            c1[x] += 1
            if c1[x] == 1: l1 += 1
            c2[x] -= 1
            if c2[x] == 0: l2 -= 1
            ans += (l1 == l2)
        return ans