Easy Java Solution with explanation
Idea is simple consider you have left and right distance for current character from terminal positions, for any index , using that character max  substring comb
Idea is simple consider you have left and right distance for current character from terminal positions, for any index , using that character max  substring combinations are  left * right.\nLets take an exampe for unique character string **ABC** \n**A**\nLeft=1       As it is a initial position (current index +1), that is 0+1=1.\nRight=3   As terminal character distance for right is 3(size of the string-current index), that is 3-0=3.\nCase1 result1= Left * Right=3\n**B**\nLeft=2      Current index is 1, therefore left distance will be  current index +1, that is 1+1= 2.\nRight=2    Current index is 1 and size of the string is 3, therefore right distance will be size of the string-current index, that is 3-1=2 .\nCase2 result2= Left * Right=4\nSImilarly for **C**\nLeft=3     Current index is 2, therefore left distance will be  current index +1, that is 2+1= 3.\nRight=1  Current index is 2 and size of the string is 3, therefore right distance will be size of the string-current index, that is 3-2=1.\nCase3 result= Left * Right=3\nResult for above case will be addition of all three cases(result1+result2+result3), That is 3+4+3=10.\nFor unique Character String formula is simple, for each index where initial value of sum is 0 then,\nfor each index(index starts from 0 and size is String length) sum=sum+((index+1)*(size-index)).\n\nHope above concept is clear, If it is clear Now we can move to duplicate character Strings.\n\nNow for Duplicate String left and right will be calculated from current string to\n\t* for left it is previously occured same character \n\t* for right it is upcoming same character.\n**ABA**\n**A**\nLeft =1 there is no previous duplicate character, Therefore index+1, That is 0+1=1.\nRight=2 Here We will not consider terminal as there is next duplicate character at terminal, Therefore Index of Duplicate character which is next to current element - current index. That is 2-0=2.\nCase1 result1=1 * 2=2.\n**B**\nLeft=2      Current index is 1, therefore left distance will be  current index +1, that is 1+1= 2.\nRight=2    Current index is 1 and size of the string is 3, therefore right distance will be size of the string-current index, that is 3-1=2 .\nNote as B is unique character in the string so the formula will be same as Unique character formula, As discussed above.\nCase2 result2= Left * Right=4\n\n**A**\nLeft =2 there is one previous duplicate character at position 0, Therefore current index - previous index.that is \n2-0=2.\nRight=1 Here now there is no next duplicate , Therefore size(length of the String) - current index. That is 3-2=1.\nCase3 result3= Left * Right=2\nResult for above case will be addition of all three cases(result1+result2+result3), That is 2+4+2=8.\nWe can simply use HashMap to track all dublicate indexes and using below formula we can compute left and right:(Let i is current index , and track is HashMap(Character,List<Integer>) and n is the size of dublicate character List).\nLeft= i==0?track.get(i)+1:(track.get(i)-track.get(i-1));\nRight=i==n-1?size-track.get(i):(track.get(i+1)-track.get(i));\nBelow is the Implementation:\n\n```\nclass Solution {\n     public int uniqueLetterString(String s) {\n      int[] sum=new int[1];\n      sum[0]=0;\n      int size=s.length();  \n      HashMap<Character,List<Integer>>tracker=new HashMap<>();  \n       for(int i=0;i<size;i++){\n            if(tracker.containsKey(s.charAt(i))){\n             tracker.get(s.charAt(i)).add(i);\n            }else{\n                List<Integer>track=new ArrayList();\n                track.add(i);\n                tracker.put(s.charAt(i),track);\n            }\n       }\n      tracker.entrySet().forEach(entry->{\n          List<Integer> track=entry.getValue();\n          int n=track.size();\n          for(int i=0;i<n;i++){\n               int left= i==0?track.get(i)+1:(track.get(i)-track.get(i-1));\n               int right=i==n-1?size-track.get(i):(track.get(i+1)-track.get(i));\n              sum[0]=sum[0]+(left*right);\n          }\n      });  \n       \n        return sum[0];\n         \n     }\n}\n```
['Java']