Sum of Beauty of All Substrings
Advanced String Algorithms DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Sum of Beauty of all substring, Strings, Subarray, Frequency Counting, Loops, Arrays, Time Complexity, Space Complexity, Brute Force, Optimization, string properties, string manipulation, string generation, frequency counting, general programming, time complexity analysis, Substrings, Character Frequency.
Sum of Beauty of All Substrings Problem Statement A string's beauty is defined as the difference between the frequency of the most frequent character and the frequency of the least frequent character present in that string. Only characters that appear at least once in the string are considered for calculating frequencies. If a string contains only one distinct character, its beauty is 0. Given a string s, find the sum of the beauties of all its substrings. Input Specification The input consists of a single line containing a string s. Output Specification Output a single integer representing the total sum of beauties of all substrings of s. Constraints 1 <= s.length <= 500 s consists only of lowercase English letters.