Total Appeal of A String - Problem

The appeal of a string is defined as the number of distinct characters found in that string. Think of it as measuring how "diverse" a string is in terms of its character variety.

For example:

  • The appeal of "abbca" is 3 because it contains 3 distinct characters: 'a', 'b', and 'c'
  • The appeal of "aaa" is 1 because it only contains the character 'a'
  • The appeal of "abc" is 3 because all characters are distinct

Your task is to find the total appeal of all possible substrings of a given string s. A substring is any contiguous sequence of characters within the original string.

This means you need to:

  1. Generate all possible substrings of the input string
  2. Calculate the appeal (distinct character count) for each substring
  3. Sum up all these appeal values

Goal: Return the sum of appeals of all substrings of the given string.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abbca"
โ€บ Output: 28
๐Ÿ’ก Note: The string 'abbca' has substrings like 'a'(1), 'ab'(2), 'abb'(2), 'abbc'(3), 'abbca'(3), 'b'(1), 'bb'(1), 'bbc'(2), 'bbca'(3), etc. When we sum the appeal (distinct character count) of all 15 substrings, we get 28.
example_2.py โ€” Single Character
$ Input: s = "abc"
โ€บ Output: 10
๐Ÿ’ก Note: Substrings: 'a'(1), 'ab'(2), 'abc'(3), 'b'(1), 'bc'(2), 'c'(1). Total appeal = 1+2+3+1+2+1 = 10.
example_3.py โ€” Repeated Characters
$ Input: s = "aaaa"
โ€บ Output: 10
๐Ÿ’ก Note: All substrings contain only the character 'a', so each has appeal 1. There are 10 total substrings: 4 of length 1, 3 of length 2, 2 of length 3, and 1 of length 4. Total = 4ร—1 + 3ร—1 + 2ร—1 + 1ร—1 = 10.

Visualization

Tap to expand
Character Contribution AnalysisExample: s = "aba"ai=0bi=1ai=2Step-by-Step Processing:Step 1: Process 'a' at index 0last['a'] = -1, contribution = 0-(-1) = 1current_appeal = 1, total_appeal = 1Substrings: "a"(appeal=1)Step 2: Process 'b' at index 1last['b'] = -1, contribution = 1-(-1) = 2current_appeal = 3, total_appeal = 4Substrings: "ab"(2), "b"(1)Step 3: Process 'a' at index 2last['a'] = 0, contribution = 2-0 = 2current_appeal = 5, total_appeal = 9Substrings: "aba"(2), "ba"(2), "a"(1)All Substrings and Appeals:"a" โ†’ appeal = 1"ab" โ†’ appeal = 2"aba" โ†’ appeal = 2"b" โ†’ appeal = 1"ba" โ†’ appeal = 2"a" โ†’ appeal = 1Total = 9๐ŸŽฏ Key: Track contributions, not substrings!
Understanding the Visualization
1
Initialize tracking variables
Set up total_appeal=0, current_appeal=0, and last_occurrence map
2
Process each character
For each position, calculate how many new substrings this character contributes to
3
Calculate contribution
contribution = current_index - last_occurrence_index
4
Update running totals
Add contribution to current_appeal, then add current_appeal to total
Key Takeaway
๐ŸŽฏ Key Insight: Instead of generating O(nยฒ) substrings, we calculate how each character contributes to all substrings ending at each position. This transforms the problem from O(nยณ) brute force to O(n) optimal solution by leveraging the mathematical relationship between character positions and their contributions.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, constant time operations for each character

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

O(k) where k is the number of distinct characters (at most 26 for lowercase letters)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters only
  • Time limit: The solution must run efficiently for large inputs
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen