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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#D問題
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)
#print(ans,count)
ans-=count
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():

    A = input()
    n = len(A)
    ans = 1 + n*(n-1)//2 # +1 for unflipped case
    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())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#A
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
#B
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

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AGC019B - Reverse and Compare
def main():
    S = input().rstrip()
    N = len(S)
    ans = N * (N - 1) // 2 + 1  # all possible patterns
    abc = "abcdefghijklmnopqrstuvwxyz"
    for a in abc:
        x = S.count(a)
        ans -= x * (x - 1) // 2  # exclude duplicates
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC019B - Reverse and Compare
from collections import Counter


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC19 B

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from string import ascii_lowercase

A = input()


# 英小文字xについて、Aに含まれるxの個数をc[x]で表す。
# 解は、1 + c['a'](c['b'] + ... + c['z']) +
# c['b'](c['c'] + ... + c['z']) + c['y']['z'] である。
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(dp,ans)
print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
# python template for atcoder1
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

##    mana = manacher(S)
##    s = 0
##    for i in range(0, len(S) * 2, 2):
##        #print (mana.get_num(i) + mana.get_num(i + 1)) /2,
##        s += (mana.get_num(i) + mana.get_num(i + 1)) /2

    #print (len(S) * (len(S) + 1))/2 - s + 1


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in s:
  dp[ord(i) - ord('a')]+=1
#print(dp)
ans = 0
#重複する二つの文字分だけ減らして加える
for j in dp:
  ans += (j * (n-j))
print(ans//2+1)

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)

s = input()
s = sorted(s)
g = gb(s)
l = len(s)

res = l * (l - 1) // 2 + 1
for k, v in g:
    p = len(list(v))
    res -= p * (p - 1) // 2
print(res)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
DP = [0]*26
ans = 0
for st in S:
  DP[ord(st) - ord('a')] += 1
for i in range(26):
  ans += DP[i] * (N - DP[i])
print(ans//2 + 1)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

a = ns()
n = len(a)

cnt = Counter(a)

eqs = 0
for k,v in cnt.items():
    eqs += v*(v-1)//2
    
print(1 + n*(n-1)//2 - eqs)

Submission status: Accepted

Solution code:
A = input()
D = [0]*(26)
ans = 0
D[ord( A[0]) - ord("a")] = 1
for i, a in enumerate(A[1:]):
    ans += i + 1- D[ ord( a) - ord("a")]
    D[ ord( a) - ord("a")] += 1
print( ans + 1)

Submission status: Accepted

Solution code:
S=input()
A=[0]*26
N=len(S)
a=ord('a')
ans=0
for i in range(N):
    t=ord(S[i])-a
    A[t]+=1
for t in range(26):
    ans+=(A[t]*(A[t]+1))//2
print((N*(N+1))//2 - ans +1)

Submission status: Accepted

Solution code:
from collections import defaultdict
A = input()
Alphabet = defaultdict(int)

for i in range(len(A)):
    Alphabet[A[i]] += 1
Total = (len(A) - 1) * len(A) // 2 + 1
for key in Alphabet:
    if Alphabet[key] > 1:
        double = (Alphabet[key] - 1) * Alphabet[key] // 2
        Total -= double
print(Total)

Submission status: Accepted

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

a=list(input())
from collections import Counter
c= Counter(a)
cv=list(c.values())
s=len(a)*(len(a)-1)/2
for i in cv:
 s-=i*(i-1)/2
print(int(s)+1)

Submission status: Accepted

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

"""
自力ではわからず解説PDFを見て実装
"""

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

A = input()
counter = Counter(A)

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

Submission status: Accepted

Solution code:
ch = ['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']

p = []

a = list(input())
n = len(a)
for i in range(len(ch)):
    p.append(a.count(ch[i]))

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

for i in range(len(ch)):
    res = res - p[i] * (p[i]-1) //2
print(res+1)

Submission status: Accepted

Solution code:
from collections import defaultdict
s=input()
N=len(s)
D=defaultdict(int)
for i in range(N):
  D[s[i]]+=1
ans=N*(N-1)//2
for d in D.values():
  ans-=d*(d-1)//2
print(ans+1)

Submission status: Accepted

Solution code:
import collections
A = input()
Ac = collections.Counter(A)
ans = len(A)*(len(A)-1)//2
for i in Ac.values():
    ans -= i*(i-1)//2

print(ans+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import Counter

s = list(input())
cnt = Counter(s)

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

print(int(ans))

Submission status: Accepted

Solution code:
from collections import defaultdict
A = input()
N = len(A)
ans = N * (N + 1) // 2
dic = defaultdict(int)
for i in range(N):
    dic[A[i]] += 1

for k in dic.values():
    ans -= k * (k + 1) // 2

print(ans + 1)

Submission status: Accepted

Solution code:
from collections import Counter

As = input()

lenA = len(As)
cnt = Counter(As)

ans = lenA * (lenA - 1) // 2
for c, num in cnt.items():
    ans -= num * (num - 1) // 2
print(ans + 1)

Submission status: Accepted

Solution code:
n = str(input())
lis = [0 for i in range(26)]
ans = 1
for i in range(len(n)):
  ans += i
  lis[ord(n[i])-ord("a")] += 1
for i in range(26):
  for k in range(lis[i]-1):
    ans -= (k+1)
print(ans)

Submission status: Accepted

Solution code:
a=raw_input()
cnt=[0]*26
for ch in a:
	cnt[ord(ch)-ord('a')]+=1
result=1+len(a)*(len(a)-1)/2
for i in range(26):
	result-=cnt[i]*(cnt[i]-1)/2
print result

Submission status: Accepted

Solution code:
from collections import Counter

a = input()
n = len(a)
c = Counter()
ans = 1
c[a[0]] = 1
for i in range(1, n):
	ans += i - c[a[i]]
	c[a[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
import collections
import math

def C(n, r):
    if n == 0 or r == 0: return 1
    return C(n, r-1) * (n-r+1) // r 

a = list(input())
n = len(a)
aa = collections.Counter(a)
ans = C(n,2)
for i in aa:
    if aa[i] >= 2:
        ans += -C(aa[i],2)
print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

aa = list(input())
N = len(aa)
cnt = [0]*26

for a in aa:
    cnt[ord(a)-97] += 1

ans = N*(N-1)//2+1
for n in cnt:
    ans -= n*(n-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
a = input()
n = len(a)
cnt = Counter(a)
print(n * (n - 1) // 2 - sum(v * (v - 1) // 2 for k, v in cnt.items()) + 1)

Submission status: Accepted

Solution code:
from collections import Counter

s=input()
n=len(s)
v=n*(n-1)//2
for i in Counter(s).values():
  v-=i*(i-1)//2
print(v+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(A):
    c = Counter(A)
    N = len(A)
    s = 0
    for v in c.values():
        s += v*(v-1)//2

    return N*(N-1)//2 - s + 1


def main():
    A = read_str()
    # A = '0'*20000
    print(slv(A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
cnt=[0]*200

ans=1

for i,c in enumerate(input()):
	ans+=i-cnt[ord(c)]
	cnt[ord(c)]+=1

print(ans)

Submission status: Accepted

Solution code:
cnt={}
tot=0

ans=1

for i,c in enumerate(input(),0):
	if c in cnt:
		ans+=i-cnt[c]
	else:
		cnt[c]=0
		ans+=i
	cnt[c]+=1

print(ans)

Submission status: Accepted

Solution code:
S = input()
gou = 1
N = len(S)
for i in range(N):
  gou += i
import collections
c = collections.Counter(S)
c = c.items()
for a,b in c:
  gou -= b*(b-1)/2
print(int(gou))

Submission status: Accepted

Solution code:
A = input()
N = len(A)
L = [0] * 26
for a in A:
    L[ord(a)-97] += 1
ans = N*(N-1)//2 + 1
for l in L:
    if l >= 2:
        ans -= l*(l-1)//2
print(ans)

Submission status: Accepted

Solution code:
s = input().rstrip()
total = 0
for i in range(ord('a'),ord('z')+1):
  tmp = s.count(chr(i))
  total += tmp*(tmp-1)//2
print(1 + len(s)*(len(s)-1)//2 - total)

Submission status: Accepted

Solution code:
def read():
    return int(input())


def readlist():
    return list(map(int, input().split()))


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


A = input()
L = len(A)


def ctoi(char):
    return ord(char) - 97


cnt = [0] * 26

for a in A:
    cnt[ctoi(a)] += 1

ans = 1 + (L * (L - 1)) // 2
for n in cnt:
    ans -= (n * (n - 1)) // 2

print(ans)

Submission status: Accepted

Solution code:
ca = ord('a')
A = [ord(c) - ca for c in input()]

cnts = [0]*26
ans = 1
for i, a in enumerate(A):
    ans += i - cnts[a]
    cnts[a] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
a = input()
c,ans = Counter(a),1+len(a)*(len(a)-1)//2
for i in c.most_common():
	if i[1]==1: break
	else: ans -= i[1]*(i[1]-1)//2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys, math, copy
# import fractions, itertools
# import numpy as np
# import scipy

HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    a = input()
    n = len(a)
    di = {c: 0 for c in ABC}
    for c in a:
        di[c] += 1
    invdi = {c: n - di[c] for c in ABC}
    res = 0
    for c in ABC:
        res += di[c] * invdi[c]
    assert res % 2 == 0
    res //= 2
    res += 1
    print(res)


main()

Submission status: Accepted

Solution code:
s = input()
x = 1
d = {}
for i, c in enumerate(s):
    k = d.get(c, 0)
    d[c] = k + 1
    x += (i - k)
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A=input()
N=len(A)
B=[]
C=[]
for i in range(N):
    flag=0
    for j in range(len(B)):
        if B[j]==A[i]:
            C[j]+=1
            flag=1
            break
    if flag==0:
        B.append(A[i])
        C.append(1)
M=((N*(N-1))//2)+1
for i in C:
    M -= (i*(i-1))//2
print(M)

Submission status: Accepted

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

m = 1
for i in range(2,len(A)+1):
    m += len(A) - i + 1

for i in A_dic.values():
    m -= (i*(i-1))/2

print(int(m))

Submission status: Accepted

Solution code:
from collections import Counter
A=input()
n=len(A)
C=Counter(A)
tmp=0
for v in C.values():
    tmp+=v*(v-1)//2
print(n*(n-1)//2-tmp+1)

Submission status: Accepted

Solution code:
A=input()

dict={}

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

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

for k,v in dict.items():
  ans-=(v*(v-1)/2)

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n=list(input())
l=len(n)
a=l*(l-1)/2

counter = Counter(n)
for w, c in counter.most_common():
  a -= c*(c-1)/2
print(int(a+1))