Intuitive, Elegant O(n) Python solution with explanation
\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        # previous occurrence is by default -1 (outside the string)\n        prev_occurrenc
```\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        # previous occurrence is by default -1 (outside the string)\n        prev_occurrence = {c:-1 for c in ascii_uppercase}\n\t\t# the entry for each char will be a stack storing the list of occurrences of the char in descending order, starting from len(s)\n        next_occurrence = {c:[len(s)] for c in ascii_uppercase}\n        s_reversed = reversed(s)\n        ans = 0\n\t\t# build next_occurrences stack for each char\n        for idx, char in enumerate(s_reversed):\n            next_occurrence[char].append(len(s) - idx - 1)\n        \n        for idx, char in enumerate(s):\n\t\t    \'\'\'\n            We want to find the first occurrence of char at an index greater than idx. \n\t\t\tAll other occurrences of char will need occurrences greater than idx, so we can simply pop those from the stack since we will never use them again.\n\t\t\t\'\'\'\n            while next_occurrence[char][-1] <= idx:\n                next_occurrence[char].pop()\n            \'\'\'\n\t\t\tAssume string is AXAXXXAXXXX, idx = 2. \n\t\t\tHere, we want to find only the number of substrings where char at idx 2 is unique.\n\t\t\tTotal substrings where second A is unique = total number of substrings between the previous and next A. \t\n\t\t\t= (number of ways of selecting a start idx between 0 and 2) * (number of ways of selecting an end idx between 2 and 6)\n\t\t\t= 2*4 = 8\n\t\t\t\'\'\'\n            ans+=(idx - prev_occurrence[char])*(next_occurrence[char][-1] - idx)            \n            \n            prev_occurrence[char] = idx\n        \n        \n        return ans\n\t```
[]