Sum of Beauty of All Substrings

Medium

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:5
Explanation:

Beauty of substrings sums to 5.

Input:s = "a"
Output:0
Explanation:

Edge case with a single character.

Input:s = "aaaa"
Output:0
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!