C++ solution
\n    int uniqueLetterString(string s) {\n        // if s = "LEETCODE"\n        // the second E adds one to the final result in the substring of "E", "ET", "ETC
```\n    int uniqueLetterString(string s) {\n        // if s = "LEETCODE"\n        // the second E adds one to the final result in the substring of "E", "ET", "ETC", "ETCO", "ETCOD"\n        // so for each character just counting how many 1s it contributes to the final result\n        int size = s.size();\n        // for each character we matain a vector of size two, vector[0] stores the second last appearance index of that character, and vector[1] is the last appearance of that character\n        // initially, they are all -1\n        vector<vector<int>> appear(26, {-1, -1});\n        int result = 0;\n        for (int i = 0; i < size; i++) {\n            int index = s[i] - \'A\';\n            // if we meet a character and we didn\'t meet it before, set the last appearance to i\n            if (appear[index][1] == -1) {\n                appear[index][1] = i;\n            } else {\n                // this character has appeared at least onece so we can count the number of the last appearance contributes to the result\n                result += (appear[index][1] - appear[index][0]) * (i - appear[index][1]);\n                // and update the appearance vector\n                appear[index][0] = appear[index][1];\n                appear[index][1] = i;\n            }\n        }\n        // we didn\'t count the contribution for each character\'s last contribution\n        // let\'s add them up here\n        for (auto& v : appear) {\n            if (v[1] == -1) {\n                continue;\n            }\n            result += (v[1] - v[0]) * (size - v[1]);\n        }\n        return result;\n    }\n```
[]