C++ easy explanation
Assume 0-based indexing\n\nAPPROACH:\nwe will take the contribution of each character in the final answer.\n\n\nLet\'s take example: "LEETCODE"\n\nCONTRIBUTION 
**Assume 0-based indexing**\n\n**APPROACH**:\nwe will take the contribution of each character in the final answer.\n\n\nLet\'s take example: "LEETCODE"\n\n**CONTRIBUTION OF \'L\'(0th index) IN THE OVERALL ANSWER**\nif we take a closer look at the example than from o to n-1,there is only one \'L\' so all the substring from 0 to n-1 that includes \'L\' will contribute to the answer.\nstrings that contributed in answer = [L,LE,LEE,LEET,LEETC,LEETCO,LEETCOD,LEETCODE]\nso \'L\' contribution to the answer is 8,so ans = 8\n\n![image](https://assets.leetcode.com/users/images/5a520c4b-0e87-42c2-9cd8-c5c74d345298_1658580081.3556244.png)\n\n**CONTRIBUTION OF \'E\'(1st index) IN THE OVERALL ANSWER**\nas there is another \'E\' on 2nd index so if we take any substring that includes both the \'E\' \'s,the constribution of \'E\' will be 0,because we have to count all the unique characters.\nstrings that contributed in answer = [LE,E]\nso \'E\' contribution to the answer(1st index) is 2,so ans = 8+2 = 10\n![image](https://assets.leetcode.com/users/images/32c3041e-d705-454a-bc23-4ab7737a953c_1658580491.9722972.png)\n\n**CONTRIBUTION OF \'E\'(2nd index) IN THE OVERALL ANSWER**\nas we can\'t take the \'E\' that is on 1st index because in that case \'E\' at 2nd index will not contribute to the answer.\nand similarly we can\'t take substrings after 6th index as there is repeated \'E\' on 7th index.\nstrings that contributed in answer = [E,ET,ETC,ETCO,ETCOD]\nso \'E\' contribution to the answer(2nd index) is 5,so ans = 10+5 = 15\n![image](https://assets.leetcode.com/users/images/5ef94794-7a77-4ff7-9dab-fc07ecebb5a5_1658581706.7808452.png)\n\n\n**CONTRIBUTION OF \'T\'(3rd index) IN THE OVERALL ANSWER**\nas there is no \'T\' on either side of \'T\' which is at 3rd index so all the subsring that include that \'T\' will contribute to the answer.\nso total number of strings that will contribute to the answer = 4*5 = 20(combination of all the strings that are made using LEET ,taking all the strings that made using C,O,D,E + LEET combination)\nso ans = 15+20 = 35\n![image](https://assets.leetcode.com/users/images/be5a9da1-5fbd-43d0-8184-e2b68d16da4e_1658581158.4246523.png)\nand so on......\nat the end ans will be 92\n\n\n\nso we just need to find the previous and next occurance of that character(ith index; 0 <= i < n)\nso our answer for ith index will be leftLengthh*rightLength\n\n\n**CODE**\n```\nint uniqueLetterString(string s) {\n        int n = s.size();\n        map<char,int> left;\n        map<char,vector<int>> right;\n        for(int i = n-1 ; i >= 0 ; i--){\n            right[s[i]].push_back(i);\n        }\n        int ans = 0;\n        for(int i = 0 ; i < n ; i++){\n            right[s[i]].pop_back();\n            int l = ((left.find(s[i]) != left.end())?left[s[i]]:-1);\n            int r = ((right[s[i]].size() > 0)?right[s[i]].back():n);\n            int llen = max(1,i-l);\n            int rlen = max(1,r-i);\n            int len = rlen*llen;\n            ans += len;\n            left[s[i]] = i;\n        }\n        return ans;\n    }\n```
['C']