Total Appeal of A String - Problem
The appeal of a string is the number of distinct characters found in the string.
For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.
Given a string s, return the total appeal of all of its substrings.
A substring is a contiguous sequence of characters within a string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abbca"
›
Output:
28
💡 Note:
All substrings: "a"(1), "ab"(2), "abb"(2), "abbc"(3), "abbca"(3), "b"(1), "bb"(1), "bbc"(2), "bbca"(3), "b"(1), "bc"(2), "bca"(3), "c"(1), "ca"(2), "a"(1). Sum = 28
Example 2 — Single Character
$
Input:
s = "z"
›
Output:
1
💡 Note:
Only one substring "z" with appeal 1
Example 3 — All Same
$
Input:
s = "aaa"
›
Output:
6
💡 Note:
Substrings: "a"(1), "aa"(1), "aaa"(1), "a"(1), "aa"(1), "a"(1). Sum = 6
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string with characters to analyze
2
Find All Substrings
Consider every contiguous subsequence
3
Sum Appeals
Add up distinct character counts
Key Takeaway
🎯 Key Insight: Calculate character contributions mathematically instead of generating all substrings
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code