Java || O(n) Time || commented with 3 test cases
In this aproach I use a hashmap to store each characters index in the string. Then iterate through the hashmap calculating the left and right of each occurance 
In this aproach I use a hashmap to store each characters index in the string. Then iterate through the hashmap calculating the left and right of each occurance of that character using the trick in the commented test cases. \n```\nclass Solution {\n    \n    /*\n    "ABC"\n    A = 1*3 (_A_B_C_) there is 1 space to the left of A and 3 to the right\n    B = 2*2 (_A_B_C_) 2 to the left and right of B\n    C = 3*1 (_A_B_C_) 3 to the left and 1 to the right of C\n    ABC = 10, the total of all substrings\n    */\n    \n    /*\n    "ABA"\n    A = 1*2 + 2*1 (_A_B_A_) here we have 2 substrings with A being unique (_A_B_) and (_B_A_)\n    B = 2*2\n    ABA = 8\n    */\n    \n    /*\n    "LEETCODE"\n    L = 1*8\n    E = 2*1 + 1*5 + 5*1 (_L_E_) (_E_T_C_O_D_) (_T_C_O_D_E_)\n    T = 4*5\n    C = 5*4\n    O = 6*3\n    D = 7*2\n    LEETCODE = 8 + 12 + 20 + 20 + 18 + 14 = 92\n    */\n    public int uniqueLetterString(String s) {\n        Map<Character, List<Integer>> charIndices = new HashMap<>();\n        for (int i = 0; i < s.length(); i++) {\n            char c = s.charAt(i);\n            charIndices.putIfAbsent(c, new ArrayList());\n            charIndices.get(c).add(i);\n        }\n        int count = 0;\n        for (List<Integer> charIdx: charIndices.values()) {\n            for (int i = 0; i < charIdx.size(); i++) {\n                \n                // first occurance of each character \n                // [ A A B A A ], lets say that we are on B\n                // if we want the count of the left side it would be \n                // 2 - 0 = 2 because char B is at index 2 and i == 0 for first occuance in list\n                // but there are 3 spaces, so we add one to index 2 for this case to get 3\n                // otherwise we subtract current occurance index with the previous occurance index\n                // [A A B A A B A A], 5 - 2 = 3, there are 3 spaces on left side of second occurance\n                int left = i == 0 ? charIdx.get(i) + 1 : charIdx.get(i) - charIdx.get(i-1);\n                // same thing for the last occurance when getting the right sum\n                int right = i == charIdx.size()-1 ? s.length()-charIdx.get(i) : charIdx.get(i+1) - charIdx.get(i);\n                count += left*right;\n            }\n        }\n        return count;\n    }\n}\n```
['Java']