Sum Frequency Imbalance Across Substrings

Quick Overview

Sum Frequency Imbalance Across Substrings. For a nonempty string, define its beauty as its largest character frequency minus its smallest positive character frequency. For lowercase string s, sum this quantity over every contiguous nonempty substring and return the total.

Sum Frequency Imbalance Across Substrings

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem For a nonempty string, define its beauty as its largest character frequency minus its smallest positive character frequency. For lowercase string `s`, sum this quantity over every contiguous nonempty substring and return the total. ```cpp class Solution { public: int beautySum(string s); }; ``` ### Examples **Example 1:** ```text s = "aaab" Output: 3 Explanation: Only aab and aaab have nonzero beauty, respectively 1 and 2. ``` **Example 2:** ```text s = "abab" Output: 2 Explanation: The substrings aba and bab each contribute 1; every other substring contributes 0. ``` ### Constraints - `1 <= s.length <= 500`. - Characters are lowercase English letters. ### Clarifying Questions The least frequent character must occur in that substring. Absent alphabet letters are excluded, as the source examples establish. ```hint Inspect the structure Which character frequencies are eligible for the minimum in one substring's beauty? ``` ### Reasoning checkpoints - Account for every eligible substring occurrence without omission or duplication. - Compute the minimum only among positive frequencies. ### Optional extensions How would prefix character counts compare with incrementally extending every left endpoint?

Overview: Sum Frequency Imbalance Across Substrings. For a nonempty string, define its beauty as its largest character frequency minus its smallest positive character frequency. For lowercase string s, sum this quantity over every contiguous nonempty substring and return the total.

Bloomberg logo
Bloomberg
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

For a nonempty string, define its beauty as its largest character frequency minus its smallest positive character frequency. For lowercase string s, sum this quantity over every contiguous nonempty substring and return the total.

class Solution {
public:
    int beautySum(string s);
};

Examples

Example 1:

s = "aaab"
Output: 3
Explanation: Only aab and aaab have nonzero beauty, respectively 1 and 2.

Example 2:

s = "abab"
Output: 2
Explanation: The substrings aba and bab each contribute 1; every other substring contributes 0.

Constraints

  • 1 <= s.length <= 500 .
  • Characters are lowercase English letters.

Clarifying Questions Guidance

The least frequent character must occur in that substring. Absent alphabet letters are excluded, as the source examples establish.

Reasoning checkpoints

  • Account for every eligible substring occurrence without omission or duplication.
  • Compute the minimum only among positive frequencies.

Optional extensions

How would prefix character counts compare with incrementally extending every left endpoint?