Problem statement

https://binarysearch.com/problems/Split-String-with-Same-Distinct-Counts/

Solution

Equal to Leetcode 1525. Number of Good Ways to Split a String.

Complexity

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

Code

class Solution:
    def solve(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