Count Residue Prefixes
Grow each prefix one letter at a time, track how many distinct letters it has, and count the prefixes where that count equals the length modulo 3.
Grow each prefix one letter at a time, track how many distinct letters it has, and count the prefixes where that count equals the length modulo 3.