A Python Solution
Basic idea: From the beginning of the string s, we iterate over string and count how many times of a character s[i] contribute to the unique counts.\n\n1) A sim
Basic idea: From the beginning of the string s, we iterate over string and count how many times of a character s[i] contribute to the unique counts.\n\n1) A simple case.\nFor s[i], if it is never shown up in s[0], s[1], s[2], ... s[i-1], then it will counted *i+1* times when we consider the substring ended at s[i]. If s[i+1] != s[i], s[i] will be counted another *i+1* times for substring ended *i+1*th location. \ns[i] will not longer contribute to the unique count until we see the same character at s[j] (i < j). As a result, s[i] can only contribute to the unique counts **(i+1) * (j-i)** times.\n\n2) A complicate case.\nDifferent from the case 1, s[i] show up in s[0], s[1], s[2], ... s[i-1] at least once. Assume, the last appearance of s[i] is at *k*th location (k < i) in s.\nThen for the substring ended at *i*th location, s[i] is unique for all substring s[start:i+1] where (k < start <= i). In other words, it contributes *i - k* unique counts. Similarly to case 1, if s[i+1] != s[i], s[i] will be counted another *i-k* times for substring ended *i+1*th location. \ns[i] will not longer contribute to the unique count until we see the same character at s[j] (i < j). As a result, s[i] can only contribute to the unique counts **(i-k) * (j-i)** times.\n\nThe following code is the implementation of the above algorithm. *lastMap* is a hash table where the key is the character s[i]. Its corresponding value is a pair which contains the location of s[i] and how many times it contribute to the unique counts when we consider substring ended at s[i], s[i+1], ..., s[j-1].\n```\nfrom collections import defaultdict\n\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        # Initialization\n        lastMap = defaultdict(lambda: None)\n        n = len(s)\n        \n        # Base case for the first letter\n        uniqueCount = 0\n        lastMap[s[0]] = [0, 1]\n        \n        # Iterate over the other letters\n        for i in range(1, n):\n            if s[i] not in lastMap:\n\t\t\t\t# Case 1.\n                lastMap[s[i]] = [i, i+1]\n            else:\n\t\t\t\t# Case 2.\n                prevLoc, prevh = lastMap[s[i]]\n                w = i - prevLoc\n                uniqueCount += w * prevh\n                lastMap[s[i]] = [i, i-prevLoc]\n\t\t\n\t\t# Pop out all the letters in the map\n\t\t# The contribution of s[i] will only be counted when it is popped from the map.\n        for k in lastMap:\n            prevLoc, prevh = lastMap[k]\n            w = n - prevLoc\n            uniqueCount += w * prevh\n\n        return uniqueCount\n```
['Python']