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 collections
n = int(input())
s = []
for i in range(n):
    s.append("".join(sorted(list(input()))))
c = collections.defaultdict(int)
ans = 0
for t_s in s:
    c[t_s] += 1
    ans += c[t_s] - 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
dic = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26}

ans = []
for i in range(N):
    si = input()
    a = [0]*26
    s = ""
    for j in range(10):
        a[dic[si[j]]-1] += 1
    for k in range(26):
        s = s + "a"
        s = s + str(a[k])
    ans.append(s)
c = Counter(ans)
b = 0
for v in c.values():
    if v > 1:
        b += v*(v-1)//2
    else:
        pass
print(b)

Submission status: Accepted

Solution code:
import collections

N = int(input())
list = [''.join(sorted(input())) for i in range(N)]

c = collections.Counter(list)
ans_list = c.most_common()
ans = [ans[1]*(ans[1]-1)//2 for ans in ans_list if not ans[1]==1]
ans_len = len(ans)
if ans_len==0:
    print(0)
else:
    print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
lista = []
for n in range(N):
    sn = sorted(list(input()))
    #print(sn)
    lista.append(sn)
listaa = sorted(lista)    
ans = 0
count = 0
for a in range(1,N):
    if listaa[a] == listaa[a-1]:
        count += 1
        ans = ans + count
    else:
        count = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = sorted([sorted(list(input())) for i in range(N)])

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

ans = 0
n_same_str = 1
for i in range(N-1):
    if L[i] == L[i+1]:
        n_same_str += 1
    else:
        if n_same_str >= 2:
            ans += comb(n_same_str, 2)
        n_same_str = 1

if n_same_str >= 2:
            ans += comb(n_same_str, 2)

print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())
s = [list(input()) for i in range(N)]
S = []

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

for i in range(N):
  s[i].sort()
  string = ''.join(s[i])
  S.append(string)
  
a = {}
answer = 0
for i in range(N):
  if S[i] not in a.keys():
    a[S[i]] = 1
  else:
    a[S[i]] += 1
else:
  for j in a.values():
    if j != 1:
      answer += comb(j, 2)
  else:
    print(answer)

Submission status: Accepted

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

ans = 0
cnt = 0
tmp = ss.pop(0)
for s in ss:
    if tmp == s:
        cnt+=1
    elif cnt > 0:
        tmp = s
        ans+=((cnt+1) * cnt // 2)
        cnt = 0
    else:
        tmp = s
        cnt = 0
print(ans + ((cnt+1) * cnt // 2))

Submission status: Accepted

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

s_list = sorted(s_list)

count = 0
total = 0
s_prev = s_list[0]
for s in s_list[1:]:
    if s_prev == s:
        count += 1
    else:
        if count > 0:
            total += count * (count + 1) // 2
            count = 0
    s_prev = s

if count > 0:
    total += count * (count + 1) // 2

print(total)

Submission status: Accepted

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

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

count = defaultdict(int)

answer = 0
for i in range(1,N+1):
    c = Counter(s[i])
    c = sorted(c.items(), key=lambda x:x[0])
    # O(len(c))
    str_c = ""
    for key,item in c:
        str_c += key*item
    answer += count[str_c]
    count[str_c] += 1

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
m = dict()
ans = 0
for _ 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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
import math

N = int(input())
H = defaultdict(int)

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

ret = 0
for v in H.values():
    if v >= 2:
        ret += math.factorial(v) // (math.factorial(v - 2) * math.factorial(2))
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
a = [''.join(sorted(input())) for i in range(n)]
ans = 0
count = 0
a.sort()
for i in range(1,n) :
    if a[i] == a[i-1] :
        count += 1
        ans += count
    else :
        count = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
from collections import Counter

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]


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()
S = [[s for s in input().strip()] for _ in range(N)]

words = []
for s in S:
	s.sort()
	words.append(s)

words.sort()

ans = []
tmp = ''
num = -1
for w in words:
	#print(tmp , w, num)
	if tmp == w:
		#print('same')
		ans[num] += 1
		#print(ans)
	else:
		#print('different')
		tmp = w
		num+=1
		ans.append(1)
		#print(ans)
		
#print(ans)

count = 0
for a in ans:
	if a != 1: count += cmb(a, 2)

print(count)

Submission status: Accepted

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

# refered to editorial

import numpy as np

N = int(input())
S = []
hash_table = {}

a_ascii = ord('a')
bins_of_char = np.zeros((N, 26),dtype=np.int)
num_of_anagram = 0

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

for i in range(N):
    if S[i] in hash_table:
        num_of_anagram += hash_table[S[i]]
        hash_table[S[i]] += 1
    else:
        hash_table[S[i]] = 1


print(num_of_anagram)

Submission status: Accepted

Solution code:
import collections
N = int(input())
array = []
for i in range(N):
    s = input()
    dict = {}
    for j in range(len(s)):
        if s[j] not in dict:
            dict[s[j]] = 1
        else:
            dict[s[j]] += 1
    dict = sorted(dict.items())

    array.append(str(dict))
sum = 0
c = collections.Counter(array)
for v in c.values():
    sum += int(v*(v-1)/2)
print(sum)

Submission status: Accepted

Solution code:
from collections import Counter

kw = Counter()

n = int(input())

s = []
for _ in range(n):
    a = sorted(input())
    kw[''.join(a)] += 1

sum = 0
for _, val in kw.most_common():
    sum += val * (val - 1) // 2

print(sum)

Submission status: Accepted

Solution code:
from collections import Counter

def ABC137C_GreenBin():
    N = int(input().strip())
    S = [input().strip() for i in range(N)]

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

        di[s] += 1
    
    print(ans)


def main():
    ABC137C_GreenBin()

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s = [''.join(sorted(input())) for i in range(n)]
s.sort()
s.append("a")
b=1
c=0
for i in range(n):
    if s[i]!=s[i+1]:
        c+=(b*(b-1))//2
        b=1
    else:
        b+=1
print(c)

Submission status: Accepted

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

s.insert(len(s), "")

current = 0
ans = 0
for i in range(len(s)):
    if s[current] != s[i]:
        num = i - current
        if num >= 2:
            ans += math.factorial(num)//(math.factorial(2)*math.factorial(num-2))

        current = i

print(ans)

Submission status: Accepted

Solution code:
from math import factorial
n = int(input())
ans_list = []
num_list = []
for i in range(n):
    s = list(input())
    s.sort()
    ans_list.append("".join(s))
ans_list.sort()
count = 1
for i in range(n-1):
    if ans_list[i] == ans_list[i+1]:
        count += 1
    else:
        num_list.append(count)
        count = 1
num_list.append(count)
ans = 0
for i in range(len(num_list)):
    if num_list[i] != 1:
        ans += factorial(num_list[i]) // factorial(2) // factorial(num_list[i]-2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

List=[''.join(sorted(input())) for i in range(N)]
List.sort()
List.extend("1")
cnt = 0
num = 0

for i in range(N):
  a = List[i]
  b = List[i+1]
  if a != b:   
    cnt = cnt + num * (num+1) / 2
    num = 0 
  else:
    num = num + 1

print(int(cnt))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

a={}
ans=0
for i in range(n):
    s=input()
    b=sorted(s)
    w=''.join(b)
    if not a.get(w):
        a[w]=1
    else:
        v=a[w]
        v +=1
        a[w]=v

for v in a.values():
    if v > 1:
        c=int((v*(v-1))/2)
        ans +=c
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
import math


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


n = int(input())
s = []
for i in range(n):
    s.append("".join(sorted(input())))
c = Counter(s).most_common()
ans = 0
for i in c:
    if i[1] > 1:
        ans += nCr(i[1], 2)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
l=[]
for x in range(0,N):
    l.append(input())
for x in range(N):
    k=list(l[x])
    k.sort()
    k=''.join(k)
    l[x]=k
l.sort()
k=1
ans=0
for i in range(N-1):
    if l[i]==l[i+1]:
        k+=1
    else:
        ans+=k*(k-1)//2
        k=1
ans+=k*(k-1)//2
print(ans)

Submission status: Accepted

Solution code:
import math

n=int(input())
L={}
for i in range(n):
    s="".join(sorted(list(input())))
    if s not in L.keys():
        L[s]=1
    else:
        L[s]+=1

ans=0
for v in L.values():
    if v!=1:
        ans += math.factorial(v)/(math.factorial(2)*math.factorial(v-2))
print(int(ans))

Submission status: Accepted

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

for i in range(N):
    num = cnt.get(S[i], 0)
    ans += num
    cnt[S[i]] = num + 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append(input())
history = {}
ans = 0

for i in range(N):
    sorted_s = ''.join(sorted(S[i]))
    num = history.get(sorted_s, 0)
    ans += num
    history[sorted_s] = num + 1

print(ans)

Submission status: Accepted

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd

def lcm(n,m):
    return int(n*m/gcd(n,m))

import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace

from queue import PriorityQueue as pq

def reduce(p, q):
    common = fractions.gcd(p, q)
    return (p//common , q//common )
# from itertools import accumulate
# from collections import deque

from operator import mul
from functools import reduce

def combinations_count(n, r):
    if n<r:
        return 0
    else:
        r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

import random

def main():
    n = int(input().strip())
    dict_S = {}
    for i in range(n):
        S = input().strip()
        # S = str(sorted(S))      # こうしてしまうと，"[" や "]"まで入った，謎の文字列になってしまう
        S="".join(sorted(S))
        # eprint(type(S))         # 普通に文字列をsortすると，list型に勝手に変換されてしまう

        
        if S in dict_S:
            dict_S[S]+=1
        else: 
            dict_S[S]=1
    
    ans=0
    # SSS="asdfasdfa"
    # eprint(SSS)
    # eprint("dict_S.items()",end="\n")
    # eprint(dict_S.items())
    # for key,value in dict_S.items():
    #     eprint(type(key))
    #     eprint(key)
    #     eprint(value)
        
    for key,value in dict_S.items():
        ans+=combinations_count(value,2)
    print(ans)
    return

if __name__ == '__main__':
    main()

    

'''
#+TITLE: Mathematical Formulation
given N (2<=N<=10^5)
given [s_{i}] 
	(0<=i<=N-1)
	(\forall i \in {0...(N-1)}, len(s_{i})==10 )
	
	( \forall (i,j) \in {0...(N-1)}^{2} and 0<=i<j<=(N-1), s_{i} != s_{j} )
	
	

'''

# ### 
# dict_name = {key: value, key: value, ...}

# if pair in dict_1:
#     dict_1[pair]+=1
# else: 
#     dict_1[pair]=0

# for key, value in dict_1.items():
#     print(key, value)

Submission status: Accepted

Solution code:
n=int(input())
s=[''.join(sorted(input())) for _ in range(n)]
ans=0
cnt={}
for i in range(n):
  if s[i] in cnt:
    cnt[s[i]]+=1
  else:
    cnt[s[i]]=1
print(sum(cnt[key]*(cnt[key]-1)//2 for key in cnt))

Submission status: Accepted

Solution code:
N = int(input())
lst = [''.join(sorted(input().rstrip())) for i in range(N)]

ans = 0
dic = {}

for i in lst:
    if i in dic:
        ans += dic[i]
        dic[i] += 1
    else:
        dic[i] = 1
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)

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

Submission status: Accepted

Solution code:
N = int(input())

S = []
for i in range(N):
    S.append(input())

count = {}
ans = 0

for s in S:
    s=str(sorted(s))
    if s in count:
        count[s] += 1
    else:
        count[s] = 1

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

Submission status: Accepted

Solution code:
#各文字列をソートしながらハッシュテーブルへぶち込む
import sys
input = sys.stdin.readline

def cmb(n, r):
    if n - r < r: r = n - r
    if n == 1: return 0
    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())
table = {}
cache = {}
for x in range(N):
    s = ''.join(sorted(list(input())))
    s = s.strip()
    if s in table:
        table[s] = table[s] + 1
    else:
        table[s] = 1

ans = 0
for x in table:
    if table[x] not in cache:
        p = cmb(table[x], 2)
        cache[x] = p
    ans = ans + cache[x]
    #print("{}: {} → {}".format(x, table[x], cache[x]))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
d={}
for i in range(N):
  a = "".join(sorted(list(input())))
  if a not in d:
    d[a] = 1
  else:
    d[a]=d[a]+1
cnt = 0
for B in d.values():
  cnt += int(B * (B-1) /2)
print(cnt)

Submission status: Accepted

Solution code:
def ccc(n):
    #c(n,2)
    result = (n * (n-1))//2
    return result



def sortString(a):
    a = list(a)
    a.sort()
    return "".join(a)


def calculate(arr):
    if len(arr) == 0:
        return 0

    dict = {}
    for i in range(len(arr)):
        s = sortString(arr[i])
        if dict.get(s) == None:
            dict.setdefault(s,1)
        else:
            dict.__setitem__(s,dict.get(s) + 1)


    sum = 0
    for v in dict.values():
        sum += ccc(v)

    return sum



arr = []

N = int(input())
for i in range(N):
    arr.append(input())


result = calculate(arr)
print(result)

Submission status: Accepted

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

from collections import Counter

cs = Counter(["".join(sorted(_)) for _ in S])

result = 0
for c, n in cs.items():
    result += n * (n - 1) // 2

print(result)

Submission status: Accepted

Solution code:
N = int(input())
S = sorted(''.join(sorted(list(input()))) for i in range(N))
# N(10+log_2(10)+10)
ans = 0
sep = []
for i in range(N-1): # N*4
    if  S[i] != S[i+1]:
        sep.append(i+1)
sep.append(N)
# print(sep)
count = []
prev = 0
for i in sep: # len(set(N))*2 <= N*2
    count.append(i-prev)
    prev = i
# print(count)
for i in count: # N*2 (similarly)
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())

cnt = collections.Counter(["".join(sorted(input())) for i in range(n)])

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

print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

N = int(input())

D = dict()
for i in range(N):
    a = input()
    a = ''.join(sorted(a))
    if a in D:
        D[a] = D[a]+1
    else:
        D[a] = 1

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

print(ans)

Submission status: Accepted

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

c = collections.Counter(s)
for i in set(s):
    n = c[i]
    ans += n * (n-1) // 2
print(ans)

Submission status: Accepted

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

c = collections.Counter(s)
for i in set(s):
    n = c[i]
    ans += n * (n-1) // 2
print(ans)

Submission status: Accepted

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

c = collections.Counter(s)
for i in set(s):
    n = c[i]
    ans += n * (n-1) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections
from operator import mul
from functools import reduce

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

def main():
  n = input()
  n = int(n)
  x = []
  for i in range(n):
    l = input()
    x.append(''.join(sorted(l)))

  c = collections.Counter(x)
  values, counts = zip(*c.most_common())
  sum = 0
  for i in range(len(counts)):
    if counts[i] > 1:
      sum += C(counts[i], 2)
  print(sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
from operator import mul
from functools import reduce

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


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

c = collections.Counter(x)
values, counts = zip(*c.most_common())
sum = 0
for i in range(len(counts)):
  if counts[i] > 1:
    sum += C(counts[i], 2)

print(sum)

Submission status: Accepted

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

for i in range(n):
    s = ''
    list = sorted(input())
    for j in list:
        s += j
    if i != 0 and 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={}

for i in range(n):
  a=input()
  newa="".join(sorted(a))
  dict[newa]=dict.get(newa,0)+1

ans=0

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

print(int(ans))

Submission status: Accepted

Solution code:
from collections import defaultdict
N=int(input())
S={}
S_counter = defaultdict(int)
count=0
for i in range(N):
  s="".join(sorted(input()))
  if s in S:
    count += S[s]
    S[s] += 1
  else:
    dict={s:1}
    S.update(dict)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(n):
    t=input()#.rstrip()
    s.append(t)
#print(s)

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

s=sorted(s)

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

Submission status: Accepted

Solution code:
n=int(input())
import math
d={}
for i in range(n):
    a="".join(sorted(input()))
    if a not in d:
        d[a]=1
    else:
        d[a]+=1
ans=0
for i in d.values():
  ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = ["".join(sorted(i)) for i in open(0).read().split()[1:]]
    ans = 0
    d = {}
    for i in s:
        if i in d:
            ans += d[i]
            d[i] += 1
        else:
            d[i] = 1
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    s = map(lambda x: "".join(sorted(x)), open(0).read().split()[1:])
    d = defaultdict(lambda: 0)
    ans = 0
    for i in s:
        ans += d[i]
        d[i] += 1
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    s = ("".join(sorted(i)) for i in open(0).read().split()[1:])
    d = defaultdict(lambda: 0)
    ans = 0
    for i in s:
        ans += d[i]
        d[i] += 1
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    s = [tuple(sorted(i)) for i in open(0).read().split()[1:]]
    d = defaultdict(lambda: 0)
    ans = 0
    for i in s:
        ans += d[i]
        d[i] += 1
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
import collections
from operator import mul
from functools import reduce


def cmb(n, r):
    if n < r:
        return 0
    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():
    n = int(input())
    s_list = []
    for i in range(n):
        temp_s = sorted(list(input()))
        s_list.append(''.join(temp_s))

    counter = collections.Counter(s_list)
    ans = 0
    for key, value in counter.items():
        temp = cmb(value, 2)
        ans += temp


    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    import math
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # map(int, input().split())

    n = int(input())
    wow = dict()
    for _ in range(n):
        s = list(input()[:-1])
        s.sort()
        s = str(s)
        if wow.get(s) == None:
            wow[s] = 0
        wow[s] += 1
    # print(wow)

    def combinations_count(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    ans = 0
    for i in wow.values():
        # print(i)
        if i == 1:
            continue
        ans += combinations_count(i, 2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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



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

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())
from collections import Counter
li = []
for i in range (n):
    li.append(sorted(input()))
li.sort()
for i in range(n):
    li[i]="".join(li[i])

d = Counter(li)
L = list(d.values())

cnt = 0
for i in range(len(L)):
    cnt += L[i]*(L[i]-1)//2
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
slist = ["".join(sorted(list(input()))) for _ in range(N)]
c = Counter(slist)
ans = 0
for v in c.values():
    ans += sum(range(v))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
ans = 0
ctr = Counter()

for i in range(n):
  s = ''.join(sorted(list(input())))
  if s in ctr:
    ans += ctr[s]
  ctr[s] += 1
  
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
s = sorted(["".join(sorted(input())) for i in range(n)])
a = 0
b = []
for i in range(n-1):
  if s[i]==s[i+1]:
    a += 1
  else:
    a = 0
  b.append(a)
print(sum(b))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    x = {}
    N = int(input())
    
    for i in range(N):
        s = sorted(input())
        s = ''.join(s)
        if s in x: x[s] += 1
        else: x[s] = 1
    
    ans = 0
    
    for n in x.values():
        ans += (n*(n-1)//2)
        
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
def com(n,r):
  if n<r:
    return 0
  return int(factorial(n) // factorial(r) // factorial(n-r))

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

for _ in range(n):
  l=sorted(input())
  l_list.append(l)

l_list = sorted(l_list)

count=0

ans = 0
i = 0
j = 1
while i < n:
  tmp = 0
  while i + j < n and l_list[i] == l_list[i + j]:
    tmp += 1
    j += 1
  if tmp != 0:
    ans += com(tmp+1, 2)
  i += j
  j = 1

print(ans)

Submission status: Accepted

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

#print(s)

#出現文字列
sdict = {}

for i in range(N):

    s_i = str(s[i])

    if s_i in sdict:
        #組み合わせが増えるので
        sdict[s_i]+= 1
        continue

    else:
        sdict[s_i] = 1

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s={}

for _ in range(n):
    ss = str(sorted(input()))
    if ss not in s:
        s[ss] = 1
    else:
        s[ss] += 1

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

Submission status: Accepted

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

num_list = []
itr=0

while itr < N:
    num=1
    for tgt in range(itr+1, N):
        #print(itr, tgt)
        if sentence[itr]==sentence[tgt]:
            num += 1
            if tgt == N-1:
                num_list.append(num)
                itr=tgt
        else:
            num_list.append(num)
            itr = tgt
            break
    if itr==N-1:
        break

result = 0
if len(num_list)>0:
    for num in num_list:
        result += num*(num-1)//2
print(result)

Submission status: Accepted

Solution code:
n=int(input())

S={}
for i in range(n):
    s="".join(sorted(input()))
    if s in S:
        S[s]+=1
    else:
        S[s]=1
ans=0
for i in S.values():
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
from functools import reduce
import operator as op


def nCr(n,r):
    r = min(r,n-r)
    """ operator.mul = 掛け算を行う関数"""
    """引数の２つ目は先頭を示す"""
    up = reduce(op.mul,range(n,n-r,-1),1)
    down= reduce(op.mul,range(r,0,-1),1)
    return up // down

def main():
    n = int(sys.stdin.readline().rstrip())
    
    sorted_letters =[''.join(sorted(sys.stdin.readline().rstrip())) for _ in range(n)]

    count = 0
    for c in collections.Counter(sorted_letters).values():
        if c >=2:
            count +=nCr(c,2)
        
    
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = sorted([sorted(list(input())) for i in range(n)])
ans = []
las = l[0]
count = 1
for i in range(1,n):
    if las == l[i]:
        count += 1
    else:
        ans.append(count)
        count = 1
        las = l[i]
if sum(ans) != n:
    ans.append(count)
#print(ans)
a = 0
for i in range(len(ans)):
    if ans[i] != 1:
        for j in range(1,ans[i]):    
            a += j
print(a)

Submission status: Accepted

Solution code:
n=int(input())
d=[]
for i in range(n):
  s=list(input())
  s.sort()
  s="".join(s)
  d.append(s)
d.sort()
x=[]
k=1

for i in range(n-1):
  if d[i]==d[i+1]:
    k+=1
  else:
    x.append(k)
    k=1
x.append(k)

ans=[]
for i in range(len(x)):
  if x[i]>1:
    ans.append((x[i]*(x[i]-1))/2)
  else:
    ans.append(0)
    
    
print(int(sum(ans)))