304 North Cardinal St.
Dorchester Center, MA 02124

# Total Appeal of A String LeetCode Solution

## Problem – Total Appeal of A String LeetCode Solution

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.

substring is a contiguous sequence of characters within a string.

Example 1:

``````Input: s = "abbca"
Output: 28
Explanation: The following are the substrings of "abbca":
- Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.
- Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.
- Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7.
- Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6.
- Substrings of length 5: "abbca" has an appeal of 3. The sum is 3.
The total sum is 5 + 7 + 7 + 6 + 3 = 28.
``````

Example 2:

``````Input: s = "code"
Output: 20
Explanation: The following are the substrings of "code":
- Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.
- Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6.
- Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6.
- Substrings of length 4: "code" has an appeal of 4. The sum is 4.
The total sum is 4 + 6 + 6 + 4 = 20.
``````

Constraints:

• `1 <= s.length <= 105`
• `s` consists of lowercase English letters.

### Total Appeal of A String LeetCode Solution in Java

``````    public long appealSum(String s) {
int last[] = new int;
long res = 0;
for (int i = 0; i < s.length(); ++i) {
last[s.charAt(i) - 'a'] = i + 1;
for (int j: last)
res += j;
}
return res;
}
``````

### Total Appeal of A String LeetCode Solution in C++

``````    long long appealSum(string s) {
vector<int> last(26);
long res = 0, n = s.size();
for (int i = 0; i < n; ++i) {
last[s[i] - 'a'] = i + 1;
for (int j: last)
res += j;
}
return res;
}
``````

### Total Appeal of A String LeetCode Solution in Python

``````    def appealSum(self, s):
last = {}
res = 0
for i,c in enumerate(s):
last[c] = i + 1
res += sum(last.values())
return res
``````
##### Total Appeal of A String LeetCode Solution Review:

In our experience, we suggest you solve this Total Appeal of A String LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Total Appeal of A String LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Total Appeal of A String LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions