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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct occurence_t
{
	int a;
	int b;
	int c;	
	int temp;
};

struct abc_less
{
	bool operator() (occurence_t& o1, occurence_t& o2)
	{
		if (o1.a < o2.a) return true;
		if (o1.a == o2.a && o1.b < o2.b) return true;
		if (o1.a == o2.a && o1.b == o2.b && o1.c < o2.c) return true;
		return false;	
	}	
};

int numberOfBalancedWordsWithOneLetter(string& word, int n)
{
	int number = 0;
	int currentCount = 1;
	char currentChar = word[0];
	for (int i=1;i<n;++i)
	{
		if (word[i] == currentChar)
			++currentCount;
		else
		{
			number += currentCount * (currentCount + 1) / 2;
			currentCount = 1;
			currentChar = word[i];
		}
	}
	number += currentCount * (currentCount + 1) / 2;
	return number;
}

void printOccurences(occurence_t* occurences, int n)
{
	for (int i=0;i<=n;i++)
		cout << occurences[i].a << " ";
	cout << endl;
	for (int i=0;i<=n;i++)
		cout << occurences[i].b << " ";
	cout << endl;
	for (int i=0;i<=n;i++)
		cout << occurences[i].c << " ";
	cout << endl;
}

int numberOfBalancedWordsWithPrefixSums(occurence_t* occurences, int n)
{
	int number = 0;
	int aCur = occurences[0].a, bCur=occurences[0].b, cCur=occurences[0].c, currentCount = 1;
	for (int i=1;i<=n;++i)
	{
		if (occurences[i].a == aCur && occurences[i].b == bCur && occurences[i].c == cCur)
			++currentCount;
		else
		{
			number += currentCount * (currentCount - 1) / 2;
			currentCount = 1;
			aCur = occurences[i].a;
			bCur = occurences[i].b;
			cCur = occurences[i].c;
		}
	}
	number += currentCount * (currentCount - 1) / 2;
	return number;
}

int main()
{
	string word;
	cin >> word;
	int n = word.size();
	
	if (n == 0)
	{
		cout << 0 << endl;
		return 0;
	}
	
	long long balancedWords = 0;
	
	balancedWords += numberOfBalancedWordsWithOneLetter(word, n);
	//cout << balancedWords << endl;
	
	occurence_t *occurences = new occurence_t[n+1];
	
	// empty suffices
	occurences[0].a = 0;
	occurences[0].b = 0;
	occurences[0].c = 0;
	
	// set flag to 1 if a letter occurs at index i
	for (int i=0;i<n;++i)
	{
		if (word[i]=='a') occurences[i+1].a = 1;
		else occurences[i+1].a = 0; 
		
		if (word[i]=='b') occurences[i+1].b = 1;
		else occurences[i+1].b = 0; 
		
		if (word[i]=='c') occurences[i+1].c = 1;
		else occurences[i+1].c = 0; 
	}
	// scan arrays of occurences (partial sums)
	for (int i=2;i<=n;++i)
	{
		occurences[i].a += occurences[i-1].a;
		occurences[i].b += occurences[i-1].b;
		occurences[i].c += occurences[i-1].c;
	}
	
	//printOccurences(occurences, n);
	// now we have vectors of occurences, time to normalize for [c,c,c] -> a,b,c exist in word
	for (int i=0;i<=n;++i)
	{
		occurences[i].b -= occurences[i].a;
		occurences[i].c -= occurences[i].a;
		occurences[i].temp = occurences[i].a;
		occurences[i].a = 0;
	}
	
	sort(occurences, occurences + n + 1, abc_less());
	
	//printOccurences(occurences, n);
	
	balancedWords += numberOfBalancedWordsWithPrefixSums(occurences, n);
	//cout << balancedWords << endl;
	
	// we do the same for [0,c,c], [c,c,0] and [c,0,c]
	for (int i=0;i<=n;++i)
	{
		occurences[i].a = occurences[i].temp;
		occurences[i].b += occurences[i].a;
		occurences[i].c += occurences[i].a - occurences[i].b;
		occurences[i].temp = occurences[i].b;
		occurences[i].b = 0;
	}
	sort(occurences, occurences + n + 1, abc_less());
	balancedWords += numberOfBalancedWordsWithPrefixSums(occurences, n);
	//printOccurences(occurences, n);
	//cout << balancedWords << endl;
	
	for (int i=0;i<=n;++i)
	{
		occurences[i].b = occurences[i].temp;
		occurences[i].c += occurences[i].b;
		occurences[i].b -= occurences[i].a;
		occurences[i].temp = occurences[i].a;
		occurences[i].a = 0;
	}
	sort(occurences, occurences + n + 1, abc_less());
	balancedWords += numberOfBalancedWordsWithPrefixSums(occurences, n);
	//printOccurences(occurences, n);
	//cout << balancedWords << endl;
	
	for (int i=0;i<=n;++i)
	{
		occurences[i].a = occurences[i].temp;
		occurences[i].b += occurences[i].a;
		occurences[i].c -= occurences[i].a;
		occurences[i].temp = occurences[i].a;
		occurences[i].a = 0;
	}
	sort(occurences, occurences + n + 1, abc_less());
	balancedWords += numberOfBalancedWordsWithPrefixSums(occurences, n);
	//printOccurences(occurences, n);
	
	delete[] occurences;
	
	cout << balancedWords << endl;
	return 0;
	
	
}