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
#include <iostream>
#include <stdint.h>
#include <string>

int MAX = 300'000;

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

  int64_t result = 0;
  int MAX = s.length();

  for (int i = 0; i < MAX; ++i) {

    int a = 0;
    int b = 0;
    int c = 0;

    for (int j = i; j < MAX; ++j) {
      switch (s[j]) {
      case 'a':
        a++;
        break;
      case 'b':
        b++;
        break;
      case 'c':
        c++;
        break;
      }

      if ((a == b && b == c) || (a == b && c == 0) || (a == c && b == 0) ||
          (b == c && a == 0) || (a == 0 && b == 0) || (a == 0 && c == 0) ||
          (b == 0 && c == 0)) {

        result++;

        // DEBUG
        //        std::cerr << i << " " << j << std::endl;
        //        std::cerr << s.substr(i, j - i + 1) <<std::endl;
      }
    }
  }

  std::cout << result << std::endl;
  return 0;
}