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



Sample input:
aatt


Sample output:
5

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    N=len(S)
    cnt = Counter(S)
    ans = 0
    for i,s in enumerate(S):
        cnt[s]-=1
        remain = N-i-1
        ans += remain - cnt[s]
    print(ans+1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

for v in Counter(A).values():
	ans -= v*(v-1)//2
print(ans+1)

Submission status: Accepted

Solution code:
A = input()
import collections
a_frq_dict = collections.Counter(A)

n_all = len(A) * (len(A)-1) * 0.5 + 1
n_duplication = 0
for frq in a_frq_dict.values():
    n_duplication += frq * (frq-1) * 0.5

print(int(n_all - n_duplication))
# print(n_all, n_duplication)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
#input = sys.stdin.readline
from collections import Counter

def main():
    a = input()
    n = len(a)

    base = n * (n - 1) // 2
    counter = Counter(a)

    def f():
        for c in counter.values():
            yield c * (c - 1) // 2

    print(base - sum(f()) + 1)

main()

Submission status: Accepted

Solution code:
# 解説AC
A = list(input())
cnt = [0] * 26
for s in A:
    cnt[ord(s) - ord("a")] += 1

ans = len(A) * (len(A) - 1) // 2
ans -= sum(c * (c - 1) // 2 for c in cnt)
ans += 1

print(ans)

Submission status: Accepted

Solution code:
import math

a = input()

d = {}

for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1

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

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

Submission status: Accepted

Solution code:
A=input()
cnt={chr(code): 0 for code in range(ord('a'),ord('z')+1)}
for i in A:
  cnt[i]+=1

a=len(A)
if a==1:
  print(1)
  exit()
b=0
for i in cnt.values():
  if i>=2:
    b+=i*(i-1)//2
ans=a*(a-1)//2-b
print(ans+1)

Submission status: Accepted

Solution code:
from collections import Counter

A = input()
len_a = len(A)

ans = len_a * (len_a - 1) // 2 + 1
for i in Counter(A).values():
    ans -= i * (i - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
A = input()
N = len(A)
ans = 1 + N*(N-1)//2
count = Counter(A)
for k, v in count.items():
    ans -= v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
a=list(input())
ans=len(a)*(len(a)-1)//2
import collections
J=dict(collections.Counter(a))
for i in J.values():
    ans-=i*(i-1)//2
print(ans+1)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
import string
DD = [chr(i) for i in range(ord("a"), ord("z")+1)]
D = {}
for i in range(len(DD)):
    D[DD[i]] = i
A = [0] * 26
ans = 1
for i in range(n):
    ans += i - A[D[s[i]]]
    A[D[s[i]]] += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    A=input()
    N=len(A)
    ans=1
    p={}
    for i,a in enumerate(A):
        if a not in p:
            p[a]=0
        ans+=i-p[a]
        p[a]+=1    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
A = input()
n = len(A)
ans = (n-1)*n//2+1
alp = 'abcdefghijklmnopqrstuvwxyz'
for i in alp:
    num = A.count(i)
    ans -= num*(num-1)//2
print(ans)

Submission status: Accepted

Solution code:
a=input()
n=len(a)
ans=n*(n-1)//2+1
d=dict()
for i in range(n):
  if a[i] not in d:
    d[a[i]]=1
  else:
    d[a[i]]+=1
d=list(d.items())
for i,j in d:
  ans-=j*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
A = [ord(a) - 97 for a in input()]
N = len(A)
D = {}
ans = N * (N - 1) // 2 + 1
for i, a in enumerate(A):
    if a in D:
        ans -= D[a]
        D[a] += 1
    else:
        D[a] = 1
print(ans)

Submission status: Accepted

Solution code:
def main():
  A = input()
  N = len(A)
  ans = 0
  for i in range(26):
    c = ord('a')+i
    c = chr(c)
    m = A.count(c)
    ans += m*(m-1)//2
  print(N*(N-1)//2-ans+1)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7
from collections import defaultdict
def ddprint(x):
  if DBG:
    print(x)

a = ins()
h = defaultdict(int)
for i in range(len(a)):
    h[a[i]] += 1
sm = 1
keys = list(h.keys())
for i,x in enumerate(keys):
    for j in range(i+1,len(keys)):
        sm += h[keys[i]]*h[keys[j]]
print(sm)

Submission status: Accepted

Solution code:
from collections import defaultdict
A = input()
n = len(A)
ans = 1
cnt = defaultdict(int)
for i in range(n):
    ans += i - cnt[A[i]]    # iの累計 n*(n-1)/2
    cnt[A[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
A = raw_input()

C = [0]*26
for a in A:
  C[ord(a)-ord("a")] += 1
ans = len(A)*(len(A)+1)/2+1
for c in C:
  ans -= c*(c+1)/2
print ans

Submission status: Accepted

Solution code:
A = input()

dic = {}
for i in range(len(A)):
    if A[i] in dic:
        dic[A[i]] += 1
    else:
        dic[A[i]] = 1

ans = len(A)*(len(A)-1)//2 + 1
for key in dic:
    ans -= dic[key]*(dic[key]-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


A = input()

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

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def f(A):
    N = len(A)
    D = {}

    for a in A:
        D[a] = D.setdefault(a, 0) + 1
    
    return sum([v * (v - 1) // 2 for v in D.values()])


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

    c = f(A)

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
A = input()

freq = [0] * 26
freq_sum = 0

result = 1

for x in map(lambda c: ord(c) - ord('a'), A):
    result += freq_sum - freq[x]
    freq[x] += 1
    freq_sum += 1

print(result)

Submission status: Accepted

Solution code:

import numpy as np

dic={}
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'

A = input()
for a in ascii_lowercase:
    dic[a]=[0]*len(A)

i=0
for l in A:
    dic[l][i]=1
    i+=1

for d in dic:
    dic[d]=np.cumsum(dic[d])



dp=[0]*len(A)
dp[0]=1
for i in range(1,len(A)):
    tmp = (i+1-dic[A[i]][i])
    #print(tmp)
    dp[i]=dp[i-1]+tmp

print(dp[-1])

Submission status: Accepted

Solution code:
from collections import Counter

A = input()
N = len(A)

c = Counter(A)
ans = 1 + sum(v*(N-v) for v in c.values()) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

A = input()
N = len(A)

c = Counter(A)
ans = 1 + N*(N-1)//2 - sum((v*(v-1))//2 for v in c.values())

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
 
show_flg=False
show_flg=True
 
ans=0

s=input()
n=len(s)

ans+=1+n*(n-1)//2
p=''
c=1
s=sorted(s)
for i in [1]+s+[0]:
    if p==i:
        c+=1
    else:
        ans-=c*(c-1)//2
        c=1
    p=i    
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = len(S) * (len(S)-1) // 2 + 1

for ch in 'abcdefghijklmnopqrstuvwxyz':
  n = S.count(ch)
  if n>0:
    #print(ch,n)
    ans -= n * (n-1) // 2
    
print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    s = list(input())
    n = len(s)
    dic = defaultdict(int)
    for i in range(n):
        dic[s[i]] += 1
    ans = 0
    for e in dic:
        ans += (n-dic[e])*dic[e]
    print(ans//2+1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

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

d = defaultdict(int)

dp = [0]*N
dp[0] = 1
d[A[0]] = 1
for i in range(1,N):
    dp[i] = dp[i-1]+i-d[A[i]]
    d[A[i]] += 1

print(dp[-1])

Submission status: Accepted

Solution code:
from collections import Counter
a=input()
n=len(a)
d=Counter(a)

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

for c in d.values():
    res -= (c*(c-1))//2
print(res)

Submission status: Accepted

Solution code:
A=list(input())
N=len(A)

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

for s in "abcdefghijklmnopqrstuvwxyz":
    n=A.count(s)
    if n>=2:
        S=S-n*(n-1)//2

print(S)

Submission status: Accepted

Solution code:
from collections import Counter


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

num=Counter(A)

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

for i in num.values():
    if i==1:continue
    else:
        S=S-i*(i-1)//2

print(S)

Submission status: Accepted

Solution code:
A = input().strip()
C = {chr(i):0 for i in range(97,123)}
n = len(A)
for i in range(n):
    C[A[i]] += 1
cnt = 0
for a in C:
    cnt += (C[a]*(C[a]-1))//2
print(1+(n*(n-1))//2-cnt)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
a=input()
li=[0]*26
for i in range(len(a)):
    li[ord(a[i])-ord("a")]+=1
ans=len(a)*(len(a)-1)//2+1
for i in li:
    ans-=i*(i-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

A = input()

N = len(A)
C = Counter(A)

print(1 + N * (N - 1) // 2 - sum(v * (v - 1) // 2 for v in C.values()))

Submission status: Accepted

Solution code:
d = {}
for i in range(26):
    d[i] = 0
s = input()
n = len(s)
for i in range(n):
    x = s[i]
    y = ord(x) - 97
    d[y] += 1
ans = 0
for i in range(26):
    ans += d[i]*(n-d[i])
ans = ans // 2 + 1
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

bbn=1000000007
	
#+++++
#Reverse and Compare
#https://atcoder.jp/contests/agc019/tasks/agc019_b


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
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
#import numpy as np
from collections import Counter

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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)
# 53

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def examB():
    A = SI(); N = len(A)
    d = defaultdict(int)
    for s in A:
        d[s] +=1
#    print(d)
    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()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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()
# template

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

def main():
    S = input().rstrip()
    # ss = set()
    # ss.add(S)
    # for K in range(2, len(S) + 1):
    #     for i in range(len(S) - K + 1):
    #         news = ''.join([S[0:i], reverse(S[i:i + K]), S[i + K:]])
    #         print(news, i, K, end="")
    #         if news not in ss:
    #             ss.add(news)
    #             print("")
    #         else:
    #             print("*")
    # print(len(ss))
    d = collections.defaultdict(int)
    for x in S:
        d[x] += 1
    # print(d)
    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()

Submission status: Accepted

Solution code:
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(d[i])
print(ans//2+1)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
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, (len(table[ord(S[i]) - a]) - x))
print(res)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#editorial参照
#答えは(i < jかつAi̸=Ajであるような組(i; j)の個数) +1
#上記のi～jの反転はi+1～j-1の反転と同等
#回文や接尾辞とかの厄介なアルゴリズムは不要
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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):
    # i より左にあるやつと c を選ぶと、その間の文字列を反転したのと同じになる
    ans -= counts[c]
    counts[c] += 1
print(ans)

# print(test(S))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()))

Submission status: Accepted

Solution code:
from collections import Counter
a = input()
n = len(a)
data = Counter(a)
#print(data)
ans = n*(n-1)//2 + 1
for i in data:
    ans -= (data[i]-1)*data[i]//2

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

    a = Counter(input())
    ans = 1

    # KeyInsight
    # 何も操作しない場合が1通り
    # 異なる文字(a-z)を2つ選ぶ組み合わせ

    # 以下の場合だと，組み合わせは増えない．
    # 例2：abcda．1文字目と5文字目の場合，2文字目と4文字目の場合の並びは同じ．
    # See:
    # https://www.youtube.com/watch?v=9OiB8ot3a0w
    for i, j in combinations(a.values(), 2):
        ans += i * j

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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
            
#print(L)
ans=1
num=sum(L)
for i in range(26):
    ans+=L[i]*(num-L[i])
    num-=L[i]
print(ans)

Submission status: Accepted

Solution code:
#%%
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)

#%%

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)