Given a string s
, the task is to calculate the sum of the beauty of all its substrings. The beauty of a substring is defined as the difference between the highest frequency and the lowest frequency of characters present in the substring (excluding characters not present in that substring).
s
consisting of only lowercase English letters.s
.Constraints:
s.length
≤ 500s
consists of lowercase English letters only.Note: For every substring, only consider characters that appear at least once when computing the minimum frequency.
The first idea that comes to mind is to generate all possible substrings of s
and, for each substring, count the frequency of each character. Then, compute the beauty by finding the maximum and minimum frequency (ignoring zeros). Finally, sum up the beauties for all substrings.
However, this brute-force approach can be inefficient, especially since the number of substrings is O(n2) and counting frequencies for each can be O(n), leading to O(n3) time complexity.
To optimize, we can try to reuse frequency data as we expand substrings, reducing redundant computations. We also note that the constraints (string length up to 500) allow for O(n2) solutions, so improving from O(n3) to O(n2) is feasible and desirable.
Let's break down the optimized solution step by step:
i
(from 0 to n-1), expand the substring to every possible ending index j
(from i
to n-1).j
, increment its count in the array.max_freq - min_freq
.This approach ensures that we only need O(1) time to update the frequency array for each substring extension, and O(26) time to compute the beauty, resulting in an overall O(n2) time complexity.
Let's use the input s = "aabcb"
.
This demonstrates how we expand substrings, update frequencies, and compute beauties efficiently.
The optimized approach is efficient for the given constraints and avoids redundant computations.
In this problem, we efficiently calculated the sum of the beauty of all substrings by:
class Solution:
def beautySum(self, s: str) -> int:
n = len(s)
total = 0
for i in range(n):
freq = [0] * 26
for j in range(i, n):
idx = ord(s[j]) - ord('a')
freq[idx] += 1
max_freq = 0
min_freq = float('inf')
for f in freq:
if f > 0:
max_freq = max(max_freq, f)
min_freq = min(min_freq, f)
total += max_freq - min_freq
return total
class Solution {
public:
int beautySum(string s) {
int n = s.length();
int total = 0;
for (int i = 0; i < n; ++i) {
int freq[26] = {0};
for (int j = i; j < n; ++j) {
freq[s[j] - 'a']++;
int max_freq = 0, min_freq = INT_MAX;
for (int k = 0; k < 26; ++k) {
if (freq[k] > 0) {
max_freq = max(max_freq, freq[k]);
min_freq = min(min_freq, freq[k]);
}
}
total += max_freq - min_freq;
}
}
return total;
}
};
class Solution {
public int beautySum(String s) {
int n = s.length();
int total = 0;
for (int i = 0; i < n; i++) {
int[] freq = new int[26];
for (int j = i; j < n; j++) {
freq[s.charAt(j) - 'a']++;
int maxFreq = 0, minFreq = Integer.MAX_VALUE;
for (int f : freq) {
if (f > 0) {
maxFreq = Math.max(maxFreq, f);
minFreq = Math.min(minFreq, f);
}
}
total += maxFreq - minFreq;
}
}
return total;
}
}
var beautySum = function(s) {
let n = s.length;
let total = 0;
for (let i = 0; i < n; i++) {
let freq = new Array(26).fill(0);
for (let j = i; j < n; j++) {
freq[s.charCodeAt(j) - 97]++;
let maxFreq = 0, minFreq = Number.MAX_VALUE;
for (let f of freq) {
if (f > 0) {
maxFreq = Math.max(maxFreq, f);
minFreq = Math.min(minFreq, f);
}
}
total += maxFreq - minFreq;
}
}
return total;
};