The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.
"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 <= 500s consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the sum of beauty of all substrings, we'll look at every possible substring of the given string. For each substring, we'll calculate its beauty, and then add it to a running total.
Here's how the algorithm would work step-by-step:
def sum_of_beauty_of_all_substrings(input_string):
total_beauty = 0
for starting_index in range(len(input_string)):
for ending_index in range(starting_index, len(input_string)):
substring = input_string[starting_index:ending_index + 1]
# Calculate the beauty of the current substring
total_beauty += calculate_beauty(substring)
return total_beauty
def calculate_beauty(substring):
if not substring:
return 0
character_counts = {}
for char in substring:
character_counts[char] = character_counts.get(char, 0) + 1
min_frequency = float('inf')
max_frequency = float('-inf')
# Find the minimum and maximum character frequencies
for char in character_counts:
frequency = character_counts[char]
min_frequency = min(min_frequency, frequency)
max_frequency = max(max_frequency, frequency)
# Beauty is the difference between max and min frequencies
return max_frequency - min_frequencyThe efficient solution avoids checking every substring individually. Instead, it concentrates on figuring out the frequency of each character within each substring, and cleverly calculates the 'beauty' score based on these frequencies without repetitive recalculations.
Here's how the algorithm would work step-by-step:
def sum_of_beauty_of_all_substrings(input_string):
total_beauty_score = 0
string_length = len(input_string)
for left in range(string_length):
character_counts = {}
for right in range(left, string_length):
current_character = input_string[right]
if current_character in character_counts:
character_counts[current_character] += 1
else:
character_counts[current_character] = 1
# Find the maximum and minimum frequency of characters in substring
max_frequency = 0
min_frequency = string_length
for char in character_counts:
current_frequency = character_counts[char]
max_frequency = max(max_frequency, current_frequency)
min_frequency = min(min_frequency, current_frequency)
#Beauty score is the difference between the most and least frequent character
substring_beauty = max_frequency - min_frequency
total_beauty_score += substring_beauty
return total_beauty_score| Case | How to Handle |
|---|---|
| Empty string input | Return 0 immediately as there are no substrings to process. |
| String with a single character | Return 0 since the min and max frequency will be the same for the only character in the substring. |
| String with all identical characters (e.g., 'aaaa') | The beauty of each substring will be 0, as the min and max frequencies will be equal. |
| String with highly skewed character distribution (e.g., 'aaaaabc') | The algorithm should correctly calculate min/max frequencies based on the actual character counts. |
| Very long string input to test performance | Ensure the time complexity is O(n^2) where n is string length and avoid unnecessary computations. |
| String with a wide range of Unicode characters | The frequency count array needs to be large enough to accommodate all possible characters, or a hash map can be used. |
| String contains only two distinct characters repeated many times (e.g., 'ababab') | Test correct min/max frequency calculations for all possible substrings with only two characters. |
| String where one character appears only once, and all other characters appear many times. | The minimum frequency will always be 1, affecting the beauty calculation. |