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

  1. class Solution {
  2. public:
  3. int beautySum(string s) {
  4. int ans = 0;
  5. for (int i = 0; i < s.length(); i++)
  6. for (int j = i + 1; j <= s.length(); j++)
  7. ans += getBeauty(s.substr(i, j - i));
  8. return ans;
  9. }
  10. int getBeauty(string s) {
  11. int count[26] = {0};
  12. for (int i = 0; i < s.length(); i++) {
  13. if (s[i] >= 'a' && s[i] <= 'z') {
  14. count[s[i] - 'a']++;
  15. }
  16. }
  17. int max = 1;
  18. int min = s.length();
  19. for (int i = 0; i < 26; i++) {
  20. if (count[i] > max)
  21. max = count[i];
  22. if (count[i] != 0 && count[i] < min)
  23. min = count[i];
  24. }
  25. return max - min;
  26. }
  27. };