Prefix and Suffix java solution: O(n) time and O(n) space.
The idea is same as Lee215 solution, but tried to fit it in some known paradigm (Prefix and Suffix), Found the first occurence of a character and next occurence
The idea is same as Lee215 solution, but tried to fit it in some known paradigm (Prefix and Suffix), Found the first occurence of a character and next occurence of the character and calculated the no of substrings that would contain the current character between that range. \n\npublic int uniqueLetterString(String s) {\n        \n        int[] left = new int[s.length()];\n        \n        int[] right = new int[s.length()];\n        \n        int[] last = new int[26];\n        int res = 0;\n        for(int iter=0;iter<26;iter++){\n            last[iter] = -1;\n        }\n        for(int iter=0;iter<s.length();iter++){\n            left[iter] = last[s.charAt(iter)-\'A\'];\n            last[s.charAt(iter)-\'A\'] = iter;\n        }\n        for(int iter=0;iter<26;iter++){\n            last[iter] = s.length();\n        }\n        for(int iter=s.length()-1;iter>=0;iter--){\n            right[iter] = last[s.charAt(iter)-\'A\']; \n            last[s.charAt(iter)-\'A\'] = iter; \n        }\n        for(int iter=0;iter<left.length;iter++){\n            res = (res+((iter-left[iter])*(right[iter]-iter)));\n        }\n        return res;\n    }
['Suffix Array']