-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeautySum.cpp
More file actions
29 lines (29 loc) · 862 Bytes
/
beautySum.cpp
File metadata and controls
29 lines (29 loc) · 862 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
class Solution {
public:
int getBeauty(int count[26]) {
int minFreq = 502, maxFreq = -1;
for (int i = 0; i < 26; ++i) {
if (!count[i]) continue;
minFreq = std::min(minFreq, count[i]);
maxFreq = std::max(maxFreq, count[i]);
}
return maxFreq - minFreq;
}
int beautySum(string s) {
if (s.size() < 3) return 0;
int n = s.size(), ans = 0;
for (int len = 3; len <= n; ++len) {
int count[26] = {};
for (int i = 0; i < len; ++i) {
++count[s[i] - 'a'];
}
ans += getBeauty(count);
for (int l = 0, r = len; r < n; ++l, ++r) {
--count[s[l] - 'a'];
++count[s[r] - 'a'];
ans += getBeauty(count);
}
}
return ans;
}
};