[DP, Math] JS Solution
\n/*\nGreat explanation from `lee215`:\n\nThink about string "XAXAXXAX" and focus on making the second "A" a unique character.\nWe can take "XA(XAXX)AX" and bet
```\n/*\nGreat explanation from `lee215`:\n\nThink about string "XAXAXXAX" and focus on making the second "A" a unique character.\nWe can take "XA(XAXX)AX" and between "()" is our substring.\nWe can see here, to make the second "A" counted as a uniq character, we need to:\n\ninsert "(" somewhere between the first and second A\ninsert ")" somewhere between the second and third A\nFor step 1 we have "A(XA" and "AX(A", 2 possibility.\nFor step 2 we have "A)XXA", "AX)XA" and "AXX)A", 3 possibilities.\n\nSo there are in total 2 * 3 = 6 ways to make the second A a unique character in a substring.\nIn other words, there are only 6 substring, in which this A contribute 1 point as unique string.\n\nInstead of counting all unique characters and struggling with all possible substrings,\nwe can count for every char in S, how many ways it can be found as a unique char.\nWe count and sum, and it will be our answer.\n*/\n\nvar uniqueLetterString = function(s) {\n    if (s === "") {\n        return 0;\n    }\n    // initialise to -1 because array is 0-indexed\n    let lastL = new Array(26).fill(-1);\n    let lastR = new Array(26).fill(s.length);\n    let contribute = new Array(s.length).fill(0);\n    for (let i = 0; i < s.length; i++) {\n        let idx = s.charCodeAt(i) - 65;\n        let lastOccur = lastL[idx];\n        // we get the number of chars between current character and\n        // last occurrence of current character (assuming we traverse from left) \n        // + 1 (current character)\n        contribute[i] = i - lastOccur;\n        lastL[idx] = i;\n    }\n    for (let i = s.length-1; i >= 0; i--) {\n        let idx = s.charCodeAt(i) - 65;\n        let lastOccur = lastR[idx];\n        // we get the number of chars between current character and\n        // last occurrence of current character (assuming we traverse from right) \n        // + 1 (current character)\n        contribute[i] *= lastOccur - i;\n        lastR[idx] = i;\n    }\n    return contribute.reduce((acc,cur) => acc + cur);\n    // T.C: O(N)\n    // S.C: O(N)\n};\n```
['JavaScript']