Problem:


Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
3
acornistnt
peanutbomb
constraint


Sample output:
1

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from collections import defaultdict
dic = defaultdict(int)

N = int(readline())
for i in range(N):
  s = readline().rstrip()
  dic["".join(sorted(s))] += 1

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

Submission status: Accepted

Solution code:
n=int(input())
mojilist=[]
for i in range(n):
	tmps=sorted(input())
	mojilist.append("".join(tmps))

mojilist.sort()
tmp=""
count=1
sum=0
for moji in mojilist:
	if moji == tmp:
		count += 1
	else:
		sum += count*(count-1)//2
		count=1
		tmp=moji

sum += count*(count-1)//2
print(sum)

Submission status: Accepted

Solution code:
from scipy.special import comb

N = int(input())
sdict = {}
for i in range(N):
    inp = ''.join(sorted(list(input())))
    if inp not in sdict:
        sdict.update({inp:1})
    else:
        sdict[inp] += 1

ans = 0
for key in sdict:
    ans += comb(sdict[key], 2 , exact=True)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
ans = 0
for i in range(n):
    a = input()
    s.append(list(a))
for i in range(n):
    s[i].sort()
s.sort()
anslist = []
p = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        p += 1
    else:
        anslist.append(p)
        p = 0
anslist.append(p)
for i in range(len(anslist)):
    ans += anslist[i]*(anslist[i]+1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
s=[input() for i in range(n)]
cnt=Counter()
ans=0
for j in s:
    a=''.join(sorted(list(j)))
    if a in cnt:
        ans+=cnt[a]
    cnt[a]+=1
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
S = [input() for _ in range(N)]
S = [''.join(sorted(x)) for x in S]
S = collections.Counter(S)

ans = [x*(x-1)//2 for x in S.values()]
print(sum(ans))

Submission status: Accepted

Solution code:
import itertools
import collections
N = int(input())
S = [[str(x) for x in input()] for _ in range(N)]
S = [''.join(sorted(x)) for x in S]

cnt=0
reg = collections.defaultdict(int)
for s in S:
    reg[s] += 1
    if reg[s] > 1:
        cnt += reg[s] - 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
Dict = {}
ans = 0
tmp_list = []
for i in range(n):
    tmp = list(input())
    tmp_list.append(tmp)
tmp_list.reverse()
for i in range(n):
    tmp = tmp_list[i]
    tmp = sorted(tmp)
    tmp = ''.join(tmp)
    if tmp not in Dict.keys():
        Dict[tmp] = 1
    else:
        ans += Dict[tmp]
        Dict[tmp] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())

res =0
cd = {}

for i in range(N):
    w = "".join(sorted(input()))
    if cd.get(w, 0):
        res += cd[w]
        cd[w] += 1
    else:
        cd[w] = 1
print(res)

Submission status: Accepted

Solution code:
n = int(input())
arr = []
for i in range(n):
    s = list(str(input()))
    s.sort()
    k = ''
    for i in range(10):
        k += s[i]
    arr.append(k)
arr.sort()
ans = 0
count = 1
for i in range(n - 1):
    if arr[i] == arr[i + 1]:
        count += 1
        if i == n - 2:
            ans += count * (count - 1) // 2
    else:
        ans += count * (count - 1) // 2
        count = 1
print(ans)

Submission status: Accepted

Solution code:
def count(data):
    dic = {}
    for d in data:
        if d in dic:
            dic[d] += 1
        else:
            dic[d] = 1
    return dic

n = int(input())
counts = {}
for _ in range(n):
    s = ''.join(sorted(input()))
    if s in counts:
        counts[s] += 1
    else:
        counts[s] = 1

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

Submission status: Accepted

Solution code:
import sys
import math
import bisect

def main():
    d = dict()
    for _ in range(int(input())):
        s = ''.join(sorted(input()))
        if s not in d:
            d[s] = 0
        d[s] += 1
    ans = 0
    A = list(d.values())
    for a in A:
        val = a * (a - 1) // 2
        ans += val
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
A = defaultdict(int)
for _ in range(N):
    c = Counter(list(input()))
    d = sorted(c.items(),key= lambda x:x[0])
    A[tuple(d)] += 1

SUM = 0
for v in A.values():
    if v > 1:
        SUM += combinations_count(v,2)

print(SUM)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
l=[''.join(sorted(input())) for _ in range(n)]
ans=0
for i in Counter(l).values():
  ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
import math
def combination(n,r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N=int(input())
mem={}
for i in range(N):
    s=list(input())
    s.sort()
    s=''.join(s)
    if s in mem: mem[s]+=1
    else: mem[s]=0
    count=0
for i in mem.values():
    if i!=0: count += combination(i+1,2)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
lst = []
for i in range(n):
  s = list(input())
  s = sorted(s)
  lst.append(s)
lst = sorted(lst)
lst.append(["dummy"])

sum_ = 0
count = 1
for i in range(1, n+1):
  if lst[i] == lst[i-1]:
    count += 1
  else:
    sum_ += count * (count-1)//2
    count = 1
    
print(sum_)

Submission status: Accepted

Solution code:
n = int(input())
Dict = {}
ans = 0
tmp_list = []
for i in range(n):
    tmp = list(input())
    tmp_list.append(tmp)
tmp_list.reverse()
for i in range(n):
    tmp = tmp_list[i]
    tmp = sorted(tmp)
    tmp = ''.join(tmp)
    if tmp not in Dict.keys():
        Dict[tmp] = 1
    else:
        ans += Dict[tmp]
        Dict[tmp] += 1


print(ans)

Submission status: Accepted

Solution code:
from math import factorial

def unique(w):
    d = {}
    for c in w:
        if c in d.keys():
            d[c] += 1
        else:
            d.setdefault(c, 1)
    return list(d.values())

n = int(input())
s = [0] * n
for i in range(n):
    s[i] = input()
# s = sorted([''.join(sorted(list(t))) for t in s])
s = [''.join(sorted(list(t))) for t in s]
values = unique(s)
count = 0
for i in values:
    if i > 1:
        count += factorial(i) // factorial(2) // factorial(i - 2)
print(count)

Submission status: Accepted

Solution code:
from collections import Counter


n=int(input())
S = []
for i in range(n):
    S.append(''.join(sorted(input())))

cnt = Counter(S)
 
ans = 0
for num in cnt.values():
    ans += num*(num-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
S = [str(sorted(input())) for _ in range(n)]

print(sum(map(lambda x: x * (x - 1) // 2, Counter(S).values())))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
Sss = [''.join(sorted(input().rstrip())) for _ in range(N)]

cnt = Counter(Sss)
#print('cnt:', cnt)

ans = 0
for num in cnt.values():
    ans += num*(num-1)//2
print(ans)

Submission status: Accepted

Solution code:

dic={}
n=int(input())
for i in range(n):
  s=input()
  s=''.join(sorted(str(s)))
  if(s in dic):              
    dic[s]+=1
  else:
    dic[s]=1

ans=0
for x in dic.values():
  ans+=int(x*(x-1)/2)
print(ans)
# print(dic)

Submission status: Accepted

Solution code:
import collections
n = int(input())
S = ["".join(sorted(input())) for _ in range(n)]
C = collections.Counter(S)
ans = [(v * (v - 1) // 2) for v in C.values() if v >= 2]
print(sum(ans))

Submission status: Accepted

Solution code:
n = int( input() )
s = []
for _ in range( n ):
    s.append( input() )

for i in range( n ):
    s[i] = "".join( sorted( s[i] ) )

found = {}
cnt = 0
for s_i in s:
    if s_i in found:
        found[ s_i ] = found[ s_i ] + 1
    else:
        found[ s_i ] = 0

for key in found:
    c = found[key]
    cnt = cnt + c * ( c + 1 ) // 2

print( cnt )

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
s = [''.join(sorted(input())) for _ in range(N)]

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

print(ans)

Submission status: Accepted

Solution code:
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque

#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp
		


n=input1()
s=[]
for i in range(n):
	s.append(str(input()))
str_dict={"".join(sorted(s[0])):0}
count=0
for i in range(1,n):
	now_d="".join(sorted(s[i]))
	if now_d in str_dict:
		str_dict[now_d]+=1
		count+=str_dict[now_d]
	else:
		str_dict[now_d]=0
print(count)

Submission status: Accepted

Solution code:
N = int(input())
from collections import Counter
S = [''.join(sorted(input())) for _ in range(N)]
ans = 0
for i in Counter(S).values():
    ans += i*(i-1)//2 
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
sd = {}
cnt = 0
for i in range(N):
    str = ','.join(sorted(input()))
    if str in sd:
        sd[str] += 1
        cnt += sd[str]
    else:
        sd[str] = 0
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
sin = [''.join(sorted(input())) for _ in range(n)]

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
ss = []
for _ in range(n):
    ss.append(sorted(input().strip()))
ss.sort()
now = ss[0]
ans = 0
cnt = 1
for i in range(1, n):
    if ss[i] == now:
        cnt += 1
    else:
        if cnt != 1:
            ans += cnt * (cnt - 1) // 2
        now = ss[i]
        cnt = 1
if cnt != 1:
    ans += cnt * (cnt - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
from collections import Counter
S = [''.join(sorted(input().rstrip())) for _ in range(N)]
ans = 0
for i in Counter(S).values():
    ans += i*(i-1)//2 
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=[str(sorted(input())) for i in range(n)]
c=collections.Counter(s)
print(sum(v*(v-1)//2 for v in c.values()))

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(sorted(input()))
s.sort()

ans = 0
cnt = 1 # for combination

for j in range(N-1):
    if s[j] == s[j+1]:
        cnt += 1
    else:
        ans += cnt * (cnt - 1) // 2
        cnt = 1 # reset
ans += cnt * (cnt -1) // 2
print(ans)

Submission status: Accepted

Solution code:
# C - Green Bin

from collections import Counter

N = int(input())
S = [str(input()) for _ in range(N)]
for i in range(N):
    S[i] = ''.join(sorted([s for s in S[i]]))
counter = Counter(S)
ans = 0
for i in counter.values():
    tmp = 0
    for j in range(1, i):
        tmp += j
    ans += tmp

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(sorted(input()))
s.sort()

ans = 0
tmp = 1
for i in range(N-1):
    if s[i] == s[i + 1]:
        tmp += 1
    else:
        ans += tmp * (tmp-1) // 2
        tmp = 1
ans += tmp * (tmp-1) // 2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
dic={}
ans=0
for i in range(N):
    s=list(input())
    s.sort()
    S=''.join(s)
    if S not in dic:
        dic[S]=1
    else:
        dic[S]+=1
for v in dic.values():
    if v>=2:
        ans+=v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
T = [S() for _ in range(n)]
S = []
for t in T:
    S.append("".join(sorted(t)))

ans = 0
tmp = Counter(S)
for k, v in tmp.items():
    if v > 1:
        ans += int(v*(v-1)//2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0
A = []
for _ in range(n):
    t = sorted(input())
    t = "".join(t)
    A.append(t)

import collections
C = collections.Counter(A)
lC = list(C.values())
for i in lC:
    cnt += i * (i-1) // 2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
D=dict()
for i in range(n):
	s= input()
	s=sorted(s)
	S=''.join(s)
	if S in D:
		D[S]+=1
	else:
		D[S]=1

cnt=0
for s in D:
	cnt+=D[s]*(D[s]-1)//2
print(cnt)

Submission status: Accepted

Solution code:
#137-c
n = int(input())
buf = []

for i in range(n):
    s  = input()
    l = list(s)
    l.sort()
    sort_s = "".join(l)
    buf.append(sort_s)

buf.sort()    
ans = 0
same = 1
s = buf[0]
for i in range(1,n):
    if buf[i]==s:
        same += 1
    else:
        ans += same*(same-1)//2
        same = 1
        s = buf[i]

ans += same*(same-1)//2
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
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
d = defaultdict(int)
for i in range(n):
    tmp = list(input())
    tmp.sort()
    tmp = tuple(tmp)
    d[tmp] += 1
res = 0
for key in list(d):
    v = d[key]
    res += v*(v-1)//2
print(res)

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 # 一行ずつ全読み込み
sys.setrecursionlimit(10**7) # 再帰限界変更（デフォルトは1000）

n=int(input())
s_lis=[''.join(sorted(input().rstrip())) for _ in range(n)]

counter = Counter(s_lis) # 同一文字列の出現回数リスト取得
 
answer = sum(x*(x-1)//2 for x in counter.values())
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input() for _ in range(n))
d={}

res=0
for c in s:
    c=sorted(c)
    c=''.join(c)
    if c not in d:
        d[c]=1
    else:
        d[c]+=1
for i in d.values():
    res+=(i*(i-1))/2
print(int(res))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    # 文字列を一文字ずつのリストにして、辞書順にソートしたあと、文字列に戻す
    # ['aab','aab','aab','ccd','ccd']ってなってる
    l=[''.join(sorted(list(input()))) for i in range(n)]

    from collections import Counter
    c=Counter(l)# ()にカウンターの対象のリストの変数名
    # print(c)するとCounter({'aab': 3, 'ｃｃｄ': 2})ってなってる
    # print(c.values())すると[3,2]
    ans=0
    for j in c.values():
        ans+=(j*(j-1))//2
    print(ans)
resolve()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

s = []
for _ in [0]*n:
  s.append("".join(sorted(list(input()))))

c = Counter(s)

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

Submission status: Accepted

Solution code:

from collections import Counter

N = int(input())
S = [''.join(map(str,(sorted(str(input()))))) for i in range(N)]
counter = Counter(S)

answer = sum(x*(x-1)//2 for x in counter.values())
print(answer)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    s=[sorted(list(input())) for i in range(n)]
    s.sort()
    ans=0
    cnt=1
    for i in range(n-1):
        if s[i]==s[i+1]:
                cnt+=1
                if i ==n-2:
                    ans+=cnt*(cnt-1)//2
        else:
            if cnt>=2:
                ans += cnt * (cnt - 1) // 2
                cnt=1
    print(ans)
resolve()

Submission status: Accepted

Solution code:
from math import factorial
n = int(input())

dic={}
for i in range(n):
    s=''.join(sorted(list(input())))
    dic[s]=dic.get(s,0)+1

ans=0
for key,value in dic.items():
    if value<2:
        continue
    ans+=factorial(value)//(factorial(2)*factorial(value - 2))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
for i in range(n):
    s[i] = sorted(s[i])
s.sort()
result = 0
count = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        result += (count*(count-1))//2
        count = 1
result += count*(count-1)//2
print(result)

Submission status: Accepted

Solution code:
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(input())
d = {}

res = 0
for i in range(N):
    s = "".join(sorted(sys.stdin.readline().strip()))
    if s in d:
        d[s] += 1
    else:
        d[s] = 1

for dd in d.values():
    res += dd * (dd - 1) // 2

print(res)

Submission status: Accepted

Solution code:
n=int(input())
sd={}
for i in range(n):
    si=''.join(sorted(list(input())))
    sd[si]=sd.get(si, 0)+1

ans=0
for v in sd.values():
    if(v>1):
        ans+=((v*(v-1))//2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque, OrderedDict
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    n = int(input())
    d = [''.join(sorted(list(input()))) for _ in range(n)]
    dd=Counter(d)
    cnt=0
    for i in dd.values():
        cnt += i*(i-1)//2
    print(cnt)




resolve()

Submission status: Accepted

Solution code:
from sys import stdin
def nC2(n):
    return n*(n-1)//2

def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    s=[""]*N
    for i in range(N):
        a=list(readline().strip())
        a.sort()
        s[i]="".join(a)

    d=dict()
    for i in range(N):
        if s[i] not in d:
            d[s[i]]=1
        else:
            d[s[i]]+=1

    res=0
    for v in d.values():
        if v==1:
            pass
        else:
            res+=nC2(v)

    print(res)
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

n = int(input())

from collections import defaultdict

d = defaultdict(int)

for i in range(n):
    s = list(input().rstrip())
    s.sort()
    d["".join(s)] += 1

ans = 0

for i in d.keys():
    ans += d[i]*(d[i] - 1)//2

print(ans)

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N=int(input())
    S=[input().rstrip() for _ in range(N)]
    from collections import Counter,defaultdict
    from string import ascii_lowercase
    d=defaultdict(int)
    for s in S:
        C=Counter(s)
        t=0
        for i,a in enumerate(ascii_lowercase):
            t+=C[a]*(11**i)
        d[t]+=1
    ans=0
    for v in d.values():
        ans+=v*(v-1)//2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = Counter([''.join(sorted(input())) for _ in range(n)]).values()
print(sum([i*(i-1)//2 for i in s if i != 1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = Counter(["".join(sorted(input())) for _ in range(n)])

ans = 0
for x in s.values():
  if x == 1: continue
  ans += (x*(x-1))//2
print(ans)

Submission status: Accepted

Solution code:
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
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 printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()
n=I()
lis=["".join(sorted(list(input().rstrip()))) for i in range(n)]
c=Counter(lis)
#print(c)
ans=0
for j in c:
    ans+=(c[j]*(c[j]-1))//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

L = []
for i in range(N):
    x = list(input())
    x.sort()
    x = "".join(x)
    L.append(x)
    
from collections import Counter
L = Counter(L)
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

ans = 0
for i in L:
    a = L[i]
    if a > 1:
        ans += combinations_count(a,2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

dic = {}
for i in range(n):
    s = input()
    lst = [0 for j in range(30)]
    for j in range(10):
        lst[ord(s[j])-ord('a')] += 1
    string = ''.join(map(str, lst))
    if string in dic:
        dic[string] += 1
    else:
        dic[string] = 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

from collections import defaultdict

N = int(input())

S = [list(input()) for _ in range(N)]

for i in range(N):
    S[i].sort()

S.sort()
ans = 0
count = 1
for i in range(1, N):
    if S[i - 1] == S[i]:
        count += 1
    else:
        ans += count * (count - 1) // 2
        count = 1

ans += count * (count - 1) // 2
print (ans)

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

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

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

def main():
    ana = defaultdict(int)
    n = int(input())
    for i in range(n):
        s = "".join(sorted(input()))

        ana[s] += 1
    ans = 0
    for k,v in ana.items():
        ans += v*(v-1)//2
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
C = {}
for _ in range(N):
    s = "".join(sorted(list(input().strip())))
    if s not in C:
        C[s] = 0
    C[s] += 1
cnt = 0
for s in C:
    n = C[s]
    cnt += (n*(n-1))//2
print(cnt)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = ['' for _ in range(n)]
    for i in range(n):
        s[i] = input()

    appearance = dict()
    for i in range(n):
        s[i] = tuple(sorted(s[i]))
        if s[i] not in appearance:
            appearance.update({s[i]: 1})
        else:
            appearance[s[i]] += 1

    ans = 0
    for _, x in appearance.items():
        ans += (x * (x - 1)) // 2

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())

D={}

for i in range(N):
    s=input()
    s="".join(sorted(s))
    if s not in D:
        D[s]=1
    else:
        D[s]+=1
ans=0
for v in D.values():
    ans+=v*(v-1)//2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
D = {}
ans = 0
for i in range(n):
    s = str(input())
    d = [0]*26
    for j in range(len(s)):
        c = ord(s[j])-ord('a')
        d[c] += 1
    d = tuple(d)
    if d in D:
        ans += D[d]
        D[d] += 1
    else:
        D[d] = 1
print(ans)

Submission status: Accepted

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

N = int(input())
cnt = defaultdict(int)
ans = 0

for _ in range(N):
    s = input()[:-1]
    s = ''.join(sorted(list(s)))
    ans += cnt[s]
    cnt[s] += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
def f(s):
  return "".join(sorted(list(s)))
s=[f(input())for _ in range(n)]
from collections import Counter
d=Counter(s)
ans=0
for i in d:ans+=d[i]*(d[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
s = []
for _ in range(N):
    tmp = input()
    tmp = "".join(sorted(tmp))
    s.append(tmp)

ans = 0
for x in Counter(s).values():
    ans += (x*(x-1))//2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict

memo = defaultdict(int)

N = int(input())
for _ in range(N):
    s = sorted(input())
    s = "".join(s)
    memo[s] += 1

ans = np.array(list(memo.values()))
tmp = ans - 1
ans = (ans*tmp)/2

print(int(ans.sum()))

Submission status: Accepted

Solution code:
import numpy as np
import sys
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

N = int(input())
S = []
[S.append(input()) for _ in range(N)]

ans = defaultdict(int)

for s in S:
    key = "".join(sorted(s))
    ans[key] += 1

sum = 0

for a in ans:
    sum += (  (ans[a]) *( ans[a] - 1 ) ) // 2

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
a=set()
b=[]
import collections
for i in range(n):
    s=input()
    s=sorted(s)
    a.add("".join(s))
    b.append("".join(s))
ans=0
a=list(a)
c=collections.Counter(b)
for i in range(len(a)):
    ans+=c[a[i]]*(c[a[i]]-1)//2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=["".join(sorted(input())) for i in range(N)]
s.sort()
ans=0
t=0
for i in range(N):
  if s[t]!=s[i]:
    ans+=(i-t)*(i-t-1)//2
    t=i
i+=1
ans+=(i-t)*(i-t-1)//2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
cnt = dict()
output = 0

for i in range(N):
    string = ''.join(sorted(input()))
    cnt.setdefault(string, 0)
    output += cnt[string]
    cnt[string] += 1

print(output)

Submission status: Accepted

Solution code:
N=int(input())

from collections import Counter

ans=[]
d={}
cnt=0
for i in range(N):
    s=list(input())
    s=sorted(s)
    
    s= ''.join(s)

    if s in d:
        d[s]+=1
    else:
        d[s]=1

total=0

for _,value in d.items():
    if value==1:
        continue
    else:
        total +=value*(value-1)/2

print(int(total))

Submission status: Accepted

Solution code:
N = int(input())
S = [None] * N
for i in range(N):
    S[i] = input()

C = []
for s in S:
    T = tuple(sorted(s))
    C.append(T)

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

N = int(input())
dic = []
ans = 0

for i in range(N):
  S = "".join(sorted(input()))
  dic.append(S)

dic.sort()

for i in range(N):
  S = dic[i]
  r = bisect_right(dic, S)
  ans += r-i-1


print(ans)

Submission status: Accepted

Solution code:
# 未完成
def solve():
    n = int(input())
    a = 0
    s = {}
    for i in range(n):
        t = ''.join(sorted(input()))
        b = s.get(t, 0)
        a += b  # k個の同じものから2個選ぶからkC2だが0~(k-1)の総和と同地
        s[t] = b+1
    print(a)


solve()

Submission status: Accepted

Solution code:
N=int(input())
dic={}
for i in range(N):
  s=sorted(list(input()))
  s="".join(s)
  if s in dic:
    dic[s]+=1
  else:
    dic[s]=1
count=0
u=len(dic)
for i in range(u):
  kk=dic.popitem()
  count+=kk[1]*(kk[1]-1)/2
print(int(count))

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
s.sort()
ans = 0
t = 0
for i in range(1,n):
    if s[i-1]==s[i]:
        t += 1
        if i==n-1:
            ans += t*(t+1)/2
    else:
        if t>0:
            ans += t*(t+1)/2
            t = 0

print(int(ans))