$记录每个位置所对应的字符的之前和之后出现的位置,两者相乘则是该位置的字符的贡献,依次相加即可$
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long ll;
char str[N];
int pre[N];
int nex[N];
int h[26];
ll ans;
int main(){
scanf("%s",str + 1);
int len = strlen(str + 1);
for(int i=1;i<=len;i++)
{
pre[i] = 1;
nex[i] = len;
}
for(int i=1;i<=len;i++){
int t = str[i] - 'a';
pre[i] = h[t];
h[t] = i;
}
for(int i=0;i<26;i++) h[i] = len+1;
for(int i=len;i>=1;i--){
int t = str[i] - 'a';
nex[i] = h[t];
h[t] = i;
}
for(int i=1;i<=len;i++)
{
ans+=(ll)(i-pre[i])*(nex[i]-i);
}
cout<<ans<<endl;
return 0;
}