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: Time Limit Exceeded

Solution code:
num=raw_input()
out=0
for i in range(len(num)):
    #out+=num[i:].count(num(i))
    #print len(num[i:])-num[i:].count(num[i])
    out+=len(num[i:])-num[i:].count(num[i])

print out+1

Submission status: Time Limit Exceeded

Solution code:
# Reverse and Compare
A = input()

L = len(A)
L = len(A)
pe = [A]
for i in range(L):
    for j in range(L-i-1):
        pA = A[i:i+j+2]
        rpA = pA[::-1]
        if pA != rpA:
            a = []
            a.append(A[:i])
            a.append(rpA)
            a.append(A[i+j+2:])
            s = "".join(a)
            if s not in pe:
                pe.append(s)
print(len(pe))

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 1

for i in range(1, len(s)):
    ss = s[0:i]
    # print(ss, s[i])

    n = len(ss) - ss.count(s[i])
    count += n
    # print(n, count)
print(count)

Submission status: Time Limit Exceeded

Solution code:
a = input()

ans = 1

for i in range(len(a)):
    for j in range(i, len(a)):
        if a[j:i:-1] + a[i] != a[i:j + 1] and a[i] != a[j]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
A = list(input())
N = len(A)
uq_list = []


def concatStr(liststr):
    temp = ""
    for _ in liststr:
        temp += _
    return temp

for i in range(0, N):
    for j in range(i, N):
        temp = A.copy()
        temp[i], temp[j] = temp[j], temp[i]
        strings = concatStr(temp)
        if strings not in uq_list:
            uq_list.append(strings)

print(len(uq_list))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
se=[s]
n=len(s)
num=0
total=n*(n-1)//2 +1
for i in range(n):
  for j in range(i+1,n):
    if s[i]==s[j]:
      num+=1
print(total-num)

Submission status: Time Limit Exceeded

Solution code:
x = str(input())

res=1
st=0
while st<=len(x)-2:
    la=len(x)-1
    while la>=st:

        i=st
        j=la
        flg=False
        while (i<j):
            if x[i]==x[j]:break    
            if x[i]!=x[j]:
                flg=True
                break
            i=i+1
            j=j-1
        if flg==True:
            res=res+1
        
        la=la-1
    st=st+1

print(res)

Submission status: Time Limit Exceeded

Solution code:
A = input()

que = []
i = 0
j = 0

while i != len(A) + 1 and j != len(A) + 1:
    reA = A[i:j]
    reB = reA[::-1]
    if i == 0 and j == len(A):
        ans = reB
    elif i == 0:
        ans = reB + A[j:]
    elif j == len(A):
        ans = A[:i] + reB
    else:
        ans = A[:i] + reB + A[j:]
    an = ans in que
    if an == False:
        que.append(ans)
    if j == len(A):
        i += 1
        j = i + 1
    else:
        j += 1

print(len(que))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
se=[s]
n=len(s)
num=0
total=n*(n-1)//2+1
s_="0"+s
for i in range(n):
  for j in range(1,n-i):
    k=s_[1:i+1]+s_[i+j+1:i:-1]+s_[i+j+2:]
    if k in se: num-=1
    else: se+=[k]
print(num+total)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import defaultdict
from itertools import combinations

def main():
    A = str(input())
    set_all = set()
    set_all.add(A)
    for i in range(2, len(A) + 1):
        for j in range(len(A) - i + 1):
            _i, _j = j, i + j - 1
            if _i == 0:
                a = A[_j::-1] + A[_j + 1:]
            else:
                a = A[:_i] + A[_j:_i - 1:-1] + A[_j + 1:]
            if a in set_all:
                continue
                # print(_i, _j)
                # print(a)
            else:
                set_all.add(a)
    print(len(set_all))



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
a = input()

aa = [a]
ans = 1
for i in range(len(a)-1):
    for j in range(i+2,len(a)+1):
        b = a[:i] + a[i:j][::-1] + a[j:len(a)+1]
        if not b in aa:
            aa.append(b)
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':    
    A = input()
    cand = set([A])
    l = len(A)
    for i in range(0, l - 1):
        for j in range(i + 1,l):
            p = A[i:j+1]
            cand.add((A[0:i] + p[::-1] + A[j + 1:l]))
    print(len(cand))

Submission status: Time Limit Exceeded

Solution code:
a = input()

ans = [a]

for i in range(len(a)):
    for j in range(i, len(a)):
        rev = a[0:i] + a[j:i:-1] + a[i] + a[j+1:]
        if rev not in ans:
            ans.append(rev)

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
# Reverse and Compare

A = input()

L = len(A)
pe = [A]
for i in range(L):
    for j in range(L-i-1):
        if A[i:i+j+2] != A[i:i+j+2][::-1]:
            a = A[:i]+A[i:i+j+2][::-1]+A[i+j+2:]
            if a not in pe:
                pe.append(a)
print(len(pe))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
se=set()
se.add((s))
n=len(s)
s_="0"+s
for i in range(n):
  for j in range(1,n-i):
    k=(s_[1:i+1]+s_[i+j+1:i:-1]+s_[i+j+2:]) 
    se.add(k)
print(len(se))

Submission status: Time Limit Exceeded

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

a = raw_input()

count = 0

for i in range(len(a)):
    for j in range(len(a)-i-1):
        if a[i] == a[i+j+1]:
            count += 1

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

print ans

Submission status: Time Limit Exceeded

Solution code:
# Reverse and Compare

A = input()

L = len(A)
pe = [A]
for i in range(L):
    for j in range(L-i-1):
        a = A[:i]+A[i:i+j+2][::-1]+A[i+j+2:]
        if a not in pe:
            pe.append(a)
            #print(a)
print(len(pe))

Submission status: Time Limit Exceeded

Solution code:
A = input()

que = []

def solve(A):
    for i in range(len(A)+1):
        for j in range(len(A)+1):
            if i <= j:
                reA = A[i:j]
                reB = reA[::-1]
                if i == 0 and j == len(A):
                    ans = reB
                elif i == 0:
                    ans = reB + A[j:]
                elif j == len(A):
                    ans = A[:i] + reB
                else:
                    ans = A[:i] + reB + A[j:]
                an = ans in que
                if an == False:
                    que.append(ans)
solve(A)
print(len(que))

Submission status: Time Limit Exceeded

Solution code:
A = input()

result = 1
for i in range(len(A)):
    for j in range(len(A)-i):
        if A[i] != A[i+j]:
            result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
se=[s]
n=len(s)
s_="0"+s
for i in range(n):
  for j in range(1,n-i):
    k=s_[1:i+1]+s_[i+j+1:i:-1]+s_[i+j+2:]
    se+=[k]
uniq = list(set(se))
print(len(uniq))

Submission status: Accepted

Solution code:
A=input()
n=len(A)
from collections import defaultdict
d = defaultdict(int)
for i in A:
    d[i]+=1
a=0
for i in d.values():
    a+=i*(i-1)//2
print(n*(n-1)//2+1-a)

Submission status: Accepted

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

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

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

for n in R.values():
  ans -= n * (n - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
A=list(input())
m=len(A)

B=Counter(A)

cnt=0
for i in A:
    B[i] -=1
    cnt +=B[i]
print(m*(m-1)//2-cnt+1)

Submission status: Accepted

Solution code:
from collections import Counter
A=list(input())
length=len(A)

B=Counter(A)

cnt=0
for i in A:
    B[i] -=1
    cnt +=B[i]
print(length*(length-1)//2-cnt+1)

Submission status: Accepted

Solution code:
s = input()

dist = -1
for i in range(26):
    temp = s.count(chr(ord("a")+i))
    dist += temp * ~-temp // 2

print(len(s) * ~-len(s) // 2 - dist)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
A = input()
N = len(A)
d = dd(int)
for i in range(N):
	d[A[i]] += 1
cnt = 0
for v in d.values():
	cnt += v*(v-1)//2
print(N*(N-1)//2 - cnt + 1)

Submission status: Accepted

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)



def solve(n=None, s1=None, s2=None):
    s = getS()
    cnt = Counter(s)
    n = len(s)
    ans = 0
    for k, v in cnt.items():
        ans += v * (n - v)
        

    ans //= 2
    ans += 1

    print(ans)


def main():
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=input()
ans=1
from collections import defaultdict
d = defaultdict(int)
for i in range(len(a)):
    d[a[i]]+=1
    ans+=(i+1-d[a[i]])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
a=input()
cnt=Counter(a)
n=len(a)
ans=n*(n-1)//2+1

for v in cnt.values():
    ans-=v*(v-1)//2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
 
A = sys.stdin.readline().strip()
la = len(A)
 
ans = la * (la - 1) // 2 + 1
counter = Counter(A)
for key, count in counter.items():
    if count == 1:
        continue
    ans -= count * (count - 1) // 2
 
print(ans)

Submission status: Accepted

Solution code:
import sys
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


A = LS2()
N = len(A)

# 答えは、(Ai != Aj たる 1 <= i < j <= N の個数) +1

from collections import defaultdict

d = defaultdict(int)
for i in range(N):
    d[A[i]] += 1

ans = 0
for key in d.keys():
    ans += d[key]*(N-d[key])

ans //= 2
ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


A = LS2()
N = len(A)

from collections import defaultdict

d = defaultdict(int)
for i in range(N):
    d[A[i]] += 1


ans = 0
for key in d.keys():
    ans += d[key]*(N-d[key])

ans //= 2
ans += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


A = input()
N = len(A)
cntr = Counter(A)
ans = 0
for c in A:
    ans += N - cntr[c]
ans //= 2
print(ans + 1)

Submission status: Accepted

Solution code:
s=input()
l={}
for i in s:
  if i in l:
    l[i]+=1
  else:
    l[i]=1

def c(x):
  return x*(x-1)//2

sm=c(len(s))
for i in l:
  sm-=c(l[i])
print(sm+1)

Submission status: Accepted

Solution code:
a = input()
count = [0]*26
ans = 1
for i in range(len(a)):
    x = ord(a[i])-ord("a")
    ans += i-count[x]
    count[x] += 1
print(ans)

Submission status: Accepted

Solution code:
a = input()
n = len(a)

l = [0]*(2*n-1)
i = 0
j = 0
while i < 2*n - 1:
    while i >= j and i+j+1 < 2*n and a[(i-j)//2] == a[(i+j+1)//2]:
        j += 1
    l[i] = j
    k = 1
    while i + k < 2*n-1 and i >= k and l[i] >= k and l[i-k] != l[i]-k:
        l[i+k] = min(l[i-k],l[i]-k)
        k += 1
    i += k
    if j > k:
        j -= k
    else:
        j = 0
ans = n*(n-1)//2+1
count = [0]*26
for i in a:
    count[ord(i)-ord("a")] += 1
for i in count:
    ans -= i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

a = input()
cnta = Counter(a)

ans = (len(a)** 2 - len(a)) // 2 + 1
for i in cnta.values():
    ans -= (i ** 2 - i) // 2
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

A=input()

A=list(map(lambda x:ord(x)-97,A))

log=[0]*26
log[A[0]]+=1
ans=1


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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

A = input()[:-1]
cnt = defaultdict(int)
ans = 0

for i in range(len(A)):
    ans += i-cnt[A[i]]
    cnt[A[i]] += 1

print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
d=dict(zip([chr(i+97) for i in range(26)],[0]*26))
a=input()
n=len(a)
for i in a:
  d[i]+=1
ans=n*(n-1)//2+1
for i in d.values():
  ans-=i*(i-1)//2
print(ans)

Submission status: Accepted

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

cnt = Counter(a)
ans = n * (n - 1) // 2 - sum(m * (m - 1) // 2 for m in cnt.values()) + 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = str(input())
n = len(s)
ans = 1 + n * (n-1) // 2
for k, v in Counter([x for x in s]).items():
    ans -= v * (v-1) // 2
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    s = input()
    l = len(s)
    counter = Counter()
    ans = 1

    for i in range(l):
        char = s[i]
        ans += i - counter[char]
        counter[char] += 1

    print(ans)


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, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
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:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
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 TUPLE(): return tuple(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 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

A = input()

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    n=len(s)
    dict_=defaultdict(lambda:0)
    for i in range(n):
        dict_[s[i]]+=1
    ans=1+(n*(n-1))//2
    for x in dict_.keys():
        ans-=(dict_[x]*(dict_[x]-1))//2
    print(ans)
        
            
            
            
            
        
    
    
    
    



if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from string import ascii_lowercase as a2z

input = sys.stdin.readline


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

    count = {al: 0 for al in a2z}
    ans = 1
    for i, a in enumerate(A):
        ans += (i - count[a])
        count[a] += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

A = input()
Ac = Counter(A)
cnt = 0
# 前からdp等して数え上げするか
# グルーピングして掛け算するか

# A = 'babab'だった場合
# (1, 2):abbab
# (1, 3):babab
# (1, 4):ababb
# (1, 5):babab

# (2, 3):bbaab
# (2, 4):babab
# (2, 5):bbaba

# (3, 4):babba
# (3, 5):babab

# (4, 5):babab

# i < jでAi = Ajだった場合、
# Ai+1 と Aj-1とで結果がダブる
for val in Ac.values():
    cnt += val * (val - 1) // 2
print(len(A) * (len(A)-1) // 2 - cnt + 1)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    s = input()
    c = collections.Counter(s)

    l = len(s)
    total = l * (l - 1) // 2

    for ci in c.values():
        tmp = ci * (ci - 1) // 2
        total -= tmp

    print(total + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

A = input()
Ac = Counter(A)
cnt = 0
for val in Ac.values():
    cnt += val * (val - 1) // 2
print(len(A) * (len(A)-1) // 2 - cnt + 1)

Submission status: Accepted

Solution code:
a=list(input())

unko=[0]*(ord("z")+1-ord("a"))

for aa in a:
    unko[ord(aa)-ord("a")]+=1
tmp=0
for u in unko:
    tmp+=(u*(u-1))//2

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
print((len(a:=input())**2-sum(map(lambda x:x*x,map(a.count,[chr(i)for i in range(97,123)]))))//2+1)

Submission status: Accepted

Solution code:
import collections
A= input()
Ac = collections.Counter(A)
cnt = 0
for val in Ac.values():
    cnt += val*(val-1)//2
print(len(A)*(len(A)-1)//2-cnt+1)

Submission status: Accepted

Solution code:
s = input().rstrip()
n = len(s)
ans = n * (n-1) // 2

from string import ascii_lowercase

for c in ascii_lowercase:
    m = s.count(c)
    ans -= m * (m-1) // 2

print(ans + 1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    a = input()
    cnt = defaultdict(lambda: 0)
    for aa in a:
        cnt[aa] += 1
    ans = len(a) * (len(a) - 1) // 2 + 1
    for c, n in cnt.items():
        ans -= n * (n - 1) // 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a = input()
    n = len(a)
    total = (n - 1) * n // 2 + 1

    D = Counter(a)
    for v in D.values():
        total -= (v - 1) * v // 2
    print(total)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    A = input()
    N = len(A)
    c = Counter(A)
    print(1 + N * (N - 1) // 2 - sum([v * (v - 1) // 2 for v in c.values()]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
A = input()
n = len(A)
c = Counter(A)

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

Submission status: Accepted

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

A = readline().rstrip()

def nC2(x):
  return (x * (x - 1)) // 2

from collections import defaultdict
dic = defaultdict(int)

for a in A:
  dic[a] += 1
  
ans = nC2(len(A))
for v in dic.values():
  ans -= nC2(v)

print(ans + 1)

Submission status: Accepted

Solution code:
A = input()

dp = [0]*len(A)
dp[0] = 1
dic = {chr(ord('a')+i):0 for i in range(26)}
dic[A[0]] += 1

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

Submission status: Accepted

Solution code:
# 全体から引く？全体がわからん わかるの？
# abcd -> [abcd, bacd, cbad, dcba, acbd, adcb, abdc] で7
# aattはaaとttが回分になるのでそれを引いて5？
# abc -> [abc, bac, cba, acb]
# abracadabra 11文字
# 1 + 2 + .. + n - 1 = n*(n-1)/2 3文字だと3で、これにもとのやつを足して4
# 4文字だと4*3/2 + 1 = 6 + 1 = 7
# 11文字だと11*10/2 + 1 = 56 あってそう
# maxだと2 * 10^5 せいぜい19999900000
# arbacadabra を構成する方法が2通りあるということだが？？？？
# （abracadabra がオリジナル）
# たしかに 偶数からなる場合は abraとbrをの2パターンある
# 両端が同じで長さが偶数ならたしかにそうなる
# しかし両端が同じなら、というのならば奇数でも同じではないか？
# -> つまり、同じ文字に挟まれたところはすべて該当するのではないか？
# abracadabraでは {'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1} で、同じ文字で2つえらぶとすると a = 5*4/2 = 10, b = 1, r = 1
# これを足して全体から引くと答えになりそう
s = list(input())
d = {}
for c in s:
    if c not in d:
        d[c] = 1
    else:
        d[c] += 1
ans = len(s) * (len(s) - 1) // 2 + 1
for v in d.values():
    ans -= v * (v - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    # A = 'abracadabra'
    # a = set()
    # a.add(A)
    # for i in range(11):
    #     for j in range(i,11):
    #         tmp = A[i:j+1][::-1]
    #         if i > 0:
    #             tmp = A[:i] + tmp
    #         if j < 10:
    #             tmp = tmp + A[j+1:]
    #         if tmp in a:
    #             print(i,j,tmp)
    #         a.add(tmp)
    # print(len(a))
    A = input()
    N = len(A)
    l = Counter(A)
    ans = N*(N+1)//2 + 1 - N
    for x in l.values():
        ans -= x*(x-1)//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
dic = defaultdict(int)
S = str(input())
N = len(S)
for i in range(N):
  dic[S[i]] += 1
#print(dic)

ans = 1 #もともと
L = list(dic.keys())
for i in range(len(L)):
  for j in range(i+1,len(L)):
    temp = dic[L[i]]*dic[L[j]]
    ans += temp
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = list(in_s())
    N = len(S)
    cnt = Counter(S)

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

    orda = ord('a')
    for c in range(orda, orda + 26):
        c = chr(c)
        n = cnt[c]
        ans -= n * (n - 1) // 2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

A = sys.stdin.readline().strip()
la = len(A)

ans = la * (la - 1) // 2 + 1
counter = Counter(A)
for key, count in counter.items():
    if count == 1:
        continue
    ans -= count * (count - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import Counter
def main():
    A = input()
    n = len(A)
    A = Counter(A)
    ret = n *(n-1)//2
    for i, c in A.items():
        ret -= c*(c-1)//2
    print(ret+1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    A = list(input())
    d = defaultdict(int)
    for a in A:
        d[a] += 1
    ans = 1
    for i in range(97,123):
        for j in range(i+1,123):
            ans += d[chr(i)] * d[chr(j)]

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    A = input()
    n = len(A)

    res = (n - 1) * n // 2 + 1
    D = Counter(A)
    for v in D.values():
        if v >= 2:
            res -= (v - 1) * v // 2
    print(res)


if __name__ == '__main__':
    resolve()