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
#include <bits/stdc++.h>

#pragma GCC optimize(-O3)
#pragma GCC optimize(Ofast)
#pragma GCC optimize("unroll-loops")

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << (x))
#define sz(x) (int)x.size()
#define fout(x) {cout << x << "\n"; return; }

using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = 1e5;
const int M = pw(16);
const long long mod = 998244353;
const int N = 2e5;
const int inf = 1e9;

template <typename T> void vout(T s){cout << s << endl;exit(0);}

string s;

long long solve(int l, int r){
    if(l == r) return 1;
    int m = (l + r) >> 1;
    map <vector <int>, int> mp;

    vector <int> v(3), b(3), wh(3);

    long long ans = 0;

    for(int step = 1; step < pw(3); step++){        
        int R = 0;        
        mp.clear();
        for(int j = 0; j < 3; j++) if((pw(j) & step)) wh[j] = R++;
        v.resize(R);
        b.resize(R);
        fill(all(v), 0);                

        for(int i = m; i >= l; i--){   
            if((pw(s[i] - 'a') & step) == 0) break;
            v[wh[s[i] - 'a']]++;     
            b = v;
            int mn = *min_element(all(b));            
            for(auto &j : b) j -= mn;            
            mp[b]++;
        }

        fill(all(v), 0);

        for(int i = m + 1; i <= r; i++){            
            if((pw(s[i] - 'a') & step) == 0) break;
            v[wh[s[i] - 'a']]++;     
            b = v;
            int mx = *max_element(all(v));                    
            for(auto &j : b) j = mx - j;                    
            ans += mp[b];
        }        
    }
    

    return ans + solve(l, m) + solve(m + 1, r);
}

long long solve1(string s){
    vector <int> v(3), b;
    int n = s.size();
    long long ans = 0;
    for(int st = 1; st < pw(3); st++){
        fill(all(v), 0);
        map <vector <int>, int> mp;
        for(int i = 0; i < n; i++){            
            b = v;
            int mx = 0;            
            for(int j = 0; j < 3; j++) if((pw(j) & st)) mx = max(mx, b[j]);
            for(int j = 0; j < 3; j++) if((pw(j) & st)) b[j] = mx - b[j];                           
            mp[b]++;

            v[s[i] - 'a']++;     
            b = v;
            mx = 0;       
            for(int j = 0; j < 3; j++) if((pw(j) & st)) mx = max(mx, b[j]);
            for(int j = 0; j < 3; j++) if((pw(j) & st)) b[j] = mx - b[j];                           
            ans += mp[b];
        }
    }    
    return ans;
}

long long brute(string s){
    vector <int> v(3);
    int n = s.size();
    long long ans = 0;
    for(int i = 0; i < n; i++){
        fill(all(v), 0);
        for(int j = i; j < n; j++){
            int mn, mx;
            mn = 1e9, mx = -1e9;
            v[s[j] - 'a']++;
            for(auto c : v) if(c) mn = min(mn, c), mx = max(mx, c);
            if(mn == mx){
                ans++;
                //if(i != j) cout << i << " " << j << "\n";
            }
        }
    }
    return ans;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> s;

    //cout << solve(0, s.size() - 1) << "\n";
    cout << solve1(s) << "\n";

    //cout << brute(s) << "\n";
    

    return 0;
}