import sys
from collections import Counter
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
# 第一步:统计所有字符的总出现次数
char_count = Counter(s)
total = 0
# 第二步:遍历每个字符,计算其右侧不同字符的组合数之和
for c in s:
# 先将当前字符的计数减1(表示右侧剩余的该字符数量)
char_count[c] -= 1
# 计算当前字符右侧,所有不同字符的 C(count, 2) 之和
current_sum = 0
for d, cnt in char_count.items():
if d != c and cnt >= 2:#abb,“b”的数量必须大于2,否则不成立
current_sum += cnt * (cnt - 1) // 2#推导出公式等差求和,abb是1(2个b),abbb是3(3个b),abbbb是6(4个b)....
total += current_sum
print(total)

京公网安备 11010502036488号