Show problems as
Classic problem #2262HardHash TableStringDynamic Programming

Total Appeal of A String

Real-world version: Total Variety Across Every Window
Tracked in this browser only
Read Total Appeal of A String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A reel of labels reads s, a string of lowercase letters. The variety of a contiguous stretch of the reel is how many distinct letters it holds.

Return the total variety over every non-empty contiguous stretch of the reel.

Examples

Example 1

Input
s = "mississippi"
Output
148

Every one of the sixty-six stretches contributes its own count of distinct letters, and those counts add up to this total.

Example 2

Input
s = "ab"
Output
4

The stretches are "a" with variety 1, "b" with variety 1, and "ab" with variety 2, giving 4.

Example 3

Input
s = "aa"
Output
3

Each single label has variety 1 and the whole reel still has variety 1, since the repeat adds no new letter, giving 3.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def appeal_sum(s: str) -> int:
Java
public long appealSum(String s)