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<bits/stdc++.h>
#define f first
#define s second

using namespace std;

const int N = 3e5+3;

typedef long long ll;
typedef pair<int,int> pint;

int n;
int T[N+3];
int sp[N+3][4];
ll ans = 0;
map<pint,ll> cnt;
map<int,ll> cnt2;

void zlicz(int x, int y){
	cnt2.clear();
	int roz = 0;
	cnt2[0]=1;
	for(int i = 1;i<=n;i++){
		if(T[i]==x){
			roz += 1;
		}
		if(T[i]==y){
			roz -= 1;
		}
		if((T[i]==y||T[i]==x)){
			cnt2[roz]+=1;
		}
		if((T[i]!=x && T[i]!=y) || i == n){
			for(auto& kv : cnt2){
				int co = kv.f;
				ll w = kv.s;
				ans += max(0LL,(w*(w-1))/2LL); 
			}
			cnt2.clear();
			cnt2[0]=1;
			roz = 0;
		}
	}
}

void jed(){
	int p = 1;
	for(int k = 1;k<=n;k++){
		if(T[p]==T[k]){
			ans += k-p+1;
		}else{
			p = k;
			ans += 1;
		}
	}
	zlicz('a','b');
	zlicz('a','c');
	zlicz('b','c');
}

int main(){
	ios_base::sync_with_stdio(0);
	
	string inp;
	cin>>inp;
	n = inp.size();
	for(int i = 0;i<inp.size();i++){
		T[i+1] = inp[i];
		sp[i+1][1] = sp[i][1] + 2*int(T[i+1]=='a') - 1;
		if(inp[i]=='c'){
			sp[i+1][1] = sp[i][1];
		}
		sp[i+1][2] = sp[i][2] + 2*int(T[i+1]=='b') - 1;
		if(inp[i]=='a'){
			sp[i+1][2] = sp[i][2];
		}
	}
	set<pint> st;
	for(int i = 0;i<=n;i++){
		pint ten = {sp[i][1],sp[i][2]};
		cnt[ten] += 1;
		st.insert(ten);
	}
	jed();
	
	for(pint ten : st){
		ans += max(0LL,(cnt[ten]*(cnt[ten]-1))/2LL);
	}
	
	cout<<ans<<endl;
	
	return 0;
}