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())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions


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 = []
n = 0
for i in range(N):
    s.append(list(input()))
ans = 0


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

for i in range(1,N):

    if s[i]==s[i-1]:
        n += 1
        if i == N-1:
            ans += cmb(n+1, 2)
    else:
        if n > 0:
            ans += cmb(n+1,2)
            n = 0
print(ans)

Submission status: Accepted

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

from math import factorial
from operator import mul
from functools import reduce

def comb(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

def main():
    dic = {}
    ans = 0
    for i in range(N):
        S[i].sort()
        tmp = "".join(S[i])
        S[i] = tmp

    for s in S:
        if s in dic:
            dic[s] += 1
        else:
            dic[s] = 1
    for val in dic.values():
        if val > 1:
            ans += comb(val,2)

    print(int(ans))
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
import collections


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


n = int(input())
s = ["".join(sorted(input())) for _ in range(n)]
counter = collections.Counter(s)
print(sum([combinations_count(i, 2) for i in counter.values() if i > 1]))

Submission status: Accepted

Solution code:
n = int(input())
s = ["".join(sorted(input())) for _ in range(n)]
ans = 0
s_dic = {i:0 for i in s}

for j in range(n):
	if s[j] in s_dic :
		s_dic[s[j]] += 1

print(sum(s_dic[k] * (s_dic[k] - 1) // 2 for k in s_dic))

Submission status: Accepted

Solution code:
N=int(input())
S=sorted(["".join(sorted(input())) for i in range(N)])
D={s:0 for s in S}
for s in S:
  D[s]+=1
print(sum([D[d]*(D[d]-1)//2 for d in D]))

Submission status: Accepted

Solution code:
from collections import Counter


cnt = Counter()
N = int(input())

for i in range(N):
    s = input()
    cnt[''.join(sorted(s))] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}

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

count = 0
for num in dic.values():
    count += sum([i for i in range(num)])

print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append("".join(sorted(input())))
S.sort()
C = []
cnt = 1
for i in range(N - 1):
    if S[i] == S[i + 1]:
        cnt += 1
    else:
        C.append(cnt)
        cnt = 1
C.append(cnt)
ans = 0
for cnt in C:
    ans += cnt * (cnt-1) // 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

S = []
for _ in range(n):
    s = list(input())
    s.sort()
    S.append("".join(s))
S.sort()

C = []
c = 1
for i in range(n - 1):
    if S[i] == S[i + 1]:
        c += 1
    else:
        C.append(c)
        c = 1
C.append(c)

ans = 0
for c in C:
    ans += c * (c - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a=[]
for i in range(N):
    s=input()
    s="".join(sorted(s)).split()
    a.append(s[0])
num=Counter(a).values()
#print(list(num))
ans=0
for i in (list(num)):
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a=[]
for i in range(N):
    s=input()
    S=[]
    for j in range(10):
        S.append(s[j])
    S.sort()
    s="".join(S).split()
    a.append(s[0])
x=Counter(a)
num=x.values()
#print(list(num))
ans=0
for i in (list(num)):
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

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

A = ["".join(sorted(input())) for _ in range(n)]
c = Counter(A)

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
s = []
textSet = set()
ans = 0

for i in range(n):
    s.append(''.join(sorted(input())))

s.sort()

# print(s)
cnt = 0

for i in range(1, n):
    if s[i] == s[i-1]:
        cnt += 1
    elif cnt > 0:
        # ans += cntC2
        # ans += len(list(itertools.combinations(range(cnt+1), 2)))
        ans += (cnt + 1) * cnt // 2
        cnt = 0

if cnt > 0:
    # ans += len(list(itertools.combinations(range(cnt + 1), 2)))
    ans += (cnt + 1) * cnt // 2

print(ans)

Submission status: Accepted

Solution code:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
import numpy as np
n=int(input())
s=list(input() for _ in range(n))

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

ans=0
cnt=1
for i in range(1,n):
    if s[i]==s[i-1]:
        cnt+=1
    elif (s[i]!=s[i-1]) & (cnt>1):
        ans+=combinations_count(cnt, 2) 
        cnt=1
    else:
        continue
if cnt>1:
    ans+=combinations_count(cnt, 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = [''.join(sorted(input())) for _ in range(N)]
dic = {}
count=0
for i in range(N):
    if s[i] in dic:
        dic[s[i]] = dic[s[i]] + 1
    else:
        dic.setdefault(s[i], 1)
for i in dic.values():
    if i > 1:
        count += i* (i - 1) // 2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = ['']*n
for i in range(n):
  s[i] = (''.join(sorted(input())))
s.sort()

i=0
c=1
ans = 0
while i < n-1:
  while i+c < n and s[i] == s[i+c]:
    c += 1
  else:
    ans += c*(c-1)//2
    i += c
    c = 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[''.join(sorted(list(input()))) for i in range(N)]
s.sort()
i=0
out=0
while i<N:
  c=1
  while i+c<N and s[i]==s[i+c]:
    c+=1
    
  out+=c*(c-1)//2
  i+=c
  
print(out)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for i in range(N):
    spell = ''.join(sorted(input()))
    if spell in dic.keys():
        dic[spell] += 1
    else:
        dic[spell] = 1

count = 0
for num in dic.values():
    count += sum([i for i in range(num)])

print(count)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc137/tasks/abc137_c
# アナグラムの個数を求める問題
# アナグラムの条件→各alphabetの文字数が一致 もしくは ソートすると並び順が同じでもok
# 同じ文字カウント(ソートして同じ文字列)がN個あるとき、i,jの組み合わせは1/2(n*n-n)となる (組み合わせを考えて確かめてみよ)

from collections import Counter

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

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

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = [input() for x in range(N)]
ans = 0
di = dict()
for k in li:
    word = ''.join(sorted(list(k)))
    if word in di.keys():
        ans += di[word]
        di[word] += 1
    else:
        di[word] = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for i in range(n):
    d["".join(sorted(s[i]))] += 1

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())

    s_dict = {}
    count = 0
    
    for i in range(n):
        s = sorted(input())
        _s = "".join(s)
        
        if not _s in s_dict:
            s_dict[_s] = 1
        else:
            count += s_dict[_s]
            s_dict[_s] += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    sd = {}
    ans = 0
    for _ in range(N):
        si = ''.join(sorted(input().strip()))
        if not si in sd:
            sd[si] = 1
        else:
            ans += sd[si]
            sd[si] += 1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N = int(input())

sd = {}
ans = 0

for _ in range(N):
    inp = input()
    si = ''.join(sorted(inp))
    
    if not si in sd:
        sd[si] = 1
    else:
        ans += sd[si]
        sd[si] += 1

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
stock = dict()
ans = 0
for i in range(N):
    c="".join(sorted(input()))
    if c in stock:
        ans += stock[c]
        stock[c] += 1
    else:
        stock[c] = 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ana_list = {}

for i in range(N):
  letter = input()
  tmp = []
  for k in range(10):
    tmp.append(letter[k])
    
  tmp.sort()
  letter = ''.join(tmp)
  if letter in ana_list:
    ana_list['{}'.format(letter)] += 1
  else:
    ana_list['{}'.format(letter)] = 1

num_list = list(ana_list.values())

ans = 0
for i in range(len(num_list)):
  if num_list[i] >= 2:
    ans += num_list[i] * (num_list[i] - 1) / 2
    
print(int(ans))

Submission status: Accepted

Solution code:

n=int(input())
zisyo={}
count=0
for i in range(n):
    s="".join(sorted(input()))#間を開けないでSORT
    if s in zisyo:
        zisyo[s]+=1
        count+=zisyo[s]
    else:
        zisyo[s]=0
print(count)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    n=int(input())
    D=defaultdict(int)
    for _ in range(n):
        tup=tuple(sorted(input()))
        D[tup]+=1

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

    print(ans)
resolve()

Submission status: Accepted

Solution code:
import bisect
import heapq
from collections import Counter

N = int(input())
S = []
for n in range(N):
    string = ''.join(sorted(input()))
    S.append(string)
ans = 0
counter_s = Counter(S)
for key, value in counter_s.items():
    if value >= 2:
        ans += value * (value - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

import itertools

# 引数のリストから3つ選ぶときの組み合わせリストを返す
def comb_list(seq:list, n:int):
    return list(itertools.combinations(seq, n))

# aCbを返す
def comb_num(a, b):
    if a - b < b: b = a - b
    if b == 0: return 1
    if b == 1: return a

    numerator = [a - b + k + 1 for k in range(b)]
    denominator = [k + 1 for k in range(b)]

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

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

    return result

N = int(input())

cnt = defaultdict(int)
for i in range(N):
    s = input()
    s = sorted(s)
    cnt[str(s)] += 1

ans = 0
for c in cnt.values():
    if c==1:
        continue
    ans += comb_num(c, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
s=[list(input()) for i in range(n)]
for i in range(n):
  s[i].sort()
s=["".join(s[i]) for i in range(n)]
c=Counter(s)
ans=0
for i in c.values():
  ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = []

for i in range(N):
    s = "".join(sorted(input()))
    A.append(s)

C = Counter(A)
ans=0

for i in C.values():
    if i==1:
        continue
    ans+=i*(i-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [0]*N

for i in range(N):
    #各文字列に番号A[i]を与える
    string = input()
    flag = True
    for j in range(10):
        A[i]+=10**(ord(string[j])-ord('a'))
        if(j<9):
            if(string[j]!=string[j+1]):
                flag = False
    if flag:
        A[i] = (ord(string[0])-ord('a')+1)*(-1)
    
C = Counter(A)
ans=0

for i in C.values():
    if i==1:
        continue
    ans+=i*(i-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input()) #n行の入力をリストに入れる
S = [''.join(sorted(input())) for i in range(n)] #inputの文字列をソートできる
counter = Counter(S) #Sの中の文字列を集計し、dict型で返せる

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

print(ans)

""""
l = []
for i in range(n):
    l.append(input()) 

def get_counter(s):
    return Counter(s.upper().replace(' ', ''))

def verify_anagrams(first_word, second_word):
    return get_counter(first_word) == get_counter(second_word)

count =0 
for i in range(n):
    for j in range(i+1,n):
        if verify_anagrams(l[i],l[j])==True:
            count += 1


print(count)

#print(verify_anagrams(l[0],l[2]))
"""

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
#S = [''.join(sorted(input())) for _ in range(N)]
S = [''.join(input())for _ in range (N)]
#print(S)
T=[''.join(sorted(S[x]))for x in range (N)]
#print(T)
count = Counter(T)
#print (count)
ans = sum(x*(x-1)//2 for x in count.values())
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A={}
for i in range(n):
  s=str(sorted(input()))
  if s in A.keys():
    A[s]+=1
  else:
    A[s]=1
ans=0
for i in list(A.values()):
  ans+=int(i*(i-1)/2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = ["".join(sorted(input())) for i in range(N)]
S_cnt = Counter(S).values()
cnt=0
for s in S_cnt:
    for i in range(s):
        cnt+=i

print(cnt)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 +10 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase

#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
mp=defaultdict(int)
t=[]
for i in range(n):
    s=StoI()
    s.sort()
    tmp=0
    for i in s:
        tmp=tmp*100+i
    t.append(tmp)
    #mp[s]+=1
    
c=Counter(t)

ans=0
for i,j in c.items():
    ans+=j*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    c = Counter([''.join(sorted(input())) for _ in range(n)]).values()
    print(sum([i * (i - 1) // 2 for i in c]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = []
for _ in range(N):
    s = list(input())
    s.sort()
    s = ''.join(s)
    S.append(s)

counter = Counter(S)

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

Submission status: Accepted

Solution code:
n = int(input())
ana_map = {}
for i in range(n):
    s = input()
    bucket = [0 for _ in range(26)]
    
    for c in s:
        bucket[ord(c) - ord('a')] += 1
    
    if(ana_map.get(str(bucket)) is None): ana_map[str(bucket)] = 1
    else: ana_map[str(bucket)] += 1

res = 0
for key in ana_map.keys():
    res += ana_map[key] * (ana_map[key]-1) / 2
print(int(res))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
def combinations_count(n, r):
    if n<r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def solve(N: int, s: "List[str]"):
    s_dict = {}    
    for i in range(N):
        ss = ''.join(sorted(list(s[i])))
        if s_dict.get(ss) == None:
            s_dict[ss] = 1
        else:
            s_dict[ss] += 1
    answer = 0
    for _,value in s_dict.items():
        answer += combinations_count(value,2)


    print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

counts = defaultdict(int)

for s in strings:
    counts[s] += 1

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

print(comb)

Submission status: Accepted

Solution code:
n = int(input())
a = 0
input_list = []
while a < n:
    ant = input()
    input_list.append(ant)
    a += 1
l={}
for i in range(len(input_list)):
    l[i] = list(input_list[i])
    l[i].sort()
l_dic = {}
ans = 0
for i in range(len(l)):
    try:
        ans += l_dic[str(l[i])]
        l_dic[str(l[i])] += 1
    except:
        l_dic[str(l[i])] = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = [input() for i in range(n)]
ss = ["".join(sorted(s[i])) for i in range(n)]
d = Counter()
ans = 0
for i in range(n):
  t = ss[i]
  if t in d:
    ans += d[t]
  d[t] += 1
  #print(d)
print(ans)

Submission status: Accepted

Solution code:
# 15:20 -16:08 giveup
# 21:45 -22:33 review
from collections import Counter

# 3
# acornistnt
# peanutbomb
# constraint
n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
# s is ['acinnorstt', 'abbemnoptu', 'acinnorstt']
counter = Counter(s)
# counter is Counter({'acinnorstt': 2, 'abbemnoptu': 1})
# counter.values() is dict_values([3, 2])
ans = sum(x*(x-1)//2 for x in counter.values())
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = []

a = ord("a")
for i in range(N):
    s = list(input())
    s.sort()
    s_= "".join(s)
    S.append(s_)
S.sort()

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

d = Counter()
N = int(input())
ans = 0
for i in range(N):
    t = str("".join(sorted(input())))
    if t in d:
        ans += d[t]
    d[t] += 1
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    cnt = 0
    sorted_s_count = collections.defaultdict(int)
    for _ in range(N):
        s = S()
        s = ''.join(sorted(s))
        if s in sorted_s_count:
            cnt += sorted_s_count[s]
        sorted_s_count[s] += 1
    print(cnt)
main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
d=Counter()
ans=0
for i in range(n):
    t=str("".join(sorted(input())))
    if t in d:
        ans+=d[t]
    d[t]+=1
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
n=int(input())
l=[]
for _ in range(n):
  s=input()
  s=''.join(sorted(s))
  l.append(s)
l.sort()
c=0
for i,j in groupby(l):
  z=len(list(j))
  c=c+(z*(z-1)//2)
print(c)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
strings=list("".join(sorted(input())) for _ in range(N))
string_set = set()
counts = {}
count = 0
for string in strings:
    if string in string_set:
        count += counts[string]
        counts[string] += 1
    else:
        string_set.add(string)
        counts[string] = 1

print(count)

Submission status: Accepted

Solution code:
import collections as cl
import math
N=int(input())

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

result=list(cl.Counter(result_).values())

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

ans=0
for j in range(len(result)):
    if result[j]==1:
        continue
    else:
        ans+=combinations_count(result[j],2)

print(ans)

Submission status: Accepted

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

for i in range(n):
    s = "".join(sorted(input()))
    if s not in d:
        d[s] = 1
    else:
        ans += d[s]
        d[s] += 1
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

total = 0
n = int(input())
word_list = ["".join(sorted(input())) for _ in range(n)]
count_words = dict(Counter(word_list))
for item in count_words:
    ans = 0
    tmp = count_words[item] - 1
    ans = 1/2 * tmp * (tmp + 1)
    total += ans
print(int(total))

Submission status: Accepted

Solution code:
#ABC-137-C
N = int(input())
S = []
for _ in range(N):
    si = sorted(input())
    S.append("".join(si))

ans = 0
already_exist = {}

for i in range(N):
    if S[i] not in already_exist:
        already_exist[S[i]] = 0
    else:
        already_exist[S[i]] += 1
        ans += already_exist[S[i]]

# TLEになるので却下
# for i in range(N-1):
#     for j in range(i+1, N):
#         if S[i] == S[j]:
#             ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
ans=0
for i in range(n):
  s=list(input())
  s.sort()
  a.append(str(s))
a.sort()
y=''
w=0
c=[]
for x in a:
  if y==x:
    w+=1
  else:
    c.append(w)
    w=1
  y=x
c.append(w)
c=c[1:]
for x in c:
  ans+=x*(x-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
N =int(input())
S = []
#キーが存在しなかったときのデフォルトの値を定義できる
dict=defaultdict(int)
ans=0
for _ in range(N):
    s=''.join(sorted(input()))
    ans+=dict[s]
    dict[s]+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
dict = defaultdict(int)
ans = 0
for i in range(N):
    s = ''.join(sorted(input()))
    ans += dict[s]
    dict[s] += 1
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

def cbn(n):
    if n == 1:
        return 0
    else:
        return n * (n - 1) // 2

N = int(input())
S = [sorted(input()) for _ in range(N)]
S.sort()
print(sum(cbn(len(list(l))) for s, l in groupby(S)))

Submission status: Accepted

Solution code:
import math

N = int(input())
S = [0] * N
for i in range(N):
    S[i] = sorted(str(input()))

Sum = 0
hoge = 1

S.sort()

for i in range(N-1):
    if S[i] == S[i+1]:
        hoge += 1
    if (S[i] != S[i+1] or i == N-2) and hoge != 1:
        #Sum += math.factorial(hoge) // (math.factorial(hoge - 2) * math.factorial(2))
        Sum += hoge*(hoge-1)//2
        hoge = 1

print(Sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
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 = [input() for _ in range(N)]
split_s = [''.join(sorted([j for j in i])) for i in s]

c = collections.Counter(split_s)
ans = 0
for i in c.values():
    if i > 1:
        ans += cmb(i,2)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
m=dict()
ans=0
for	i in range(N):
  s=input()
  s=''.join(sorted(s))
  if s in m:
    ans += m[s]
    m[s] += 1
  else:
    m[s] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = ["".join(sorted(list(input()))) for _ in range(n)]
s.sort()


def num_cmb(a, b):
    enum = 1
    denom = 1
    for i in range(b):
        enum *= (a - i)
        denom *= (b - i)
    return enum // denom


sum = 0
count = 1
prev = s[0]
for s_i in s[1::]:
    if prev == s_i:
        count += 1
    else:
        sum += num_cmb(count, 2)
        count = 1
        prev = s_i
if 0 < count:
    sum += num_cmb(count, 2)
print(sum)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = [input() for _ in range(n)]
c = Counter()
count = 0
for word in s:
  a = ''.join(sorted(word))
  count += c[a]
  c[a] += 1
print(count)

Submission status: Accepted

Solution code:
import math
n = int(input())
s_list = []
for _ in range(n):
    s_list.append(sorted(input()))
s_list = sorted(s_list)

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


eq_count = 0
count = 0
for i in range(1,n):
    if s_list[i-1] == s_list[i]:
        eq_count = eq_count + 1
    elif eq_count != 0:
        count = count + combinations_count(eq_count+1,2)
        eq_count = 0
    if i == n-1 and eq_count!=0:
        count = count + combinations_count(eq_count+1,2)

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = []
for i in range(n) :
  str = input()
  s.append(''.join(sorted(str)))
  
sd = Counter(s)

ans = 0
for v in sd.values():
    if v != 1 :
      ans += int(v*(v-1)*0.5)

print(ans)

Submission status: Accepted

Solution code:
#ABC137_C
from collections import Counter
N=int(input())
S=[input() for i in range(N)]

ctr=Counter()
ans=0

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

Submission status: Accepted

Solution code:
import collections
num = int(input())

arr = []

for i in range(num):
    arr.append("".join(sorted(input())))


c = collections.Counter(arr)


cnt = 0
for i in list(c.values()):
    if i ==1:
        continue
    
    cnt += (i-1)*(i)//2

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
s = list(list(input())for i in range(N))
count = 1
ans = 0
for i in range(N):
    s[i].sort()
s.sort()
for i in range(0,N-1):
    if s[i] == s[i+1]:
        ans += count
        count += 1
    else:
        count = 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=sorted([''.join(sorted(input())) for _ in range(n)])
c=1
ans=[]
for i in range(n-1):
  if l[i] == l[i+1]:
    c += 1
  else:
    ans.append((c*(c-1))/2)
    c=1
ans.append((c*(c-1))/2)
print(int(sum(ans)))

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = [list(input()) for i in range(N)]

for i in range(N):
    Si = S[i]
    Si.sort()
    Si2 = ''.join(Si)
    S[i] = Si2

ans = 0
Sc = collections.Counter(S).most_common()
for s in Sc:
    ans += s[1]*(s[1]-1)//2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = ["".join(sorted(input())) for i in range(N)]
dict = {}
ans = 0
for i in range(N):
    if S[i] in dict:
        ans += dict[S[i]]
        dict[S[i]] += 1
    else:
        dict[S[i]] = 1

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce

N = int(input())

strings = []

for _ in range(N):
    s = input()
    strings.append(reduce(lambda a, b: a + b, sorted(s)))

strings = sorted(strings)

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

ans += counter * (counter - 1) / 2
        
print(int(ans))