#include <iostream>
#include <string>
#include <time.h>
inline bool check_bal(int letters[]) {
#define a letters['a']
#define b letters['b']
#define c letters['c']
	if (a == b) {
		if ((b == c) || (c == 0) || (a == 0)) return true;
	}
	else if (b == c) {
		if ((a == 0) || (b == 0)) return true;
	}
	else if (a == c) {
		if ((a == 0) || (b == 0)) return true;
	}
	return false;
#undef a
#undef b
#undef c
}
uint64_t balanced_subwords(const char* word) {
	uint64_t ctr = 0;
	int letters[100];
	for (int i = 0; word[i] != '\0'; i++) {
		letters['a'] = 0;
		letters['b'] = 0;
		letters['c'] = 0;
		for (int j = i; word[j] != '\0'; j++) {
			letters[word[j]]++;
			if (check_bal(letters)) ctr++;
		}
	}
	return ctr;
}
int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	const int MAX_SIZE = 300001;
	char tab[MAX_SIZE + 1];
	std::cin >> tab;
	std::cout << balanced_subwords(tab);
	return 0;
}
        | 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 | #include <iostream> #include <string> #include <time.h> inline bool check_bal(int letters[]) { #define a letters['a'] #define b letters['b'] #define c letters['c'] if (a == b) { if ((b == c) || (c == 0) || (a == 0)) return true; } else if (b == c) { if ((a == 0) || (b == 0)) return true; } else if (a == c) { if ((a == 0) || (b == 0)) return true; } return false; #undef a #undef b #undef c } uint64_t balanced_subwords(const char* word) { uint64_t ctr = 0; int letters[100]; for (int i = 0; word[i] != '\0'; i++) { letters['a'] = 0; letters['b'] = 0; letters['c'] = 0; for (int j = i; word[j] != '\0'; j++) { letters[word[j]]++; if (check_bal(letters)) ctr++; } } return ctr; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); const int MAX_SIZE = 300001; char tab[MAX_SIZE + 1]; std::cin >> tab; std::cout << balanced_subwords(tab); return 0; } | 
 
            
         English
                    English