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

using namespace std;

int main()
{
  ios::sync_with_stdio(false); cin.tie(nullptr);
  string S;
  cin >> S;

  map<pair<int, int>, int> z;
  z[{0, 0}]++;
  int64_t result;
  int a = 0, b = 0, c = 0;
  for(size_t i = 0; i < S.size(); i++) {
    if(S[i] == 'a')
      a++;
    else if(S[i] == 'b')
      b++;
    else if(S[i] == 'c')
      c++;
    result += z[{b - a, c - a}]++;
  }
  cout << result << endl;
}