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
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 300005;

string s;
int a, b, c;
map<ll, int> cnt3;
ll res = 0;

ll id (int a, int b, int c) {
    return 1LL * (a + MAXN) * MAXN * MAXN + 1LL * (b + MAXN) * MAXN + (c + MAXN);
}

int32_t main() {
    ios_base::sync_with_stdio(0);

    cin >> s;

    // 1 distinct letter.
    int j = 0;
    while (j < s.size()) {
        int k = 0;
        while (j + k + 1 < s.size() && s[j] == s[j + k + 1])
            k++;

        while (k >= 0) {
            res += k + 1;
            j++;
            k--;
        }
    }


    // 3 distinct letters.
    cnt3[id(0, 0, 0)]++;
    for (auto i : s) {
        if (i == 'a') {
            a++, c--;
        } else if (i =='b') {
            a--, b++;
        } else {
            b--, c++;
        }

        res += cnt3[id(a, b, c)]++;
    }

    // 2 distinct letters.
    for (char l = 'a'; l <= 'b'; l++) {
        for (char r = l + 1; r <= 'c'; r++) {
            ll sum = 0;
            
            map<ll, int> cnt = {{0, 1}};
            for (auto i : s) {
                if (i == l) {
                    sum++;
                } else if(i == r) {
                    sum--;
                } else {
                    sum += 300001;
                }

                res += cnt[sum]++;
            }
        }
    }



    cout << res << "\n";
}