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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<cstdio>
#include<unordered_map>

using namespace std;

const int P1 = 18043;
const int P2 = 30637;

char input[300300];
int a;
int b;
int c;

long long res;
int single;
char last_char;

struct MyPair
{
	int x, y;
	
	MyPair(int x, int y) { this->x = x; this->y = y; }

	bool operator==(const MyPair& oth) const { return x == oth.x && y == oth.y; }
};

struct PairHash
{
	size_t operator()(const MyPair& mp) const { return mp.x * P1 + mp.y * P2; }
};

unordered_map<MyPair, int, PairHash> abc;
unordered_map<MyPair, int, PairHash> ab;
unordered_map<MyPair, int, PairHash> ac;
unordered_map<MyPair, int, PairHash> bc;


int main()
{
	last_char = 'z';

	ab[MyPair(0, 0)] = 1;
	bc[MyPair(0, 0)] = 1;
	ac[MyPair(0, 0)] = 1;

	abc[MyPair(0, 0)] = 1;

	single = 0;
	res = 0LL;

	scanf("%s", input);

	for (int i = 0; input[i]; ++i)
	{
		a += input[i] == 'a';
		b += input[i] == 'b';
		c += input[i] == 'c';

		if (input[i] == last_char) single++;
		else
		{
			res += 1LL * single * (single + 1) / 2;
			last_char = input[i];
			single = 1LL;
		}

		res += 1LL * ab[MyPair(a - b, c)];
		res += 1LL * ac[MyPair(a - c, b)];
		res += 1LL * bc[MyPair(b - c, a)];
		
		ab[MyPair(a - b, c)]++;
		ac[MyPair(a - c, b)]++;
		bc[MyPair(b - c, a)]++;

		res += 1LL * abc[MyPair(a - b, a - c)];

		abc[MyPair(a - b, a - c)]++;
	}

	res += 1LL * single * (single + 1) / 2;

	printf("%lld\n", res);

	return 0;
}