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

typedef long long int LL;

void normalise(vector<int>& vec)
{
    const int minElem = *min_element(vec.cbegin(), vec.cend());
    for(int i = 0; i < vec.size(); ++i)
    {
        vec[i] -= minElem;
    }
}

LL countSubstrings(const string& text, unordered_set<char> allowedChars)
{
    unordered_map<char, int> charToPos;
    int pos = 0;
    for(const char c : allowedChars)
    {
        charToPos[c] = pos++;
    }

    map<vector<int>, LL> statesCount;
    vector<int> charsCount(charToPos.size(), 0);
    ++statesCount[charsCount];
    for(const char c : text)
    {
        if(allowedChars.find(c) == allowedChars.cend()) {
            continue;
        }

        ++charsCount[charToPos[c]];
        normalise(charsCount);
        ++statesCount[charsCount];
    }

    LL res = 0;
    for(const auto& elem : statesCount)
    {
        res += (elem.second * (elem.second-1) / 2);
    }

    return res;
}

vector<string> split(const string& text, unordered_set<char> allowedChars)
{
    vector<string> res;

    string curr;
    unordered_set<char> missingChars = allowedChars;
    for(char c : text)
    {
        if(allowedChars.find(c) == allowedChars.cend()) {
            if(missingChars.empty() && !curr.empty())
            {
                res.push_back(curr);
            }

            missingChars = allowedChars;
            curr = "";

            continue;
        }

        curr += c;
        missingChars.erase(c);
    }

    if(missingChars.empty() && !curr.empty())
    {
        res.push_back(curr);
    }

    return res;
}

LL countSubstrings(const string& s)
{
    const vector<unordered_set<char>> allowedCharsSet = { {'a'}, {'b'}, {'c'}, {'a', 'b'}, {'a', 'c'}, {'b', 'c'}, {'a', 'b', 'c'} };
    LL res = 0;
    for(const auto& allowedChars : allowedCharsSet)
    {
        vector<string> chunks = split(s, allowedChars);
        for(const string& chunk : chunks)
        {
            res += countSubstrings(chunk, allowedChars);
        }
    }

    return res;
}

int main()
{
    string s;
    cin >> s;

    cout << countSubstrings(s) << endl;

    return 0;
}