#1781
Sum of Beauty of All Substrings
pupil · 525 · lc medium +28 · premium · verified · 73.6% accepted · 1,568 likes · top 84%
Description
The beauty of a string is the frequency of its most common character minus the frequency of its least common character. Given a string s, return the sum of beauty values over all of its substrings.
Example 1:
Input: s = "aabcb"
Output: 5
Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
Example 2:
Input: s = "aabcbaa"
Output: 17
Code
1
2
3