1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <iostream>

std::string s;
long long res = 0;
int A[300001];
int B[300001];
int C[300001];
std::pair<int,int> detect[300001];

void count()
{
	std::sort(detect, detect + s.size() + 1);
	long long tmp = 1;
	for( int i = 1; i <= s.size(); i++)
	{
		if( detect[i] == detect[i-1] )
			tmp++;
		else
		{
			res += tmp*(tmp-1)/2;
			tmp = 1;
		}
	}
	res += tmp*(tmp-1)/2;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin >> s;
	//count 3-letters
	for( int i = 0; i < s.size(); i++ )
	{
		A[i+1] = A[i];
		B[i+1] = B[i];
		C[i+1] = C[i];
		if( s[i] == 'a' ) A[i+1]++;
		else if( s[i] == 'b' ) B[i+1]++;
		else if( s[i] == 'c' ) C[i+1]++;
	}
	for( int i = 0; i < s.size(); i++ )	detect[i+1] = std::make_pair(A[i+1]-B[i+1],A[i+1]-C[i+1]);
	count();
	//count 2-letters
	detect[0] = std::make_pair(0,0);
	for( int i = 0; i < s.size(); i++ )	detect[i+1] = std::make_pair(A[i+1],B[i+1]-C[i+1]);
	count();
	detect[0] = std::make_pair(0,0);
	for( int i = 0; i < s.size(); i++ )	detect[i+1] = std::make_pair(B[i+1],A[i+1]-C[i+1]);
	count();
	detect[0] = std::make_pair(0,0);
	for( int i = 0; i < s.size(); i++ )	detect[i+1] = std::make_pair(C[i+1],A[i+1]-B[i+1]);
	count();
	//count one letters
	long long t = 1;
	for( int i = 1; i < s.size(); i++)
	{
		if( s[i] == s[i-1] )
			t++;
		else
		{
			res += t*(t+1)/2;
			t = 1;
		}
	}
	res += t*(t+1)/2;
	std::cout << res << '\n';
	return 0;
}