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

bool check_balans(int &a_1, int &a_2, int &b_1, int &b_2, int &c_1, int &c_2) {
    int sum_a = a_1 - a_2;
    int sum_b = b_1 - b_2;
    int sum_c = c_1 - c_2;

    if (sum_a == 0) {
        if (sum_b == 0) {
            if (sum_c == 0)
                return false;
            else return true;
        }
        else {
            if (sum_c == sum_b || sum_c == 0)
                return true;
            else return false;
        }
    }
    else {
        if (sum_b == sum_a || sum_b == 0) {
            if (sum_c == sum_a || sum_c == 0)
                return true;
            else return false;
        }
        else return false;
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    std::string input;
    std::cin >> input;

    unsigned int n = input.size();

    auto *A = new int [n + 1];
    auto *B = new int [n + 1];
    auto *C = new int [n + 1];

    A[0] = 0;
    B[0] = 0;
    C[0] = 0;

    for (int i = 0; i < n; ++i) {
        A[i + 1] = A[i];
        B[i + 1] = B[i];
        C[i + 1] = C[i];
        if (input[i] == 'a')
            ++A[i + 1];
        if (input[i] == 'b')
            ++B[i + 1];
        if (input[i] == 'c')
            ++C[i + 1];
    }

    long long output = 0;
    for (int i = 0; i < n; ++i)
        for (int j = i; j < n; ++j)
            if (check_balans(A[j + 1], A[i], B[j + 1], B[i], C[j + 1], C[i]))
                ++output;

    std::cout << output;

    delete [] A;
    delete [] B;
    delete [] C;

    return 0;
}