Total Appeal of A String
Real-world version: Total Variety Across Every WindowThe 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.
def appeal_sum(s: str) -> int:public long appealSum(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Word BreakReal-world version: Label Splits Into CodesMedium
- Minimum Substring Partition of Equal Character FrequencyReal-world version: Fewest Even Tile CartonsMedium
- Longest String ChainReal-world version: Longest Chain of Grown LabelsMedium
- Number of Matching SubsequencesReal-world version: Labels That Read Through the TapeMedium