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
/*
 * zba.cpp
 *
 *  Created on: 4 gru 2021
 *      Author: chodnik
 */

#include <iostream>
#include <string>

using namespace std;

class Counter {
public:
	int abc[3]; // = {0, 0, 0};
	Counter() :
			abc { 0 } {
	}
	void add(char s, int groupSize) {
		abc[s - 'a'] += groupSize;
	}
	bool has_balanced_in_range(char groupChar, int groupSize) {
		int v0 = abc[groupChar - 'a'], v1 = 0, v2 = 0;
		if (groupChar == 'a') {
			if (abc[1] < abc[2]) {
				v1 = abc[1];
				v2 = abc[2];
			} else {
				v1 = abc[2];
				v2 = abc[1];
			}
		} else if (groupChar == 'b') {
			if (abc[0] < abc[2]) {
				v1 = abc[0];
				v2 = abc[2];
			} else {
				v1 = abc[2];
				v2 = abc[0];
			}
		} else if (groupChar == 'c') {
			if (abc[0] < abc[1]) {
				v1 = abc[0];
				v2 = abc[1];
			} else {
				v1 = abc[1];
				v2 = abc[0];
			}
		}
		if (v0 >= v2) {
			return false;
		} else if ((v1 == 0 || v1 == v2) && v0 < v2 && v0 + groupSize >= v2) {
			return true;
		} else {
			return false;
		}
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	string word; // ("aabbabcccba");
    cin >> word;
	int L = word.length();
	int left[L];// = new int[L];
	{
		char prev = 'z';
		int total = 0;
		for (int i = L - 1; i >= 0; i--) {
			if (word[i] == prev) {
				total++;
			} else {
				prev = word[i];
				total = 1;
			}
			left[i] = total;
		}
	}
	long long result = 0;
	for (int i = 0; i < L; i++) {
		int from = i;
		int groupSize = left[from];
		result += groupSize;
		Counter counterFrom;
		counterFrom.add(word[i], groupSize);
		from += groupSize;

		while (from < L) {
			groupSize = left[from];
			char groupChar = word[from];
			if (counterFrom.has_balanced_in_range(groupChar, groupSize)) {
				result++;
			}
			counterFrom.add(groupChar, groupSize);
			from += groupSize;
		}
//		if(i%1000==0){
//			cout<<"i="<<i<<endl;
//		}
	}
	cout << result << endl;
	return 0;
}