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
#pragma GCC optimize("O3")

#include <iostream>
#include <cstring>

using namespace std;

char word[300000];

int count = 0;

void checkIfBalanced() {
    int length = strlen(word);

    int a, b, c;

    for(int start=0; start<length; ++start) {
        a = b = c = 0;

        for(int i=start; i<length; ++i) {

            word[i] == 'a' ? ++a : (word[i] == 'b' ? ++b : ++c);

            if(!((a != b && b != c && a != c) || (a == b && b != c && c != 0 && a != 0) || (b == c && a != c && a != 0 && b != 0) || (a == c && b != c && b != 0 && c != 0) || (a == 0 && b == 0 && c == 0))) {
                ++count;
            }
        }
    }
}

int main() {
    cin >> word;

    checkIfBalanced();

    cout << count;

    return 0;
}