# 1781. 所有子字符串美丽值之和

class Solution {
public:
int beautySum(string s) {
int res = 0;
for (int i = 0; i < s.size(); i++) {
vector<int> cnt(26);
int maxFreq = 0;
for (int j = i; j < s.size(); j++) {
cnt[s[j] - 'a']++;
maxFreq = max(maxFreq, cnt[s[j] - 'a']);
int minFreq = s.size();
for (int k = 0; k < 26; k++) {
if (cnt[k] > 0) {
minFreq = min(minFreq, cnt[k]);
}
}
res += maxFreq - minFreq;
}
}
return res;
}
};

复杂度分析

  • 时间复杂度:$O (C*n^2)$
  • 空间复杂度:$O©$