Java O(n) time, O(1) space, character contribution with comment
\npublic int uniqueLetterString(String s) {\n        int res = 0;\n        // last two indexes\n        int[][] index = new int[26][2];\n        \n        for(i
```    \npublic int uniqueLetterString(String s) {\n        int res = 0;\n        // last two indexes\n        int[][] index = new int[26][2];\n        \n        for(int i = 0; i < 26; i++){\n            index[i] = new int[]{-1, -1};\n        }\n        \n        int n = s.length();\n        for(int i = 0; i < n; i++){\n            int[] prevIndex = index[s.charAt(i) - \'A\'];\n            // for every character we encounter\n            // if we know its last two occurrence\n            // e.g. A1 **** A2 **** A3\n            // we know substrings between [A1 + 1, A3 - 1] will have at most one A\n            // to include A2 in the substring\n            // it has to start between [A1 + 1, A2] and end between [A2, A3 - 1]\n            // the total number of those substrings is (A2 - A1) * (A3 - A2)\n            // which equals A2\'s contribution to the result\n            res += (prevIndex[1] - prevIndex[0]) * (i - prevIndex[1]);   \n            \n            prevIndex[0] = prevIndex[1];\n            prevIndex[1] = i;\n        }\n        \n        // add character contribution for its last occurrence\n        // e.g. A1 **** A2 **** n\n        for(int[] prevIndex: index){\n            res += (prevIndex[1] - prevIndex[0]) * (n - prevIndex[1]);\n        }\n        \n        return res;\n    }\n\t```
['Java']