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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	s consists of lowercase English letters.


class Solution:
    def appealSum(self, s: str) -> int:
        res, cur, prev = 0, 0, defaultdict(lambda: -1)
        for i, ch in enumerate(s):
            cur += i - prev[ch]
            prev[ch] = i
            res += cur
        return res  
class Solution {
    public long appealSum(String s) {
        long res = 0;
        char[] cs = s.toCharArray();
        int n = cs.length;
        int[] pos = new int[26];
        Arrays.fill(pos, -1);
        for (int i = 0; i < n; ++i) {
            int j = cs[i] - 'a', prev = pos[j]; 
            res += (i - prev) * (long) (n - i);
            pos[j] = i;
        }
        return res;
    }
}
class Solution {
public:
	long long appealSum(string s) {
		int n = s.size();
		long long ans = 0;
		for(char ch='a';ch<='z';ch++)       // we are finding the number of substrings containing at least 1 occurence of ch
		{
			int prev = 0;                   // prev will store the previous index of the charcter ch
			for(int i=0;i<n;i++)
			{
				if(s[i] == ch) prev = i+1;  // if the current character is equal to ch , then the no. of substring
											// ending at i and having at least one occurence of ch will be i+1 .

				ans+=prev;                  // else the no. of substrings ending at i and having at least
											// one occurence of ch will be the equal to, the previous index of ch.
			}
		}
		return ans;                        // TC - O(n*26) , SC - O(1)
	}
};
var appealSum = function(s) {
  let ans = 0, n = s.length;
  let lastIndex = Array(26).fill(-1);
  for (let i = 0; i < n; i++) {
    let charcode = s.charCodeAt(i) - 97;
    let lastIdx = lastIndex[charcode];
    ans += (n - i) * (i - lastIdx);
    lastIndex[charcode] = i;
  }  
  return ans;
};
Total Appeal of A String