Problem statement

https://binarysearch.com/problems/Anagram-Partitioning/

Solution

Go element by element and keep in counter difference of frequencies for each letter. Also keep total is number of values which are not equal to 0.

Complexity

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

Code

class Solution:
    def solve(self, a, b):
        n = len(a)
        cnt, total, ans = Counter(), 0, []
        
        for i in range(n):
            if total == 0:
                ans += [i]
            cnt[a[i]] += 1
            if cnt[a[i]] == 1: total += 1
            cnt[b[i]] -= 1
            if cnt[b[i]] == 0: total -= 1

        return ans if Counter(a) == Counter(b) else []