Java Solution. DP.
Explaination:\n\nWe can transform this question to:\n           Count the sum of countUniqueChars(s) for all valid substrings.\n\t\t\t\t\t\t\t\t\t\t\t\t\t||\n\t
Explaination:\n\nWe can transform this question to:\n           Count the sum of countUniqueChars(s) for all valid substrings.\n\t\t\t\t\t\t\t\t\t\t\t\t\t||\n\t       For each char, how many substring it can form while this char is unique in this String.\n\nAlgorithm:\n\n1. For each char, find the closest same char at it left and right side, and record its index.\n2. For each char,the num of valid substring while this char is unique in this String will be (numOfLeft * numOfRight)\n\tYXXYXXY\n\tFor the second Y, the range for it is XXYXX. So the sum is 3*3 = 9. (need to add it self 2+1 = 3)\n\t\nTC: O(3n) - O(n) if the operations of map is always O(1)\nSC: O(3n) - O(n). 2 arrays and 1 map.\n\t\n```\nclass Solution {\n    public int uniqueLetterString(String s) {\n        if (s == null || s.length() == 0) return 0;\n\t\t// setup dp array to record closest same char idx\n        int[] left = new int[s.length()];\n        int[] right = new int[s.length()];\n        \n        HashMap<Character, Integer> map = new HashMap<>();\n        for (int i=0;i<s.length();i++) {\n            char c = s.charAt(i);\n            map.putIfAbsent(c, -1);\n            left[i] = map.get(c);\n            map.put(c, i);\n        }\n        \n        map.clear();\n        \n        for (int i=s.length()-1;i>=0;i--) {\n            char c = s.charAt(i);\n            map.putIfAbsent(c, s.length());\n            right[i] = map.get(c);\n            map.put(c, i);\n        }\n        \n        int res = 0;\n        for (int i=0;i<s.length();i++) {\n\t\t\t// for each char, find its left / right border and caculate the number of chars\n            int numOfLeft = i - left[i];\n            int numOfRight = right[i] - i;\n            res += (numOfLeft * numOfRight);\n        }\n        return res;\n    }\n}\n```
[]