Problem:
 Score : 500 points    Problem Statement You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.  You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j .  You can perform this operation at most once.  How many different strings can you obtain?      Constraints  1 \leq |A| \leq 200,000  A consists of lowercase English letters.         Input Input is given from Standard Input in the following format:  A       Output Print the number of different strings you can obtain by reversing any substring in A at most once.        Sample Input 1 aatt      Sample Output 1 5  You can obtain aatt (don't do anything), atat (reverse A[2..3] ), atta (reverse A[2..4] ), ttaa (reverse A[1..4] ) and taat (reverse A[1..3] ).       Sample Input 2 xxxxxxxxxx      Sample Output 2 1  Whatever substring you reverse, you'll always get xxxxxxxxxx .       Sample Input 3 abracadabra      Sample Output 3 44

Solution (Python):
from collections import defaultdict
A = input()
N = len(A)

d = defaultdict(list)
for i, a in enumerate(A):
    d[a].append(i)

pair = 0
for k, v in d.items():
    n = len(v)
    pair += n * (n - 1) // 2

ans = N * (N - 1) // 2
ans = ans - pair + 1
print(ans)

Solution (Python):
from collections import Counter
s = input()
l = len(s)
c = Counter(s)
ans = 0
for i in c.values():
  ans += i*(i-1)//2
print(l*(l-1)//2 - ans + 1)

Solution (Python):
import sys
from collections import defaultdict

bbn=1000000007
	





def main():
	s = input()
	di = defaultdict(lambda : 0)
	for c in s:
		di[c] += 1
	
	total_s=len(s)
	ret=1
	
	for c in s:
		ret += total_s-1
		ret -= di[c]-1
		total_s -= 1
		di[c] -= 1
		
	print(ret)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
A = input()
char = [0 for _ in range(26)]

for i in range(len(A)):
    char[ord(A[i])-97] += 1

ans = 1

for i in range(25):
    for j in range(i+1,26):
        ans += char[i]*char[j]

print(ans)

Solution (Python):
s=list(input())
al=list('abcdefghijklmnopqrstuvwxyz')
d=[0]*26
for i in s:
    d[al.index(i)]+=1
n=len(s)
ans=n*(n-1)//2+1
for i in d:
    ans-=i*(i-1)//2
print(ans)

Solution (Python):
S = input()
N = len(S)

alpha = [0] * 27
for s in S:
  alpha[ord(s) - ord("a")] += 1


answer = N * (N - 1) // 2 + 1
for a in alpha:
  answer -= (a - 1) * a // 2
print(answer)

Solution (Python):
import sys

from collections import Counter

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) 
rs = lambda: stdin.readline().rstrip()  

A = list(rs())
answer = 1
c = Counter(A)
for i in range(len(A)):
    answer += len(A) - i - c[A[i]]
    c[A[i]] -= 1

print(answer)

Solution (Python):
from collections import Counter
A=input()
C=Counter(A)

ans=len(A)*(len(A)-1)//2
for n in C.values():
    ans-=n*(n-1)//2
ans+=1
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
c = Counter(s)
cnt = 0
for key,value in c.items():
    if value >= 2:
        cnt += value*(value-1)//2
print(n*(n-1)//2 - cnt + 1)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    A=input()
    n=len(A)
    ans=n*(n+1)//2

    D=defaultdict(int)
    for a in A:
        D[a]+=1

    for c in D.values():
        ans-=c*(c+1)//2

    print(ans+1)
resolve()

Solution (Python):
A = input()

pre_ch = {}
pre_ch_cnt = []
for i, c in enumerate(A):
  if c in pre_ch:
    pre_ch_cnt.append(pre_ch_cnt[pre_ch[c]] + 1)
  else:
    pre_ch_cnt.append(0)
  pre_ch[c] = i

s_cnt = [1]
for i, c in enumerate(A):
  s_cnt.append(s_cnt[i])
  s_cnt[i + 1] += i - pre_ch_cnt[i]

print(s_cnt[len(A)])

Solution (Python):
from collections import Counter

A = input()

cnt = Counter(A)

ret = len(A) * (len(A) - 1) // 2 + 1
for v in cnt.values() :
  ret -= v * (v - 1) // 2

print(ret)

Solution (Python):
s=input()
l=len(s)
t="abcdefghijklmnopqrstuvwxyz"
d={}
for i in t:d[i]=0
for i in s:d[i]+=1
a=l*(l-1)//2
for i in t:a-=d[i]*(d[i]-1)//2
print(a+1)

Solution (Python):
from collections import Counter

A = list(input())
a = len(A)
A = Counter(A)
answer = a * (a - 1) // 2 + 1
for i in A.values():
    answer -= i * (i - 1) // 2

print(answer)

Solution (Python):
from collections import Counter

A = list(input())
a = len(A)
A = Counter(A)
cnt = 0
for i in A.values():
    if i != 1:
        cnt += i * (i - 1) // 2

print(a * (a - 1) // 2 - cnt + 1)

Solution (Python):
def examB():
    A = SI(); N = len(A)
    d = defaultdict(int)
    for s in A:
        d[s] +=1

    ans = N*(N-1)//2 +1
    for i in d.values():
        ans -= i*(i-1)//2
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Solution (Python):

import sys
sys.setrecursionlimit(200000)
import collections
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def fi(): return float(input())
def mfi(): return map(float, input().rstrip().split())
def lmfi(): return list(map(float, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None
def exit(*arg): print(*arg); sys.exit()


def reverse(s: str):
    return s[::-1]

def main():
    S = input().rstrip()
    
    
    
    
    
    
    
    
    
    
    
    
    d = collections.defaultdict(int)
    for x in S:
        d[x] += 1
    
    minum = 0
    for i in d:
        minum += d[i] * (d[i] - 1) // 2
    print(len(S) * (len(S) - 1) // 2 - minum + 1)
    return


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
d=defaultdict(int)
w=input()
s=len(w)
for i in range(s):
  d[w[i]]+=1
ans=0
for i in d:
  ans+=d[i]*(s-d[i])
  
print(ans//2+1)

Solution (Python):

A = input()

dic = {}
num = 0
ans = 0

for s in A:

    if s in dic:
        ans += num - dic[s]
        dic[s] += 1

    else:
        ans += num
        dic[s] = 1

    num += 1

print (ans+1)

Solution (Python):
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
S = list(input())[: -1]
table = [[] for _ in range(26)]
a = ord("a")
res = 1
for i in range(len(S)):
  table[ord(S[i]) - a].append(i)
for i in range(len(S)):
  x = bl(table[ord(S[i]) - a], i)
  res += len(S) - (len(table[ord(S[i]) - a]) - x) - i
  
print(res)

Solution (Python):
from collections import Counter
A = input()

n = len(A)
c = Counter(A)
ans = n * (n-1) // 2 + 1
for value in c.values():
    dec = value * (value-1) // 2
    ans -= dec

print(ans)

Solution (Python):
import collections
table = collections.defaultdict(int)
A = list(input())
N = len(A)

for x in A:
    table[x] += 1
ans = (N*(N-1) // 2) + 1
for x in table:
    ans -= (table[x] * (table[x] - 1)) // 2

print(ans)

Solution (Python):




a = input()
n = len(a)
chrs = set(a)
dic = {k:0 for k in chrs}

for i in range(n):
    dic[a[i]] += 1

ans = n*(n-1)//2+1

for k in dic:
    ans -= dic[k]*(dic[k]-1)//2

print(ans)

Solution (Python):
from collections import Counter
A=list(input())
N=len(A)
C=Counter(A).values()

ans=N*(N-1)//2+1

for c in C:
  ans-=c*(c-1)//2
  
print(ans)

Solution (Python):
from collections import Counter
a = input()
ct = Counter(a)
_sum = 0
for c in ct.values():
    _sum += c * (c - 1) / 2
print(int(len(a) * (len(a) - 1) / 2 - _sum + 1))

Solution (Python):
from collections import Counter
a=list(input())
ans=len(a)*(len(a)-1)//2
c=Counter(a)
for i in c:
    ans-=c[i]*(c[i]-1)//2
print(ans+1)

Solution (Python):
import collections

S=list(input())
ABC=[chr(i) for i in range(97,123)]
N=len(S)
_S=collections.Counter(S)
a=int(N*(N-1)/2-sum([_S[s]*(_S[s]-1)/2 for s in _S]) + 1)
print(a)

Solution (Python):
def main():
    from collections import Counter
    a=input()
    ans=(len(a)*(len(a)-1))//2+1
    a=Counter(a)
    for i in a.values():
        ans-=((i-1)*i)//2
    print(ans)
main()

Solution (Python):
from collections import Counter


def comb2(n):
    return (n * (n-1)) // 2


A = input()
N = len(A)
c = Counter(A)

print(1 + comb2(N) - sum([comb2(x) for x in c.values()]))

Solution (Python):
from collections import Counter
A = list(input())
N = len(A)
cA = Counter(A)
dpl = 0
for i in cA.values():
  dpl += i * (i - 1) // 2
print(N * (N - 1) // 2 - dpl + 1)

Solution (Python):
s=input()
from collections import Counter
c=Counter(s)
m=0
for k in c.values():
    m+=k*(k-1)//2
n=len(s)
print(n*(n-1)//2-m+1)

Solution (Python):
A=input()
ans=0
a=A.count("a")
b=A.count("b")
c=A.count("c")
d=A.count("d")
e=A.count("e")
f=A.count("f")
g=A.count("g")
h=A.count("h")
i=A.count("i")
j=A.count("j")
k=A.count("k")
l=A.count("l")
m=A.count("m")
n=A.count("n")
o=A.count("o")
p=A.count("p")
q=A.count("q")
r=A.count("r")
s=A.count("s")
t=A.count("t")
u=A.count("u")
v=A.count("v")
w=A.count("w")
x=A.count("x")
y=A.count("y")
z=A.count("z")
B=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
for aa in range(26):
  ans=ans+B[aa]*(len(A)-B[aa])
print(ans//2+1)

Solution (Python):
import os
import os
import sys
from collections import defaultdict

from scipy.misc import comb

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

S = sys.stdin.readline().rstrip()
N = len(S)


def test(S):
    s = {S}
    for l in range(len(S)):
        for r in range(l + 1, len(S)):
            s.add(S[:l] + S[l:r + 1][::-1] + S[r + 1:])
    return len(s)


ans = comb(len(S), 2, exact=True) + 1
counts = defaultdict(int)
for i, c in enumerate(S):
    
    ans -= counts[c]
    counts[c] += 1
print(ans)

Solution (Python):
from collections import defaultdict

s = input()
n = len(s)

d = defaultdict(int)

ans = 1
for left_total, c in enumerate(s, 0):
    ans += left_total - d[c]
    d[c] += 1
print(ans)

Solution (Python):
a=input();print((len(a)**2-sum(a.count(x)**2for x in set(a)))//2+1)

Solution (Python):
a = input()
n = len(a)
ans = n * (n - 1) // 2 + 1
d = {}
for x in a:
	d[x] = d.get(x, 0) + 1
print(ans - sum(y * (y - 1) // 2 for y in d.values()))

Solution (Python):
from collections import Counter
a = input()
n = len(a)
data = Counter(a)

ans = n*(n-1)//2 + 1
for i in data:
    ans -= (data[i]-1)*data[i]//2

print(ans)

Solution (Python):
A=input()
dp=[0]*26
r=0
for i,a in enumerate(A):
    c=ord(a)-97
    r+=i-dp[c]
    dp[c]+=1
print(r+1)

Solution (Python):
s = input()
AZ = 'abcdefghijklmnopqrstuvwxyz'
lens =len(s)
cnt = 0
for ch in AZ:
  a = s.count(ch)
  cnt += a*(a-1)//2
print(lens*(lens-1)//2 - cnt + 1)

Solution (Python):
s = input()

memo = {}
ans = 0

for i in range(len(s)):
    if s[i] not in memo:
        memo[s[i]] = 1
    else:
        memo[s[i]] += 1
    ans += (i+1) - memo[s[i]]

print(ans + 1)

Solution (Python):



def main():
    from collections import Counter
    from itertools import combinations

    a = Counter(input())
    ans = 1

    
    
    

    
    
    
    
    for i, j in combinations(a.values(), 2):
        ans += i * j

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s=list(input())
import collections
dics=collections.Counter(s)
num=len(s)
ans=num*(num-1)//2
for i in dics.values():
  ans-=(i*(i-1)//2)
print(ans+1)

Solution (Python):
import collections
a = list(str(input()))
n = len(a)
c = collections.Counter(a)
result = (n*(n-1))/2 + 1
for i in c.values():
    result -= (i*(i-1))/2
print(int(result))

Solution (Python):
s = input()
ls = len(s)
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet_list = [0 for i in range(26)]

ans = 1
for i, v in enumerate(s,1):
    a = alphabet.index(v)
    ans += (i - 1)  - alphabet_list[a]
    alphabet_list[a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
d=defaultdict(int)

A=input()
for i in A:
  d[i]+=1
  
li=list(d.values())
li2=[i*(i-1)//2 for i in li]

n=len(A)

print(n*(n-1)//2+1-sum(li2))

Solution (Python):
from collections import defaultdict
d=defaultdict(int)

A=input()
for i in A:
  d[i]+=1
  
li=list(d.values())
li2=[i*(i-1)//2 for i in li]

n=len(A)

print(n*(n-1)//2+1-sum(li2))

Solution (Python):
S=list(input())
A=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

L=[0 for i in range(26)]

for i in range(len(S)):
    for j in range(26):
        if S[i]==A[j]:
            L[j]+=1
            

ans=1
num=sum(L)
for i in range(26):
    ans+=L[i]*(num-L[i])
    num-=L[i]
print(ans)

Solution (Python):

a = list(input())
c = [0] * 26
alpha2num = lambda x: ord(x) - ord('a') 

for i in range(len(a)):
    c[alpha2num(a[i])] += 1

ans = 1 + (len(a)*(len(a)-1)//2)
for i in range(26):
    ans -= c[i] * (c[i] - 1) //2

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline

import collections

def main():
    A = input().strip()
    n = len(A)
    ans = 1 + n*(n-1)//2
    for k in collections.Counter(A).values():
        ans -= k*(k-1)//2
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
A = input()

count = [0]*26

ans = 1
count[ord(A[0])-97]+=1

for i in range(1,len(A)):
    ans += i-count[ord(A[i])-97]
    count[ord(A[i])-97]+=1

print(ans)

Solution (Python):
from operator import mul
from functools import reduce

def nCr(n, r):
    r = min(r, n-r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n-r, -1), 1)
    under = reduce(mul, range(1, r+1), 1)
    return over // under

A = input()
strs = set(list(A))

ans = len(A) * (len(A)-1) // 2
for s in strs:
    num = A.count(s)

    if num >= 2:
        ans -= nCr(num, 2)

print(ans+1)

Solution (Python):
from collections import Counter
import sys
sys.setrecursionlimit(5000000)

S = list(input())
L = len(S)
A = Counter(S)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

if L == 1:
    print(1)
else:
    ans = cmb(L, 2) + 1
    for v in A.values():
        if v == 1: continue
        ans -= cmb(v, 2)
    print(ans)

Solution (Python):
a=input()
n=len(a)
from collections import Counter
c=Counter()
dp=[0]*(n)
dp[0]=1
c[a[0]]=1
for i in range(1,n):
    dp[i]=dp[i-1]+i-c[a[i]]
    c[a[i]]+=1
print(dp[-1])

Solution (Python):
A = input()
N = len(A)
count = {}
ans = 1

for i in range(N):
  if A[i] in count.keys():
    count[A[i]] += 1
  else:
    count[A[i]] = 1

L = list(count.values())
M = len(L)
for i in range(M):
  for j in range(i+1,M):
    ans += L[i] * L[j]
    
print(ans)

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)

C = Counter(S)

ans = N*(N-1) // 2 + 1
for k, v in C.items():
    
    ans -= v*(v-1) // 2
print(ans)

Solution (Python):
A = input()
N = len(A)
count = {}
ans = 0

for i in range(N):
  if A[i] in count.keys():
    count[A[i]] += 1
  else:
    count[A[i]] = 1

M = len(count.keys())
L = list(count.values())
for i in range(M):
  for j in range(i+1,M):
    
    ans += L[i] * L[j]
print(ans+1)

Solution (Python):

A = list(str(input()))
N = len(A)

used = []
count = 0
for a in A:
    if a not in used:
        used.append(a)
        c = A.count(a)
        count+=(c*(c-1)//2)
            
ans = 1
for i in range(N):
    if i == 0:
        pass
    else:
        ans+=(N-i)

ans-=count
print(ans)

Solution (Python):
import collections

A = list(input())
N, ans = len(A), 0

c = collections.Counter(A)
for x in list(c.keys()):
    ans += c[x] * (c[x] - 1) // 2

print(N*(N-1)//2 + 1 - ans)

Solution (Python):
a=input()
ans=len(a)*(len(a)-1)//2+1
b={}
for i in a:
  if i in b:
    b[i]+=1
  else:
    b[i]=1
for i in b:
  ans-=b[i]*(b[i]-1)//2
print(ans)

Solution (Python):
import string

S = input()
ans = len(S)*(len(S)-1)//2 + 1
for i in string.ascii_lowercase:
  c = S.count(i)
  ans -= c*(c-1)//2
print(ans)

Solution (Python):
import collections

A = input()
c = collections.Counter(A)

ans = len(A)*(len(A)-1)//2+1

for v in c.values():
    if v >= 2:
        ans -= v*(v-1)//2

print(ans)

Solution (Python):
from collections import Counter
A = list(input())
n = len(A)
b = Counter(A)
z = n * (n - 1) // 2 + 1

for k in b:
    m = b[k]
    z -= m * (m - 1) // 2
print(z)

Solution (Python):
A = list(input())
n = len(A)

str_cnt = {}
for a in A:
    if a in str_cnt:
        str_cnt[a] += 1
    else:
        str_cnt[a] = 1

zen = n * (n - 1) // 2 + 1

for k in str_cnt:
    num = str_cnt[k]
    if num > 1:
        zen -= num * (num - 1) // 2
print(zen)

Solution (Python):
def main():

    A = input()
    n = len(A)
    ans = 1 + n*(n-1)//2 
    count = dict()
    for c in A:
        count[c] = count.get(c, 0) + 1
    for c in count:
        ans -= count[c] * (count[c] - 1) //2
    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
import math

ans = 0
S = input()
l = len(S)
ans = l * (l-1) //2 + 1
alpha = {}
for c in S:
    if c not in alpha:
        alpha[c] = 0
    ans -= alpha[c]
    alpha[c] += 1
print(ans)

Solution (Python):
import collections
def main():
    A = str(input())
    c = collections.Counter(A)
    c_list = list(c.items())

    ans = len(A)*(len(A)-1)//2
    for i in range(len(c_list)):
    	ans -= c_list[i][1]*(c_list[i][1]-1)//2

    print(ans+1)

if __name__ == "__main__":
    main()

Solution (Python):
s=input()
l=len(s)
t="abcdefghijklmnopqrstuvwxyz"
d={}
for i in t:d[i]=0
for i in s:d[i]+=1
a=l*(l-1)//2
for i in t:a-=d[i]*(d[i]-1)//2
print(a+1)

Solution (Python):
from collections import Counter
a=input()
c=Counter(a)
n=len(a)
if n==1:print(1)
else:
    ans=n*(n-1)//2
    for i in c.values():
        if i==1:continue
        else:ans-=i*(i-1)//2
    print(ans+1)

Solution (Python):
import collections
A = input()
cnt = collections.defaultdict(int)
ans = 1
for i, a in enumerate(A):
    ans += i - cnt[a]
    cnt[a] += 1
print(ans)

Solution (Python):
from operator import mul
from functools import reduce


A = input()
N = len(A)
ans = N*(N-1)//2

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

for i in range(26):
    num = A.count(chr(ord('a') + i))
    
    if num >= 2:
        ans -= cmb(num,2)

print(ans+1)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline

def main():
    s = input().strip()

    count = np.zeros((len(s), ord("z") - ord("a") + 1), dtype=int)
    s_ = [0] * len(s)
    s_[0] = ord(s[0]) - ord("a")
    count[0][s_[0]] += 1
    for i in range(1, len(s)):
        s_[i] = ord(s[i]) - ord("a")
        count[i] += count[i-1]
        count[i][s_[i]] += 1
    
    ans = 0
    for i in range(len(s)-1):
        tmp = len(s) - i - 1 - (count[-1, s_[i]] - count[i, s_[i]])
        ans += tmp
    print(ans+1)

if __name__ == "__main__":
    main()

Solution (Python):
a=input()
dic={}
for i in range(len(a)):
  s=a[i]
  if s in dic:
    dic[s]=dic[s]+1
  else:
    dic[s]=1

sum=0
for val in dic.values():
  if val>1:
    sum=sum+(val-1)*val//2
  
print((len(a)*(len(a)-1))//2-sum+1)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    q,h,s,d = LI()
    n = I()
    print(min(d,s<<1,h<<2,q<<3)*((n<<2)>>3)+min(s,h<<1,q<<2)*(((n<<2)&((1<<3)-1))>>2)+min(h,q<<1)*(((n<<2)&((1<<2)-1))>>1)+q*((n<<2)&((1<<1)-1)))
    return

def B():
    a = input()
    n = len(a)
    f = defaultdict(lambda : 0)
    for i in a:
        f[i] += 1
    ans = 1+(n*(n+1)>>1)
    for i in f.values():
        ans -= i*(i+1)>>1
    print(ans)
    return


def C():

    return


def D():

    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    B()

Solution (Python):
from collections import Counter
a = input()
k = Counter(a).values()
ans = len(a) * (len(a) - 1) // 2
for k_v in k:
    if k_v > 1:
        ans -= k_v * (k_v - 1) // 2
print(ans + 1)

Solution (Python):
a=list(input())
c=[chr(i) for i in range(97, 97+26)]
ct=[0 for i in range(26)]
n=len(a)
for i in range(n):
  ct[c.index(a[i])]+=1
ans=0
for i in range(26):
  ans+=(n-ct[i])*ct[i]
print(ans//2+1)

Solution (Python):

def main():
    S = input().rstrip()
    N = len(S)
    ans = N * (N - 1) // 2 + 1  
    abc = "abcdefghijklmnopqrstuvwxyz"
    for a in abc:
        x = S.count(a)
        ans -= x * (x - 1) // 2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter


def main():
    S = input().rstrip()
    C, N = Counter(S).values(), len(S)
    ans = N * (N - 1) // 2 + 1  
    ans -= sum(i * (i - 1) // 2 for i in C)  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

a = input()
n = len(a)
c = list(Counter(a).items())
ans = n*(n-1)//2 + 1
for i,j in c:
    ans -= j*(j-1)//2
print(ans)

Solution (Python):
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

def sol():
    A = input().strip()

    cnt = Counter(A)

    sameCount = 0
    for _, c in cnt.items():
        sameCount += c * (c - 1) // 2

    N = len(A)
    ans = N * (N - 1) // 2 - sameCount + 1
    print(ans)


sol()

Solution (Python):
a = input()
d = {}
for i in a:
    if i not in d.keys():
        d[i] = 0
    d[i]+=1

import itertools
ans = 0
for l in itertools.combinations(d.values(),2):
    x,y = l[0],l[1] 
    ans+=(x*y)
print(ans+1)

Solution (Python):
import sys
input = sys.stdin.readline

from collections import Counter

A = input().rstrip()

f = lambda x: (x+1)*x//2
answer = f(len(A)) - sum(f(x) for x in Counter(A).values()) + 1

print(answer)

Solution (Python):
from collections import Counter
from string import ascii_lowercase

A = input()





c = Counter(A)
ans = 1 + sum(
    c[x] * sum(c[y] for y in ascii_lowercase if x < y)
    for x in ascii_lowercase
)

print(ans)

Solution (Python):
import collections
s = input()
c = collections.Counter(s)
n = len(s)
ans = 0
for i in range(len(s)):
    ans += (n-i-c[s[i]])
    c[s[i]]-=1
print(ans+1)

Solution (Python):
s=input()
l=len(s)
ans=1+(l*(l-1))//2
dic={}
for t in s:
  if t not in dic:
    dic[t]=1
  else:
    dic[t]+=1
for val in dic.values():
  ans-=(val*(val-1))//2
print(ans)

Solution (Python):
A=input()
N=len(A)
from collections import defaultdict
cnt=defaultdict(int)
ans=1+N*(N-1)//2
for a in A:
    ans-=cnt[a]
    cnt[a]+=1
print(ans)

Solution (Python):
a = input()
cnt = [0 for _ in range(26)]
for x in a:
    cnt[ord(x) - ord('a')] += 1
ans = len(a) * (len(a)-1) // 2 + 1
for c in cnt:
    ans -= c * (c-1) // 2
print(ans)

Solution (Python):
from collections import Counter

def main():
    A = input()
    N = len(A)
    ans = N * (N-1) // 2 + 1
    cnt = Counter(A)
    for c in cnt:
        ans -= cnt[c] * (cnt[c] - 1) // 2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
s = input().rstrip()
cnt = Counter(s)
total = len(s)

ans = 0
for c in s:
    cnt[c] -= 1
    total -= 1
    ans += total - cnt[c]
print(ans + 1)

Solution (Python):
A = input()
_memo = {}
ans = 1
for i,a in enumerate(A):
  ans += i - _memo.setdefault(a,0)
  _memo[a] += 1
  
  
print(ans)

Solution (Python):
from collections import Counter
A = input()
ctr = Counter(A)

ans = 0
for k,v in ctr.items():
    for k2,v2 in ctr.items():
        if k==k2: continue
        ans += v*v2
print(ans//2 + 1)

Solution (Python):
from collections import defaultdict
A=input()
N=len(A)
dp=[0]*26
x=ord('a')
ans=0
for i in range(N):
    dp[ord(A[i]) - x]+=1
    ans += (i + 1 - dp[ord(A[i]) - x])
    
print(ans+1)

Solution (Python):
from itertools import accumulate
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**10
from functools import reduce

A = input()
C = Counter(A)
ans = len(A)*(len(A)-1)//2+1
for k in C.keys():
    ans -= C[k]*(C[k]-1)//2
print(ans)

Solution (Python):
a = input()
n = len(a)
d = {}
for i in a:
    d[i] = d.get(i,0)+1
def cov2(x):
    return x*(x-1)//2
ans = 1 + cov2(n)
for v in d.values():
    ans -= cov2(v)
print(ans)

Solution (Python):
from collections import Counter

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    N = len(S)
    C = Counter(S)
    ans = 0
    for v in C.values():
        ans += v*(v-1)//2
    print(N*(N-1)//2-ans+1)


solve()

Solution (Python):
S = input()
alp = [chr(ord('a') + i) for i in range(26)]
L = [0]*26
def alp(n):
  return(ord(n)-97)
for i in range(len(S)):
  k = alp(S[i])
  L[k] += 1
ans = len(S)*(len(S)-1)//2+1
for i in range(26):
  ans -= L[i]*(L[i]-1)//2
print(ans)

Solution (Python):
a = input().strip()

da = {}
for _a in a:
  if _a in da:
    da[_a] += 1
  else:
    da[_a] = 1

zc = 0
for v in da.values():
  if v >= 2:
    zc += (v * (v-1)) // 2


print((len(a) * (len(a) - 1)) // 2 - zc + 1)

Solution (Python):
b=[0]*26
a=input()
n=len(a)
for i in range(n):
    b[ord(a[i])-ord("a")]+=1
print(n*(n-1)//2-sum([i*(i-1)//2 for i in b])+1)

Solution (Python):
s=input();print((len(s)**2-sum(s.count(chr(97+i))**2for i in range(26)))//2+1)

Solution (Python):
import collections as c,math
f=math.factorial
s=input()
n=len(s)
a=n*-~n//2+1
for i in c.Counter(s).values():
  a-=i
  if i>1:
    a-=f(i)//f(i-2)//2
print(a)

Solution (Python):
import collections as c,math
f=math.factorial
s=input()
n=len(s)
a=n*-~n//2+1
for i in c.Counter(s).values():
  a-=i
  if i>1:
    a-=f(i)//f(i-2)//2
print(a)

Solution (Python):
from collections import defaultdict

A = list(input())
N = len(A)
D = defaultdict(int)

for i in A:
    D[i] += 1

ans = 1 + int(N * (N - 1) / 2)

for i in D:
    ans -= int(D[i] * (D[i] - 1) / 2)

print(ans)

Solution (Python):
from collections import Counter
s = input()
n = len(s)
d = Counter(s)
c = 0
for x in d:
  c += d[x]*(d[x]-1)//2
print(1 + n*(n-1)//2 - c)

Solution (Python):

import sys
from collections import deque
import copy
import math

class manacher:
    """Cache radius array of palindrome to judge in linear time."""

    def __init__(self, l):
        """Insert '&' to judge both even and odd length palindrome."""
        self.n = len(l)
        self.m = 2*self.n+1
        self.d = ['&']*self.m
        self.r = [0]*self.m
        for i in range(self.n):
            self.d[2*i+1] = l[i]
        self.make_r()

    def make_r(self):
        """Make self.r[i] (radius of palindrome whose center is self.d[i])."""
        i, j = 0, 0
        while i < self.m:
            while j <= i < self.m-j and self.d[i-j] == self.d[i+j]:
                j += 1
            self.r[i] = j
            k = 1
            while k <= i < self.m-k and k+self.r[i-k] < j:
                self.r[i+k] = self.r[i-k]
                k += 1
            i += k
            j -= k

    def judge(self, start, end):
        """Return 1 if l[start, end) is a palindrome."""
        center = start+end
        return 2*end-1 < center+self.r[center]

    def get_num(self, center):
        return self.r[center]


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]
    dic = {}
    for s in S:
        if s not in dic:
            dic[s] = 1
        else:
            dic[s] += 1
    count = len(S) * (len(S) - 1)/2 + 1
    for s in dic:
        count -= dic[s] * (dic[s] - 1)/2
    print count







    


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
a = input()
c = Counter(a)
n = len(a)

ans = n * (n-1) // 2 + 1
for i in range(26):
    w = chr(ord('a') + i)
    count = c[w]
    if count >= 2:
        ans -= count * (count - 1) // 2

print(ans)

Solution (Python):
A = input()

ans = len(A)*(len(A)-1)//2 + 1

for i in range(ord("a"),ord("z")+1) :
    s = chr(i)
    c = A.count(s)
    ans -= c*(c-1)//2

print(ans)

Solution (Python):
S = input()
N = len(S)
ans = N * (N - 1) // 2 + 1
a = []
cnt = 0
for c in [chr(i) for i in range(97, 97+26)]:
    cnt = S.count(c)
    a.append(cnt * (cnt - 1) // 2)
ans -= sum(a)
print(ans)

Solution (Python):
S = input()
N = len(S)
ans = 1 + N*(N-1)//2
ct = [0] * 26
for c in S:
    i = ord(c) - ord('a')
    ct[i] += 1

for a in ct:
    ans -= a*(a-1)//2
print(ans)

Solution (Python):
S = input()
N = len(S)
ans = 1 + N*(N-1)//2
ct = [0] * 26
for c in S:
    i = ord(c) - ord('a')
    ct[i] += 1

for a in ct:
    ans -= a*(a-1)//2
print(ans)

Solution (Python):
s = input()
n = len(s)
dp = [0]*26

for i in s:
  dp[ord(i) - ord('a')]+=1

ans = 0

for j in dp:
  ans += (j * (n-j))
print(ans//2+1)