

【解题思路】

class Solution {
public int uniqueLetterString(String s) {
Map<Character, List<Integer>> map = new HashMap<Character, List<Integer>>();
char c;
int i;
int ans = 0;
for(i = 0; i < s.length(); i++)
{
c = s.charAt(i);
if(!map.containsKey(c))
{
map.put(c, new ArrayList<Integer>());
map.get(c).add(-1);
}
map.get(c).add(i);
}
for(Map.Entry<Character, List<Integer>> entry : map.entrySet())
{
List<Integer> arr = entry.getValue();
arr.add(s.length());
for (int j = 1; j < arr.size() - 1; j++) {
ans += (arr.get(j) - arr.get(j - 1)) * (arr.get(j + 1) - arr.get(j));
}
}
return ans;
}
}