Sum of Beauty of All Substrings - Complete Solution Guide
Sum of Beauty of All Substrings is LeetCode problem 1781, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
The beauty of a string is the difference in frequencies between the most frequent and least frequent characters. For example, the beauty of "abaacc" is 3 - 1 = 2 . Given a string s , return the sum of beauty of 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 Constraints: 1 <= s.length <= 500 s consists of only lowercase E
Detailed Explanation
The problem requires calculating the sum of the "beauty" of all possible substrings of a given string `s`. The beauty of a string is defined as the difference between the frequency of the most frequent character and the frequency of the least frequent character. The input string `s` consists of lowercase English letters, and its length is between 1 and 500, inclusive. The output should be the sum of the beauty of all substrings of `s`.
Solution Approach
The provided solution uses a brute-force approach to generate all possible substrings of the input string `s`. For each substring, it calculates the frequency of each character, finds the maximum and minimum frequencies, and computes the beauty of the substring. Finally, it sums the beauty values of all substrings to obtain the final result. The solution iterates through all possible starting positions `i` from 0 to n-1, where n is the length of the string. For each starting position, it iterates through all possible ending positions `j` from `i` to n-1, thereby generating all substrings.
Step-by-Step Algorithm
- Step 1: Initialize `total_beauty` to 0 to store the sum of beauty values of all substrings.
- Step 2: Iterate through all possible starting positions `i` of the substring from 0 to `n-1`.
- Step 3: For each starting position `i`, create a frequency array `freq` of size 26 to store the frequency of each lowercase English letter.
- Step 4: Iterate through all possible ending positions `j` of the substring from `i` to `n-1`.
- Step 5: Increment the frequency of the character `s[j]` in the `freq` array.
- Step 6: Initialize `max_f` to 0 and `min_f` to infinity.
- Step 7: Iterate through the `freq` array to find the maximum and minimum non-zero frequencies.
- Step 8: Calculate the beauty of the current substring as `max_f - min_f`.
- Step 9: Add the beauty of the current substring to `total_beauty`.
- Step 10: After iterating through all possible substrings, return `total_beauty`.
Key Insights
- Insight 1: Substrings can be generated by iterating through all possible start and end indices.
- Insight 2: For each substring, the frequency of each character needs to be calculated.
- Insight 3: The maximum and minimum frequencies need to be determined for each substring.
- Insight 4: Use an array of size 26 to efficiently store and update the character frequencies.
Complexity Analysis
Time Complexity: O(n^3)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, String, Counting.
Companies
Asked at: Fastenal.