[Java] O(n) Greedy with explaination.
Main intuition behind the logic is to find total contribution of current character at index i and accumulate contribution to the result.\nContribution of every 
Main intuition behind the logic is to find total contribution of current character at index i and accumulate contribution to the result.\nContribution of every character at index i would be,\nleft side possibilities = x\nright side possibilities = y\n**Total possible substrings = x * y**\n\n![image](https://assets.leetcode.com/users/images/9f5627b4-f370-424c-9288-ed410fbec71c_1666280357.3397422.png)\n\n```\nclass Solution {\n    public int uniqueLetterString(String s) {\n        int n = s.length();\n        // To maintain rightmost occurence\n        int[] right = new int[n];\n        // To maintain leftmost occurence\n        int[] left = new int[26];\n        Arrays.fill(left , n);\n        Arrays.fill(right , n);\n        int ans = 0 ;\n        for(int i = n - 1; i >= 0 ; i--){\n            int curr = s.charAt(i) - \'A\';\n            // Update rightmost occurence\n            right[i] = left[curr];\n            left[curr] = i;\n        }\n        Arrays.fill(left , -1);\n        for(int i = 0 ; i < n ; i++){\n            int curr = s.charAt(i) - \'A\';\n            // a|xxxxx|a|xxxxx|a\n            //  |--x--| |--y--|\n            // Total contribution would be : x * y\n            int sum = (right[i] - i) *  (i - left[curr]);\n            ans += sum;\n            left[curr] = i;\n        }\n        return ans;\n    }\n}\n```
['Java']