Solution by Python, calculating each char contribution
Intuition\nTo calculate each char in string contruibute to final answer\nI learned it from(Chinese Speaking): https://www.youtube.com/watch?v=NngeskF1wsw&t=679s
# Intuition\nTo calculate each char in string contruibute to final answer\nI learned it from(Chinese Speaking): https://www.youtube.com/watch?v=NngeskF1wsw&t=679s\n# Approach\nsuppose we have many A in string, such that:\n\n```\nB A B B A B A\n  i     j   k\n\n```\nQ: Suppose 3 As have position of i, j, k, how we gonna calculate 2nd A\'s contrubution to final answer?\n\nA: any substring containing jth, but not containing ith and kth would contribute 1. In the other word, any substring in string[i + 1: k] containing jth. \nFurthermore, from i + 1 to j, we have j - i choices, from j to k - 1, we have k - j choice. By simple combinatorics, we have (j - i)(k - j) chooices of substring sttifying above.\n\n\nQ: We now know how to deal with middle A, but what about first A and final A?\n\nA: We make faked A in the begining and end of String, such that\n\n```\n(A) B A B B A B A (A)\n-1    i     j   k len(s)\n\n```\nSo the A in ith and kth would also be "middle A", and do the calcualting as above\n\n\n\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n# Code\n```\nclass Solution:\n    def uniqueLetterString(self, s):\n        li = []\n        for i in range(26):\n            tem = []\n            tem.append(-1) # "faked A" in the begining\n            li.append(tem)\n        for i in range(len(s)):\n            # record position of certain char in string\n            li[ord(s[i]) - 65].append(i)\n        ans = 0\n        for each in li:\n            each.append(len(s)) # "faked A" in the end\n            for i in range(1, len(each) - 1): # We don\'t deal with "faked A"\n                ans += ((each[i] - each[i - 1]) * (each[i + 1] - each[i]))\n        return ans\n```
['Python3']