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
#include <cstdio>
#include <map>
using namespace std;

const int maxN = 4e5;
char S[maxN];
int N;
long long result;

void count1() {
    int last = 0;
    char c = '?';
    for (int i = 0; i < N; ++i)
        if (S[i] != c) {
            long long len = i - last;
            result += len * (len + 1) / 2;
            last = i;
            c = S[i];
        }
    result += (N - last) * 1LL * (N + 1 - last) / 2;
}


void count2(char c1, char c2) {
    map<int, int> cnt;
    int sum = 0;
    for (int i = 0; i < N; ++i) {
        cnt[sum]++;
        if (S[i] == c1)
            sum++;
        else if (S[i] == c2)
            sum--;
        else {
            sum = 0;
            cnt.clear();
            continue;
        }
        result += cnt[sum];
    }
}

void count3() {
    map<char, int> val;
    val['a'] = 1;
    val['b'] = maxN;
    val['c'] = -maxN - 1;
    long long sum = 0;
    map<long long, int> cnt;
    for (int i = 0; i < N; ++i) {
        cnt[sum]++;
        sum += val[S[i]];
        result += cnt[sum];
    }
}

int main() {
    scanf("%s", S);
    while (S[N])
        N++;
    count1();
    count2('a', 'b');
    count2('b', 'c');
    count2('c', 'a');
    count3();
    printf("%lld\n", result);
    return 0;
}