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
#include <iostream>
#include <map>

using namespace std;

typedef long long ll;

int n;
string a;

ll f0()
{
    ll odp = 0;
    map< pair<ll, ll>, ll > M;
    ll rAB = 0, rAC = 0;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == 'a')
        {
            ++rAB;
            ++rAC;
        }
        if (a[i] == 'b')
            --rAB;
        if (a[i] == 'c')
            --rAC;
        odp += M[make_pair(rAB, rAC)];
        if (rAB == 0 && rAC == 0)
            ++odp;
        ++M[make_pair(rAB, rAC)];
    }
    return odp;
}

ll f1(char c)
{
    char x = 'a', y = 'b';
    if (c == 'a')
    {
        x = 'b';
        y = 'c';
    }
    if (c == 'b')
    {
        x = 'a';
        y = 'c';
    }
    ll odp = 0, r = 0;
    map<ll, ll> M;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == c)
        {
            M.clear();
            r = 0;
            continue;
        }
        if (a[i] == x)
            ++r;
        if (a[i] == y)
            --r;
        odp += M[r];
        if (r == 0)
            ++odp;
        ++M[r];
    }
    return odp;
}

ll f2()
{
    ll odp = 0, il = 0;
    for (int i = 0; i < n; ++i)
    {
        if (i != 0 && a[i] != a[i - 1])
        {
            odp += (il * (il - 1)) / 2 + il;
            il = 0;
        }
        ++il;
    }
    odp += (il * (il - 1)) / 2 + il;
    return odp;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> a;
    n = a.size();
    cout << f0() + f1('a') + f1('b') + f1('c') + f2();
    return 0;
}