class Solution {public: int beautySum(string s) { int ans = 0; for (int i = 0; i < s.length(); i++) for (int j = i + 1; j <= s.length(); j++) ans += getBeauty(s.substr(i, j - i)); return ans; } int getBeauty(string s) { int count[26] = {0}; for (int i = 0; i < s.length(); i++) { if (s[i] >= 'a' && s[i] <= 'z') { count[s[i] - 'a']++; } } int max = 1; int min = s.length(); for (int i = 0; i < 26; i++) { if (count[i] > max) max = count[i]; if (count[i] != 0 && count[i] < min) min = count[i]; } return max - min; }};