C++ Solution 8ms by "Contribution"
Every letter in the string S eventually "contributes" to the final answer.\nConsdier the example S = "ABC".\nA will contribute 3,  respectively in substring "A"
Every letter in the string S eventually "contributes" to the final answer.\nConsdier the example S = "ABC".\nA will contribute 3,  respectively in substring "A", "AB" and "ABC".\nB will contribute 4,  respectively in substring "B", "AB", "BC" and "ABC".\nC will contribute 3,  respectively in substring "C", "BC" and "ABC".\nThe final answer is equal to the sum of "C[i] = contribution of i-th letter".\nClaim:   C[i] = L[i] * R[i], where L[i] indicates the number of letters between s[i] and the last occurence (assuming that we go through the string from left to right) of the letter \'s[i]\'; R[i] is defined similarily by assuming that we go through the string from right to left.\n\n```\nclass Solution {\npublic:\n\tconst int M = 1000000007;\n    int uniqueLetterString(string s) {\n\t\tint n = s.length();\n\t\tvector<long long> L(n, -1), R(n, -1);\n\t\tvector<long long> lastL(26, -1), lastR(26, n);\n\t\tfor (int i = 0; i < n; i++){\n\t\t\tL[i] = i - lastL[s[i] - \'A\'];\n\t\t\tlastL[s[i] - \'A\'] = i;\n\t\t}     \n\t\tfor (int i = n-1; i >= 0; i--){\n\t\t\tR[i] = lastR[s[i] - \'A\'] - i;\n\t\t\tlastR[s[i] - \'A\'] = i;\n\t\t}   \n\t\tint ans = 0; \n\n\t\tfor (int i = 0; i < n; i++){\n\t\t\tans = (ans + (L[i] * R[i]) % M) % M;\n\t\t}\n\t\treturn ans;\n    }\n};\n```\n
[]