Skip to main content

1781. Sum of Beauty of All Substrings

Problem Description​

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.

Examples​

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 English letters.

Solution for 1781. Sum of Beauty of All Substrings Problem​

Approach​

The goal of the beautySum problem is to find the sum of beauty values for all possible substrings of a given string s. The beauty value of a substring is defined as the difference between the maximum frequency and the minimum frequency of characters within that substring.

Steps to Solve the Problem​

  1. Initialization:

    • Initialize a variable ans to store the sum of beauty values.
  2. Iterate through the string:

    • Use a nested loop where the outer loop (i) iterates through each character in the string and the inner loop (j) iterates from the current character to the end of the string, effectively generating all substrings starting from each character.
  3. Track character frequencies:

    • Use a dictionary (or hashmap) to keep track of the frequency of each character in the current substring.
    • Update the dictionary as you extend the substring in the inner loop.
  4. Calculate the beauty value:

    • For each substring, calculate its beauty value by finding the maximum and minimum frequencies of characters in the substring.
    • Update the sum of beauty values (ans) with the beauty value of the current substring.
  5. Return the result:

    • After processing all substrings, return the total sum of beauty values.

Implementation​

Live Editor
function Solution(arr) {
     function beauty(mp) {
    let mini = Infinity;
    let maxi = -Infinity;

    for (let key in mp) {
        maxi = Math.max(maxi, mp[key]);
        mini = Math.min(mini, mp[key]);
    }

    return maxi - mini;
}

function beautySum(s) {
    let ans = 0;
    for (let i = 0; i < s.length; i++) {
        let mp = {};

        for (let j = i; j < s.length; j++) {
            mp[s[j]] = (mp[s[j]] || 0) + 1;
            ans += beauty(mp);
        }
    }

    return ans;
}
  const input = "aabcb"
  const output = beautySum(input)
  return (
    <div>
      <p>
        <b>Input: </b>
        {JSON.stringify(input)}
      </p>
      <p>
        <b>Output:</b> {output.toString()}
      </p>
    </div>
  );
}
Result
Loading...

Complexity Analysis​

  • Time Complexity: O(N2)O(N^2)
  • Space Complexity: O(N) O(N)

Code in Different Languages​

Written by @hiteshgahanolia
  function beauty(mp) {
let mini = Infinity;
let maxi = -Infinity;

for (let key in mp) {
maxi = Math.max(maxi, mp[key]);
mini = Math.min(mini, mp[key]);
}

return maxi - mini;
}

function beautySum(s) {
let ans = 0;
for (let i = 0; i < s.length; i++) {
let mp = {};

for (let j = i; j < s.length; j++) {
mp[s[j]] = (mp[s[j]] || 0) + 1;
ans += this.beauty(mp);
}
}

return ans;
}

References​