Solution
C++ []\nclass Solution {\npublic:\n    int uniqueLetterString(string s) {\n        int prev_count[26] = {}, prev_position[26];\n        for (size_t i = 0; i < 2
```C++ []\nclass Solution {\npublic:\n    int uniqueLetterString(string s) {\n        int prev_count[26] = {}, prev_position[26];\n        for (size_t i = 0; i < 26; ++i) prev_position[i] = -1;\n        int result = 0;\n        for (int i = 0, count = 0, n = static_cast<int>(s.size()); i < n; ++i)\n        {\n            int chr_idx = s[i] - \'A\';\n            int current = i - std::exchange(prev_position[chr_idx], i);\n            count += current - std::exchange(prev_count[chr_idx], current);\n            result += count;\n        }\n        return result;\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def uniqueLetterString(self, S):\n        index = {c: [-1, -1] for c in ascii_uppercase}\n        res = 0\n        for i, c in enumerate(S):\n            k, j = index[c]\n            res += (i - j) * (j - k)\n            index[c] = [j, i]\n        for c in index:\n            k, j = index[c]\n            res += (len(S) - j) * (j - k)\n        return res % (10**9 + 7)\n```\n\n```Java []\nclass Solution {\n    public int uniqueLetterString(String s) {\n        int n = s.length();\n        int[] right = new int[n];\n        int[] left = new int[26];\n        Arrays.fill(left , n);\n        Arrays.fill(right , n);\n        int ans = 0 ;\n        for(int i = n - 1; i >= 0 ; i--){\n            int curr = s.charAt(i) - \'A\';\n            right[i] = left[curr];\n            left[curr] = i;\n        }\n        Arrays.fill(left , -1);\n        for(int i = 0 ; i < n ; i++){\n            int curr = s.charAt(i) - \'A\';\n            int sum = (right[i] - i) *  (i - left[curr]);\n            ans += sum;\n            left[curr] = i;\n        }\n        return ans;\n    }\n}\n```\n
['C++', 'Java', 'Python3']