mediumCountingHash TableString 0 views

Sum of Beauty of All Substrings

The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.

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.

Sum of Beauty of All Substrings diagram

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

Constraints

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.