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:
n = int(input())
tatelist = [input() for i in range(n)]

jlist = [sorted(tatelist[i]) for i in range(n)]
klist = ["".join(jlist[i]) for i in range(n)]

kdic = {}

count = 0
for i in range(n):
    if klist[i] in kdic.keys():
        count += kdic[klist[i]]
        kdic[klist[i]] += 1

    else:
        kdic[klist[i]] = 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
ssorted = []
for i in range(N):
    ssorted.append("".join(sorted(list(input()))))
sdict = {}
for i in ssorted:
    if i in sdict:
        sdict[i] += 1
    else:
        sdict[i] = 1
count = 0
for i in sdict:
    s = sdict[i]
    if s >= 2:
        count += s*(s-1)//2
print(count)

Submission status: Accepted

Solution code:
n = int(input())

x = 0
d = {}
for i in range(n):
  s = "".join(sorted(list(input())))
  
  if s in d:
    d[s] = d[s] + 1
    x += d[s]
  else:
    d[s] = 0

print(x)
    
#同じsが複数回出るとだめ
#s = ["".join(sorted(list(input()))) for i in range(n)]
#u = list(set(s))
#print(n - len(u))

Submission status: Accepted

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

N=int(input())
dic={}
sum=0
for i in range(N):
    s=list(input())
    s.sort()
    s = "".join(s)
    if s in dic:
        sum+=dic[s]
        dic[s]+=1
    else:
        dic[s]=1
print(sum)

Submission status: Accepted

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

count = 0
t = dict()
for i in range(N):
    if S[i] in t:
        t[S[i]] += 1
    else:
        t[S[i]] = 1
ans = 0
for key in t:
    ans += (t[key]*(t[key]-1)) // 2


print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
sorted_s_list = defaultdict(int)
count = 0
s_list = []
for _ in range(N):
    s = str(sorted(list(input())))
    if sorted_s_list[s] > 0:
        count += sorted_s_list[s]
        sorted_s_list[s] += 1
    else:
        sorted_s_list[s] = 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]

# sort
for i in range(N):
    S[i] = "".join(sorted(S[i]))

# main
t = dict()
for i in range(N):
    if S[i] in t:
        t[S[i]] += 1
    else:
        t[S[i]] = 1

print(sum(t[key]*(t[key] - 1) // 2 for key in t))

Submission status: Accepted

Solution code:
n=int(input())
dic={}
sum=0
for i in range(n):
    s=list(input())
    s.sort()
    s="".join(s)
    if s in dic:
        sum+=dic[s]
        dic[s]+=1
    else:
        dic[s]=1
print(sum)

Submission status: Accepted

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

# ソートしたsリスト作成（ss）
ss = ['']*n
for i in range(n):
    ss[i] = ''.join(sorted(s[i]))
    
# ssのsetを作成して重複要素削除(sss)
sss = list(set(ss))

# sssから辞書作成
dict_ss = {}
for i in range(len(sss)):
    dict_ss[sss[i]] = 0

# ssにおいて「sssの要素に合致する要素」の検索＆個数計上
for i in range(n):
    dict_ss[ss[i]] += 1

# 計上した個数のリストを作成
# その個数から2個の組み合わせを計算（n C 2）、足し合わせてans
ans_l = [dict_ss[sss[i]] for i in range(len(sss))]
ans = 0
for i in range(len(ans_l)):
    ans += (ans_l[i]*(ans_l[i]-1))/2

print(int(ans))

Submission status: Accepted

Solution code:
from math import factorial

def main():
    n, *s = open(0).read().split()

    exist_s = {}
    for cur_s in s:
        cur_s = ''.join(sorted(list(cur_s)))
        if cur_s in exist_s:
            exist_s[cur_s] += 1
        else:
            exist_s[cur_s] = 1

    ans = 0
    for val in exist_s.values():
        if val < 2:
            continue
        ans += factorial(val) // (factorial(val - 2) * factorial(2))

    print(ans)
    return()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

# nC2を足す理由は、同一文字列からアナグラムとなっている2つの文字列を見つけないといけないから
N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
# ソートした時にそれぞれの文字がリストに分割されるからjoinで文字列に直す
c = collections.Counter(s)
ans = 0
for i in set(s):
    n = c[i]
    ans += n * (n - 1) // 2
print(ans)

# set(s)によって、同じ文字列は消去される。そしてcによって、何回その文字が出ているかを数えれる。

Submission status: Accepted

Solution code:
import collections
def counter_(s):
  counter = collections.Counter(s)
  counter =  [list(e) for e in counter.items()]
  counter.sort()
  return counter

from operator import mul
from functools import reduce
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
  
N = int(input())
S = []

for i in range(N):
  s = list(input())
  s.sort()
  str_ = ''.join(s)
  S.append(str_)

ans = 0
S_ = counter_(S)
for i in range(len(S_)):
  if(S_[i][1] != 1):
    a = cmb(S_[i][1],2)
    ans += a
print(ans)

Submission status: Accepted

Solution code:
def main():
  from collections import Counter
  from sys import stdin
  readline = stdin.readline
  N = int(readline())
  t = Counter([''.join(sorted(readline()[:-1])) for _ in range(N)])
  print(sum(t[k] * (t[k] - 1) // 2 for k in t))
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
S = list(input() for i in range(n)) 

ctr = Counter()
ans = 0
for s in S:
    s =  ''.join(sorted(list(s)))
    if s in ctr:
        ans += ctr[s]
    ctr[s] += 1 

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = ([''.join(sorted(input())) for _ in range(n)])
S = sorted(s)
D = {}#辞書
ans = 0
for i in S:
    #print('i:'+str(i))
    if i in D:
        #print('ii:'+str(i))
        D[i] += 1
        ans += D[i]
    else:
        D[i] = 0
#print(D)
print(ans)

Submission status: Accepted

Solution code:
n= int(input())
l = []
for _ in range(n):
    l.append(''.join(sorted(input())))

d = dict()
cnt = 0
for e in l:
    if e in d:
        d[e] += 1
        cnt += d[e]
    else:
        d[e] = 0

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

cs = Counter(s)

count = 0
for i in cs.values():
    count += ((i * (i-1)) // 2)
    
print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    
    S = sorted(sorted(input()) for _ in range(N))

    sol = 0
    current = 1

    for i, s in enumerate(S[1:], start=1):
        if s == S[i - 1]:
            current += 1
        else:
            sol += (current * (current - 1)) // 2
            current = 1

    sol += (current * (current - 1)) // 2
    print(sol)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

L = []

for i in range(0,len(S)):
	L.append(sorted(S[i]))

for i in range(0,len(L)):
	L[i] = ",".join(L[i])

dicL = dict()
answer = 0

for l in L:
	if l in dicL:
		dicL[l] += 1
		answer += dicL[l]
	else:
		dicL[l] = 0
		
print(answer)

Submission status: Accepted

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

D = {}
ans = 0
for s in S:
    if s in D:
        D[s] += 1
        ans += D[s]
    else:
        D[s] = 0

print(ans)

Submission status: Accepted

Solution code:
import operator as op
from functools import reduce

def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom

def main(): 
    n = int(input())
    s = []
    dic = {}
    ans = 0
    for i in range(n):
        b = ""
        a = sorted(input())
        for j in range(len(a)):
            b += str(a[j])
        s.append(b)
        if b in dic:
            dic[str(b)] = dic[str(b)] + 1
        else:
            dic[str(b)] = 1

    for key in dic:
        if dic[key] > 1:
            ans += ncr(dic[key], 2)
    
    print(int(ans))
    # print(dic)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
lst = [input() for _ in range(n)]

dic = dict()
res = 0
for i in range(n):
    sorted_string = "".join(sorted(lst[i]))
    if sorted_string in dic:
        dic[sorted_string] += 1
        res += dic[sorted_string]
    else:
        dic[sorted_string] = 0
    
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
dict = {}
count = 0
for word in s:
    sorted_word = sorted(word)
    sorted_word = "".join(sorted_word)
    if sorted_word not in dict:
        dict[sorted_word] = 0
    else:
        dict[sorted_word] += 1
        count += dict[sorted_word]
print(count)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    n=int(input())
    A=[0]*n
    for i in range(n):
        A[i]=''.join(sorted(input()))
    A.sort()
    from itertools import groupby
    ans=0
    for k,v in groupby(A):
        a=len(list(v))
        if a>=2: ans+=a*(a-1)//2
    print(ans)

resolve()

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    S = {}
    cnt = 0
    for _ in range(N):
        s = list(input())
        s.sort()
        s = "".join(s)
        if s not in S:
            S[s] = 0
        else:
            S[s] += 1
            cnt += S[s]
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
b = {}
ans = 0
for _ in range(n):
  x = list(input())
  x.sort()
  x = "".join(x)
  if not x in b:
    b[x] = 0
  else:
    b[x] += 1
    ans += b[x]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s_list = []
s_dict = {}
ans = 0

for j in range(N):
    s_list.append("".join(sorted(input())))

for i in range(N):
    if s_list[i] in s_dict.keys():
        ans += s_dict[s_list[i]]
        s_dict[s_list[i]] += 1

    else:
        s_dict[s_list[i]] = 1

print(ans)

Submission status: Accepted

Solution code:
#a,b=map(int,input().split())
#p_3,p_4=map(int,input().split())
n=int(input())
#g=input()
#recipe=[list(input().split())for _ in range(n)]
#m=int(input())
#have=[list(input().split())for _ in range(m)]
s=[list(input())for _ in range(n)]
#h=list(map(int,input().split()))
#b=list(map(int,input().split()))
#c=list(map(float,input().split()))
#n=int(input())
#s=[list(map(int,input().split()))for _ in range(n)]
#s=[list(input().split())for _ in range(n)]
#s=[input()for _ in range(n)]
ans=[]
dic={}
cnt=0
#文字列をソートする
for i in range(n):
    s[i].sort()
    tmp="".join(s[i])
    ans.append(tmp)

#同じ文字列があるかを調べ、その数を数える
for i in range(n):
    if not ans[i]  in dic:
        dic[ans[i]]=1
    else:
        dic[ans[i]]+=1

for val in dic.values():
    cnt+=val*(val-1)//2
print(cnt)

Submission status: Accepted

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

n = INT()
strings = [[]*10 for i in range(n)]
for i in range(n):
    strings[i] = sorted(list(input()))

strings = sorted(strings)

succession = [0]*n
counter = 0
for i in range(1,n):
    if strings[i] == strings[i-1]:
        succession[i] = succession[i-1]+1
    if strings[i] != strings[i-1] and succession[i-1]>0:
        counter += (succession[i-1]+1)*succession[i-1]//2
    if i == n-1 and succession[i] >0:
        counter += (succession[i]+1)*succession[i]//2

print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

strings = {}

ans = 0

for _ in range(N):
    s = sorted(list(input()))
    join_s = ''.join(s)
    if join_s in strings:
        strings[join_s] += 1
        ans += strings[join_s]
    else:
        strings[join_s] = 0

print(ans)

Submission status: Accepted

Solution code:
#Green bin -C-
n=int(input())
s=[]
for i in range(n):
    f=list(input())
    f.sort()
    s.append(f)
s.sort()
k=0
a=1
t=0
for i in range(1,n):
    if s[k]==s[i]:
        a+=1
    else:
        t=t+(a*(a-1))//2
        a=1
        k=i
if s[n-1]==s[k]:
    t=t+(a*(a-1))//2
print(t)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(input()) for _ in range(n)]
dic={}
t=[0]*n
for i in range(n):
	s[i].sort()
	t[i]=str(s[i])
for i in range(n):
	if t[i] in dic:
		dic[t[i]]+=1
	else:
		dic[t[i]]=1
counts=list(dic.values())
count=0
for i in range(len(counts)):
	count+=(counts[i])*(counts[i]-1)/2
print(int(count))

Submission status: Accepted

Solution code:
from collections import Counter
import sys
n = int(sys.stdin.readline())
L = [''.join(sorted(sys.stdin.readline().strip())) for _ in range(n)]
ans = 0
L = Counter(L)
for num in L.values():
    ans += num*(num-1)//2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d={}
count=0
for i in range(n):
    s=tuple(sorted(input()))
    if s not in d:
        d[s]=0
    count+=d[s]
    d[s]+=1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
ctr = Counter()
answer = 0
for s in S:
    a = ''.join(sorted(list(s)))
    #print(a)
    if a in ctr:
        answer += ctr[a]
    ctr[a] += 1
print(answer)

Submission status: Accepted

Solution code:
N=int(input())
l=[]
d={}
ans=0
for i in range(N):
  s=list(str(input()))
  s.sort()
  l.append(s) 
for s in l:
  s=''.join(s)
  if s in d:
    ans+=d[s]
    d[s]+=1
  else:
    d[s]=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
words = []
dic = {}
count = 0

for _ in range(N):
    words.append(str(sorted(input())))

for word in words:
    if word in dic:
        count += dic[word]
        dic[word] += 1
    else:
        dic[word] = 1

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial

n = int(input())
s = []

for i in range(n):
    t = list(input())
    t.sort()
    s += [t]

for i in range(n):
    s[i] = ",".join(s[i])
    s[i] = s[i].replace(",","")
    
ans = 0

d = defaultdict(int)

for key in s:
    if key in d:
        ans += d[key]
    d[key] += 1
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L= []
for _ in range(N):
    l = sorted(list(input()))
    tmp = ''
    for x in l:
        tmp += x
    L.append(tmp)
L = sorted(L)

def counter(array):
    from collections import Counter
    return list(Counter(array).most_common())
c = counter(L)

def comb_cal(x):
    return x * (x - 1) / 2
ans = 0
for x in c:
    ans += comb_cal(x[1])
print(int(ans))

Submission status: Accepted

Solution code:
from math import factorial
from operator import mul
from functools import reduce

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


N = int(input())
D = {}
L = [[]for _ in range(N)]
count = 0
for i in range(N):
    L[i] = list(input())
    L[i].sort()
    L[i] = tuple(L[i])
    if L[i] in D:
        D[L[i]] = D[L[i]] + 1
        if D[L[i]] == 2:
            D[L[i]] = D[L[i]] + 1
    else:
        D[L[i]] = 0
for key in D:
    if D[key] > 2:
        count += cmb(D[key],2)
    else:
        count += D[key]

print(count)

Submission status: Accepted

Solution code:
import collections
N = int(input())
d = {}
ans = 0
for i in range(N):
    l = ' '.join(sorted(input()))
    if l in d:
        ans += d[l]
        d[l] += 1
    else:
        d[l] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = []
dic = {}
count = 0

for i in range(n):
    S.append(str(sorted(input())))
    
for Sstr in S:
    if Sstr in dic:
        count += dic[Sstr]
        dic[Sstr] += 1
    else:
        dic[Sstr] = 1
        

print(count)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
D={}
cnt=0
for i in range(N):
    A.append(str(sorted(input())))

for sortstr in A:
    if sortstr in D:
        cnt+=D[sortstr]
        D[sortstr]+=1
    else:
        D[sortstr]=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
dict = {}
ans = 0

for i in range(n):
  s = str(sorted(input()))
  if s in dict.keys():
    ans += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
S = ["".join(sorted(s)) for s in S]

cnt = 0
dic = {}

for w in S:
    if w in dic:
        cnt += dic[w]
        dic[w] += 1
    else:
        dic[w] = 1
        
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

map = {}
count = 0
for i in range(0, N):
    strings = list(input())
    strings.sort()
    key = "".join(strings)
    if not key in map:
        map[key] = 1
    else:
        count += map[key]
        map[key] += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())

d = {}
ans = 0
for i in range(n):
    list_s = sorted(list(input()))

    s = ""
    for j in list_s:
        s = s + j

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

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
sl = collections.Counter([str(sorted(input())) for _ in range(n)])

ret = 0
for _, v in sl.most_common():
    if 2 <= v:
        ret += v * (v - 1) // 2

print(ret)

Submission status: Accepted

Solution code:
N = int(input())

map = {}
count = 0
for i in range(0, N):
    strings = list(input())
    strings.sort()
    key = "".join(strings)
    if not key in map:
        map[key] = 1
    else:
        map[key] += 1

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

Submission status: Accepted

Solution code:
import math

import sys
import collections

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

ans = 0
for v in collections.Counter(a).values():
    for i in range(1, v):
        ans += i
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc137/tasks/abc137_c

import math

import sys
import collections

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

ans = 0
for v in collections.Counter(a).values():
    for i in range(1, v):
        ans += i
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=[0]*n
for i in range(n):
    s[i] = "".join(sorted(input()))
t=collections.Counter(s)
ans=0
for i in t.keys():
    ans+=t[i]*(t[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = [input() for i in range(n)]
t = []
for i in range(n):
    t.append("".join(sorted(s[i])))
ans = 0

x = collections.Counter(t).values()
for i in x:
    ans += i*(i-1)/2
print(round(ans))

Submission status: Accepted

Solution code:

n = int(input())
s = [input() for i in range(n)]
for k in range(n):
    s[k]=sorted(list(s[k]))
s=sorted(s)

c = 1
count = 0

for x in range (n-1):
        if s[x]==s[x+1]:
            c += 1
        else:
            if c>= 2:
                count += c*(c-1)//2
            c = 1
if c >=2:
    count += c*(c-1)//2
print(count)

Submission status: Accepted

Solution code:
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
s = [input() for i in range(n)]
for k in range(n):
    s[k]=sorted(list(s[k]))
s=sorted(s)

c = 1
count = 0

for x in range (n-1):
        if s[x]==s[x+1]:
            c += 1
        else:
            if c>= 2:
                count += cmb(c,2)
            c = 1
if c >=2:
    count += cmb(c,2)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
dict = {}
ans = 0

for i in range(n):
    s = tuple(sorted(input()))
    if s in dict.keys():
        ans += dict[s]
        dict[s] += 1
    else:
        dict[s] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dict = {}
ans = 0

for i in range(n):
    s = str(sorted(input()))
    if s in dict.keys():
        ans += dict[s]
        dict[s] += 1
    else:
        dict[s] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
res = 0
for i in range(n):
  s = ''.join(sorted(input()))
  if s in d:
    res += d[s]
    d[s] += 1
  else:
    d[s] = 1
print(res)

Submission status: Accepted

Solution code:
"""自分で考えたやつ

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

count = 0
for i in range(N):
    Si = S[i]
    for k in range(N):
        if i==k:
            None
        else:
            Sk = S[k]
            if Sk == Si:
                count += 1

print(count / 2)
"""

N = int(input())
dic = {}
ans = 0

for i in range(N):
    s = ''.join(sorted(input()))
    if s in dic:
        ans += dic[s]
        dic[s] += 1
    else:
        dic[s] = 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[]
count=0
ans=0
for i in range(N):
    s=sorted(input())
    S.append(s)
S.sort()
x=S[0]
for i in range(1,N):
    if(S[i]==x):
        count += 1
    else:
        ans=ans+(count+1)*count//2
        count=0
        x=S[i]
ans=ans+(count+1)*count//2
print(ans)

Submission status: Accepted

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

ans = 0
for i in seen:
  ans+= seen[i]*(seen[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
def kaijou(n):
  sum = 0
  for i in range(n):
    sum += i
  return sum

num = int(input())

alplist = []

for i in range(num):
  alplist.append(sorted(input()))
alplist.sort()
tmp = []
count = 1
sum = 0
for _alplist in alplist:
  if tmp == _alplist:
    count += 1
  else:
    sum += kaijou(count)
    count = 1
    tmp = _alplist
sum += kaijou(count)
print(sum)

Submission status: Accepted

Solution code:
import math

n = int(input())
S = [list(input()) for i in range(n)]
ans = 0
for s in S:
  s.sort()
  "".join(s)
S.sort()
S.append(0)
cnt = 0
for i in range(n):
  if S[i]==S[i+1] :
    cnt += 1
  else:
    cnt += 1
    ans += math.factorial(cnt)/(math.factorial(cnt-2)*2) if cnt >= 2 else 0
    cnt = 0
print(int(ans))

Submission status: Accepted

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

prev = ''
count = 0
res = 0

for s in S:
    if s == prev:
        count += 1
        res += count
    else:
        count = 0
    prev = s

print(res)

Submission status: Accepted

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

prev = ''
count = 0
res = 0

for s in S:
    if s == prev:
        count += 1
        res += count
    else:
        count = 0
    prev = s

print(res)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

n = int(input())
s = [str(input()) for _ in range(n)]
s = sorted(["".join(sorted(_)) for _ in [i for i in s]])
ans = 0
preb = str()
cnt = 1
for line in s:
  if preb == line:
    cnt += 1
  elif cnt >= 2:
    ans += cmb(cnt, 2)
    cnt = 1
  else:
    cnt = 1
  preb = line
if cnt >= 2:
    ans += cmb(cnt, 2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)
from collections import defaultdict


def main():
    N = int(input())
    A = [0]*N
    dic = defaultdict(int)
    for i in range(N):
        A[i] = "".join(sorted((input()[:-1])))
        dic[A[i]] +=1
    ans = 0
    for k in dic:
        if dic[k] >= 2:
            ans += dic[k]*(dic[k]-1)//2
    print(ans)






if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
ana = {}
answer = 0

for i in range(N):
    s = sorted(input())
    new_str = ''.join(s)
    flag = 0

    if new_str in ana.keys():
        ana[new_str] += 1
        answer += ana[new_str]
        flag = 1

    if flag == 0:
        ana[new_str] = 0

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
N = []
for i in range(n):
    N.append(input())
Nn = [sorted(j) for j in N]
Nn.sort()
Nh = []
for let in Nn:
    Nh.append(('').join(let))
c_dict = {}
for letters in Nh:
    if letters not in c_dict:
        c_dict[letters] = 1
    elif letters in c_dict:
        c_dict[letters] += 1
ans_list = []
for letter, num in c_dict.items():
    ans_list.append((num * (num-1))//2)
print(sum(ans_list))

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  l = []
  d = {}
  res = 0
  for i in range(n):
    s = input()
    l.append(''.join(sorted(s)))
  for j in range(n):
    if d.get(l[j]) is None:
      d[l[j]] = 1
    elif d.get(l[j]) > 0:
      d[l[j]] += 1
      res += d[l[j]] - 1
    else:
      print('error!')
      quit()
  
  return res
  
if __name__ == "__main__":
  res = main()
  print(res)

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial

N = int(input())
s = [input() for _ in range(N)]

for i in range(N):
  s[i] = list(s[i])
  s[i].sort()
  s[i] = "".join(s[i])
  
x = Counter(s).most_common()
cnt = 0
for i in range(len(x)):
  if x[i][1] >1:
    cnt += factorial(x[i][1]) // 2 // factorial(x[i][1] -2)
print(cnt)

Submission status: Accepted

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

cnt = {}

for s in S:
    if s in cnt.keys():
        cnt[s] += 1
    else:
        cnt[s] = 1

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

Submission status: Accepted

Solution code:
N = int(input())
s = sorted([list(sorted(input())) for _ in range(N)]) + [" "]
ans = 0
cnt = 1
for i in range(N):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        ans += cnt*(cnt-1)//2
        cnt = 1

print(ans)

Submission status: Accepted

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
n = I1()
d = defaultdict(int)
for i in range(n):
    d["".join(sorted(input()))]+=1
res=0
for i in d.keys():
    if d[i] > 1:
        yy=d[i]-1
        res+=yy*(yy+1)//2
print(res)

Submission status: Accepted

Solution code:
n = int(input())
num_of_anagram = dict()
ans = 0

for _ in range(n):
    s = input()
    s = ''.join(sorted(list(s)))
    if s in num_of_anagram:
        num_of_anagram[s] += 1
        ans += num_of_anagram[s]
    else:
        num_of_anagram[s] = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    n = int(input())
    s_array = [''.join(sorted(input()[:-1])) for _ in range(n)]

    s_array.sort()
    ans = 0
    count = 0
    word = s_array[0]
    for i in range(1, n):
        if(word == s_array[i]):
            count += 1
        else:
            ans += (count*(count+1))//2
            count = 0
        word = s_array[i]

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

Submission status: Accepted

Solution code:
N = int(input())
s = [input() for _ in range(N)]
for i in range(N):
  s[i] = "".join(sorted(s[i]))
t = {}
for i in range(N):
  if s[i] in t:
    t[s[i]] += 1
  else:
    t[s[i]] = 1
print(sum(t[k]*(t[k]-1)//2 for k in t))

Submission status: Accepted

Solution code:
import time
N = int(input())
s = [str(input()) for i in range(N)]

j = 0
ans = 0
for i in range(N):
  s[i] = "".join(sorted(s[i]))
#print(s)

s = sorted(s)
tango = "yeeeh"
x = 0
#print(s)
for i in range(N):
  if s[i] == tango:
    x += 1
  else:
    ans += x * (x + 1) //2
    x = 0
  tango = s[i]
ans += x * (x + 1) //2

print(ans)

Submission status: Accepted

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

n = int(input())
s = [''.join(sorted(input())).strip() for _ in range(n)]
c = Counter(s)
ans=sum(map(lambda x: x*(x-1)//2, c.values()))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
strings = sorted([sorted(input()) for _ in range(N)])
ans = 0
temp = 1
for s1, s2 in zip(strings, strings[1:]):
    if s1 == s2: temp += 1
    else:
        ans += temp * (temp - 1) // 2
        temp = 1
ans += temp * (temp - 1) // 2
print(ans)

Submission status: Accepted

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

dic={}

for i in  s:
    i = sorted(i)
    i = ''.join(i)
    if i in dic:
        dic[i]+=1
    else:
        dic[i]=1

ans =0
for i in dic.values():
    if i==1:
        continue
    else:
        ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s_list = [input() for i in range(N)]

ans = 0
word= {}

for j in s_list:
#     a = ''.join(sorted(list(j)))
    a = ''.join(sorted(j))
    if a in word:
        ans += word[a]
        word[a] += 1
    else:
        word[a] = 1
    
print(ans)