Ctrl K

Total Frequency Spread

Medium
The **spread** of a string is the count of its most frequent letter minus the count of its least frequent letter among those present. For example, "hello" has spread 2 − 1 = 1 (l twice, every other letter once). Given a string text, return the sum of spreads over all of its substrings (contiguous pieces). Equal substrings at different positions are counted separately.
Example 1
text="abb" → 1

Only the substring "abb" has a non-zero spread: b appears twice, a once, so 2 − 1 = 1. "bb" has a single letter, spread 0.

Example 2
text="xyz" → 0

In every substring each letter occurs exactly once, so the answer is 0.

Example 3
text="aaba" → 4
Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Hint for a Digit Guess MediumCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumDomain Visit Rollup Medium
loading Python…
Input
text="abb"
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.