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
//   _|                                                _|
//   _|  _|    _|    _|    _|_|    _|_|_|      _|_|_|  _|  _|      _|_|      _|_|
//   _|_|      _|    _|  _|_|_|_|  _|    _|  _|_|      _|_|      _|_|_|_|  _|_|_|_|
//   _|  _|    _|    _|  _|        _|    _|      _|_|  _|  _|    _|        _|
//   _|    _|    _|_|_|    _|_|_|  _|_|_|    _|_|_|    _|    _|    _|_|_|    _|_|_|
//                   _|            _|
//               _|_|              _|
#include <iostream>
#include <algorithm>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define qed return 0

typedef long long ll;
typedef pair<ll, ll> pll;

int read() {
    int n = 0; bool b = 0; char c = getchar();
    for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
    for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
    if (b) return -n;
    return n;
}

const ll N = 3e5+10;
int n;
string s;
unordered_map<ll, ll> M;

void rotate() {
    foru (i, n) s[i] = (s[i]-'a'+1)%3 + 'a';
}

pair<ll, ll> diffs[2*N];
ll t = 0;
ll count() {
    ll res = 0;
    ll d = 0;
    ++t;
    diffs[N+0] = {1, t}; // one additional if for a == b (=> a - b == 0)
    foru (i, n) {
        if (s[i] == 'c') {
            d = 0; ++t;
            diffs[N+0] = {1, t}; // one additional if for a == b (=> a - b == 0)
            df("cleared\n");
            continue;
        }
        
        if (s[i] == 'a') ++d;
        if (s[i] == 'b') --d;
        df("%lld - {%lld, %lld}\n", d, diffs[N+d].x, diffs[N+d].y);
        
        if (diffs[N+d].y != t) diffs[N+d] = {0, t};
        res += diffs[N+d].x;
        ++diffs[N+d].x;
    }
    df("res: %lld\n", res);
    return res;
}

ll countsingles() {
    ll res = 0;
    char c = s[0];
    ll len = 1;
    for (int i = 1; i < n; ++i) {
        if (s[i] == c) {
            ++len;
        } else {
            res += (len * (len + 1)) / 2;
            c = s[i], len = 1;
        }
    }
    res += (len * (len+1))/2;
    return res;
}

int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    cin >> s;
    n = sz(s);
    int a = 0, b = 0, c = 0;
    // all 3 letters
    ll s1 = 0;
    ++M[(N + 0) * 2 * N + (N + 0)];
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'a') ++a;
        if (s[i] == 'b') ++b;
        if (s[i] == 'c') ++c;
        // |d| <= 3e5
        ll d1 = a-b, d2 = a-c;
        // df("(%lld, %lld)\n", d1, d2);
        ll res = M[(N + d1) * 2 * N + (N + d2)];
        s1 += res;
        ++M[(N + d1) * 2 * N + (N + d2)];
    }
    // cout << s1 << endl;
    // return 0;
    
    // all 2 letters
    ll s2 = 0;
    ll _res1, _res2, _res3;
    df("%s\n", s.c_str());
    s2 += (_res1 = count()); rotate();
    df("%s\n", s.c_str());
    s2 += (_res2 = count()); rotate();
    df("%s\n", s.c_str());
    s2 += (_res3 = count());
    df("%lld %lld %lld\n", _res1, _res2, _res3);
    // all 1 letters
    ll s3 = countsingles();
    ll total = s1 + s2 + s3;
    df("%lld %lld %lld\n", s1, s2, s3);
    cout << total << "\n";
    return 0;
}