Description
The beauty of a string is the difference in frequencies between the most frequent and least frequent characters. Given a string s, return the sum of beauty of all of its substrings.
Examples
Input:
s = "aabcb"Output:
5Explanation:
Beauty of substrings sums to 5.
Input:
s = "a"Output:
0Explanation:
Edge case with a single character.
Input:
s = "aaaa"Output:
0Explanation:
All substrings contain only the character 'a', so the difference between most and least frequent characters is always 0. For example: 'a' has beauty 0, 'aa' has beauty 0, 'aaa' has beauty 0, and 'aaaa' has beauty 0. The sum is 0.
Constraints
- •
1 ≤ s.length ≤ 500