Sum of Beauty of All Substrings
Medium · Hash Table, String, Counting
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.
Example 1
Input: s = "aabcb" Output: 5
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.
Starter code (python)
class Solution(object):
def beautySum(self, s):
"""
:type s: str
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
Previous: Check if Number is a Sum of Powers of Three · Next: Merge Strings Alternately · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.