-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_Appeal_of_String.cpp
More file actions
49 lines (35 loc) · 894 Bytes
/
Leetcode_Appeal_of_String.cpp
File metadata and controls
49 lines (35 loc) · 894 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
long long appealSum(string s) {
int n = s.length();
long long ans = 0;
for(char ch = 'a'; ch <= 'z'; ch++) {
long long contrib = 1LL * n * (n + 1) / 2;
int last = -1;
for(int i = 0; i < n; i++) {
if(s[i] == ch) {
int len = i - last - 1;
if(len > 0) {
contrib -= 1LL * len * (len + 1) / 2;
}
last = i;
}
}
// handle suffix after last occurrence
int len = n - last - 1;
if(len > 0) {
contrib -= 1LL * len * (len + 1) / 2;
}
ans += contrib;
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
cout << appealSum(s) << "\n";
return 0;
}