All problems
0896HardHash TableStringDynamic Programming

Total Variety Across Every Window

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2262Total Appeal of A String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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