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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <bits/stdc++.h>
#define FOR(i,n) for(int i = 0; i<n; ++i)
#define FOREACH(v, a) for(auto & v : (a))
#define X first
#define Y second
#define PR std::pair
#define MPR std::make_pair
typedef long long ll;
typedef std::vector<int> VI;

using namespace std;

#pragma region Ready functions

int fastPower(int a, int b){
    int res=1, pop_a = a;
    while(b){
        if(b&1) res *= pop_a;
        pop_a *= pop_a;
        b>>=1;
    }
    return res;
}

#pragma endregion

#define HEIGHT_BASE 300001

ll histogram[300003][3]; //first letter = 1, second letter = -1
ll heightCount[600003];    //height + height_base

int letterCount[300002][3];

string n;


ll DoDoubleLetter(char letterA, char letterB, int hisIndex){

    ll finalRes = 0;

    for(int lewy = 0; lewy<n.size() - 1; lewy++){

        if(n[lewy] != letterA && n[lewy] != letterB) continue;

        int prawy = lewy+1;


        for(; prawy < n.size(); prawy++){
            if(n[prawy] != letterA && n[prawy] != letterB) break;
        }

        //cout<<letterA<<" "<<letterB<<" "<<lewy<<" "<<prawy<<endl;

        if(prawy == n.size()) prawy--;

        if(prawy - lewy == 0) continue;
    
        if(n[prawy] != letterA && n[prawy] != letterB) prawy--;

        //cout<<letterA<<" "<<letterB<<" "<<lewy<<" "<<prawy<<endl;

        /*

        Now we have lewy and prawo sorted.
        We can now produce the histogram.

        */

        heightCount[HEIGHT_BASE] = 1;

        ll mini = 0, maxi = 0;

        for(int i = lewy; i<=prawy; i++){
            
            if(n[i] == letterA){
                histogram[i+1][hisIndex] = histogram[i][hisIndex] + 1;
            }
            else{
                histogram[i+1][hisIndex] = histogram[i][hisIndex] - 1;
            }

            heightCount[histogram[i+1][hisIndex] + HEIGHT_BASE]++;

            mini = min(histogram[i+1][hisIndex], mini);
            maxi = max(histogram[i+1][hisIndex], maxi);
        }

        ll tempRes = 0;

        for(int i = mini; i<=maxi; i++){
            tempRes += (heightCount[i + HEIGHT_BASE] * (heightCount[i + HEIGHT_BASE]-1))/2;
            heightCount[i + HEIGHT_BASE] = 0;
        }

        lewy = prawy + 1;

        //cout<<tempRes<<endl;

        finalRes += tempRes;


    }

    return finalRes;

}


map<pair<int,int>, int> pairCounter;

ll DoTripleLetters(){

    ll tempRes = 0;

    pairCounter[MPR(0,0)] = 1;

    FOR(i, n.size()){

        tempRes += pairCounter[MPR(letterCount[i+1][0] - letterCount[i+1][1], letterCount[i+1][0] - letterCount[i+1][2])];
        pairCounter[MPR(letterCount[i+1][0] - letterCount[i+1][1], letterCount[i+1][0] - letterCount[i+1][2])]++;
    }

    return tempRes;

}

ll result = 0;


int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    

    cin>>n;

    if(n.size()==1){
        cout<<1<<endl;
        return 0;
    }


#pragma region Single Letters

    char currentLetter = 'z';
    ll currentLength = 0;

    FOR(i, n.size()){

        //Counting letters
        FOR(j, 3) letterCount[i+1][j] = letterCount[i][j];
        letterCount[i+1][n[i]-'a']++;

        if(currentLetter == 'z'){
            currentLetter = n[i];
            currentLength = 1;
        }
        else if(currentLetter == n[i]){
            currentLength++;

            if(i == n.size()-1){
                result += (currentLength*(currentLength+1))/2;
            }
        }
        else{
            currentLetter = n[i];
            result += (currentLength*(currentLength+1))/2;
            currentLength = 1;

            if(i == n.size()-1) result++;
        }
    }


#pragma endregion


#pragma region Double Letters

    //cout<<result<<endl;
    result += DoDoubleLetter('a',  'b', 0);
    //cout<<result<<endl;
    result += DoDoubleLetter('a',  'c', 1);
    //cout<<result<<endl;
    result += DoDoubleLetter('c',  'b', 2);

#pragma endregion
    
    ll res = DoTripleLetters();

    //cout<<res<<endl;

    cout<<result + res<<endl;


}