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

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    cin>>s;
    int n=s.length();
    vector<vector<int>> countLetters(n+1, vector<int>(3,0));
    for(int i=0; i<n; ++i)
    {
        countLetters[i+1]=countLetters[i];
        ++countLetters[i+1][s[i]-'a'];
    }

    int counter=0;
    for(int i=0; i<n+1; ++i)
        for(int j=i+1; j<n+1; ++j)
        {
            int a=countLetters[j][0]-countLetters[i][0];
            int b=countLetters[j][1]-countLetters[i][1];
            int c=countLetters[j][2]-countLetters[i][2];
            if(a<b)
                swap(a,b);
            if(b<c)
                swap(b,c);
            if(a<b)
                swap(a,b);
            if(b==0)
                b=a;
            if(c==0)
                c=a;
            if(a==b&&a==c)
                ++counter;
        }

    cout<<counter<<endl;
    return 0;
}