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())
D={}
for i in range(N):
  s="".join(sorted(input()))
  if s in D:
    D[s]+=1
  else:
    D[s]=1
ans=0
for x in D.values():
  if x>=2:
    ans+=x*(x-1)//2
print(ans)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	arr = []
	dictionary = {}
	cnt = 0
	for _ in range(n):
		arr.append(str(input()))
	for i in range(n):
		l = list(arr[i])
		l.sort()
		arr[i] = ''.join(str(l))
	for i in range(n):
		if arr[i] in dictionary:
			dictionary[arr[i]]+=1
			cnt+=dictionary[arr[i]]
		else:
			dictionary[arr[i]]=0
	print(cnt)
	



main()

Submission status: Accepted

Solution code:
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/PycharmProjects/atcoder/C-GreenBin.py
#
import collections

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

for i in range(n):
    s_i = sorted(input())
    s.append("".join(s_i))

# print(s)
c = collections.Counter(s)
pairs = c.most_common()

for i in range(len(pairs)):
    pair_i = pairs[i][1]
    if pair_i > 1:
        cnt += pair_i * (pair_i-1) // 2

print(cnt)

Submission status: Accepted

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

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:
n = int(input())
s = ["".join(sorted(str(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:
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:
from collections import Counter
n = int(input())
s = [[0]*10 for _ in range(n)]
ans = 0
for i in range(n):
    str_s = input()
    for j in range(10):
        s[i][j] = str_s[j]

dict_s = {}
for i in range(n):
    s[i].sort()
    si = "".join(s[i])
    dict_s.setdefault(si, 0)
    dict_s[si] += 1

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

Submission status: Accepted

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

l.sort()
cnt=0
tmp=0

for i in range(1,len(l)):
    if l[i]==l[i-1]:
        tmp += 1
    else:
        cnt += (tmp+1)*tmp//2
        tmp=0
cnt += (tmp+1)*tmp//2
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
l = list("")
for i in range(n):
    s = ''.join(sorted(input()))
    l.append(s)
c = Counter(l)
c = c.most_common()
ans = 0
for i in range(len(c)):
    x = c[i][1] - 1
    while x > 0:
        ans += x
        x -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = {}
for i in range(n):
  tmp = "".join(sorted(input()))
  if tmp in a:
    a[tmp] += 1
  else:
    a[tmp] = 1
res = [i*(i-1)//2 for i in a.values()]
print(sum(res))

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

# [[1, 1], [0, 1], [0, 0], [1, 0]]
dic = {}

for i in range(n):
    sen = '・'.join(sorted(s[i]))
    #print(sen)
    if sen not in dic:
        dic[sen]=1
    else:
        dic[sen]+=1    
#print(dic)
ans = 0
for i in dic.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
dic = {}
for i in range(n):
    s = "".join(sorted(input()))
    dic.setdefault(s, 0)
    dic[s] += 1
ans = 0
for x in dic.values():
    ans += int(x * (x - 1) / 2)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
dic = {}
for i in range(n):
    t = ''.join(sorted(input()))
    dic.setdefault(t, 0)
    dic[t] += 1
ans = 0
for x in dic.values():
    ans += x * (x - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import math
n=int(input())
s=list(input() for _ in range(n))
S=[]
ans=0
for i in range(n):
    s1=sorted(s[i])
    S.append(''.join(s1))
S1=Counter(S)
for i in S1:
   if S1[i]>1:
        ans+=math.factorial(S1[i])//(math.factorial(S1[i]-2)*2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s=[""]*n
for i in range(n):
  s[i]=input()

def main():
    t = ["".join(sorted(t)) for t in s]
    c = Counter(t)
    cnt = 0
    for v in c.values():
        cnt += v*(v-1)
    print(cnt//2)
main()

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
N = int(input())
S = []
for i in range(N):
    tmp = input()
    tmp = sorted(list(tmp))
    S.append("".join(tmp))

cnt = Counter(S)
ans = 0
for i in cnt:
    if cnt[i] > 1:
        ans += factorial(cnt[i]) // (factorial(cnt[i] - 2) * factorial(2))

print(ans)

Submission status: Accepted

Solution code:
from math import factorial, ceil
n = int(input())
d = {}
for i in range(n):
    s = input()
    ns = ''.join(sorted(s))
    if ns not in d.keys():
        d[ns] = 1
    else:
        d[ns] += 1
ans = 0
for i, v in d.items():
    if v != 1:
        ans += factorial(v) /(factorial(v-2)*2)
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = [sorted(list(input())) for _ in range(N)]
import collections
A = []
for a in S:
    A.append("".join(a))
count = 0
c = collections.Counter(A)
for a in c.values():
    count += a*(a-1)//2

print(count)

Submission status: Accepted

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

dic = {}
for w in s:
    if w in dic:
        dic[w] += 1
    else:
        dic[w] = 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    s=input()
    k=[]
    for i in range(10):
        k.append(s[i])
    k.sort()
    p="".join(k)
    a.append(p)
a.sort()
bef=a[0]
cnt=0
sum=0
for i in range(n):
    if bef==a[i]:
        cnt+=1
    else:
        sum+=(cnt*(cnt-1)//2)
        cnt=1
        bef=a[i]
    if i==n-1:
        sum+=(cnt*(cnt-1)//2)
print(sum)

Submission status: Accepted

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

for i in range(N):
    s=input()
    t=sorted(s)
    word="".join(t)

    if word in dic:
        dic[word]+=1
    else:
        dic[word]=1

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

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
L = []
for i in range(N):
    s = input()
    L.append(sorted(s))
L = sorted(L)
ans = 0
comb = 0
a = 0
b = 1
while b <= N-1:
    if L[a] == L[b] and b-a == 1:
        comb += 2
        b += 1
    elif L[a] == L[b]:
        comb += 1
        b += 1
    elif comb != 0:
        ans += (math.factorial(comb))//((math.factorial(comb-2))*2)
        a = b
        b = a+1
        comb = 0
    else:
        a = b
        b = a + 1
if comb != 0:
    ans += (math.factorial(comb)) // ((math.factorial(comb - 2)) * 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())

temp_dict = defaultdict(int)
for _ in range(n):
    s_list = list(input())
    s_list.sort()
    s = "".join(s_list)
    temp_dict[s] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

temp_dict = {}
for _ in range(n):
    s_list = list(input())
    s_list.sort()
    s = "".join(s_list)

    if s not in temp_dict:
        temp_dict[s] = 0
    temp_dict[s] += 1

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

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
s = [''.join(sorted(input())) for _ in range(N)]
ans = 0
for count in Counter(s).values():
    ans += comb(count, 2, exact=True)
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n=int(input())
    lis=[]
    ans=0
    for i in range(n):
        s=input()
        s_c=Counter(s)
        s_ct=tuple(sorted(s_c.items()))
        lis.append(s_ct)
    lis_c=Counter(lis)
    for li in lis_c.items():
        x=li[1]
        if x>=2:
            ans+=x*(x-1)//2
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
dict={}
ans=0
for i in range(n):
  s=''.join(sorted(input()))
  dict[s]=dict.get(s,-1)+1
  ans+=dict[s]
print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import Counter
import math

N = int(input())

def generate_prime_numbers():
    search_range = 150
    search_list = [True for i in range(0, search_range+1)]
    search_list[0] = False
    search_list[1] = False
    search_list[2] = True
    for i in range(2, search_range+1):
        for j in range(i*2, search_range+1, i):
            search_list[j] = False
    prime_numbers = [i for i in range(search_range+1) if search_list[i] == True]
    return prime_numbers[:27]

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

prime_numbers = generate_prime_numbers()
encoded_strings = []
for i in range(N):
    S = input()
    encoded_string = 1
    for c in S:
        char_to_int = ord(c) - ord('a')
        encoded_string *= prime_numbers[char_to_int]
    encoded_strings.append(encoded_string)
# print(encoded_strings)

ans = 0
# for comb in itertools.combinations(encoded_strings, 2):
#     if comb[0] == comb[1]:
#         ans += 1
counter = Counter(encoded_strings)
for i in counter.values():
    if i > 1:
        ans += combination(i, 2)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = [list((map(str,input().split()))) for _ in range(N)]

A = []
for i in S:
    ew_str = ''.join(sorted(i[-1]))
    A.append(ew_str)

cnt = Counter(A)
cnt = list(cnt.values())
#print(cou)

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

Submission status: Accepted

Solution code:
import operator as op
from functools import reduce
import math
	

def ncr(n, r):
	if n < r:
		return 0
	else :
		return (math.factorial(n)) / (math.factorial(n-r)*math.factorial(r))

def countPairs(str_arr, N):
	str_cnt = [0]
	str = str_arr[0]
	j = 0
	for i in range(N):
		if(str_arr[i]==str):
			str_cnt[j] += 1
		else:
			str = str_arr[i]
			j += 1
			str_cnt.append(0)
			str_cnt[j] += 1
	count = 0
	for i in range(len(str_cnt)):
		count += ncr(str_cnt[i],2)
	print(int(count))

def main():
	N = int(input())
	str_arr = []
	for i in range(N):
		str = input()
		str_arr.append(sorted(str))
	str_arr = sorted(str_arr)
	countPairs(str_arr, N)


main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ans = []
cnt = 0
for i in range(n):
    s = input()
    l = list(s)
    l.sort()
    ans.append("".join(l))
c = Counter(ans)


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

print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n,*S = sys.stdin.read().split()
n = int(n)

def main():
    t = [''.join(sorted(t)) for t in S]
    c = Counter(t)
    cnt = 0
    for v in c.values():
        cnt += v*(v-1)
    print(cnt//2)
main()

Submission status: Accepted

Solution code:
N = int(input())
D = {}
for _ in range(N):
    s = input()
    r = ''.join(sorted([c for c in s]))
    if r in D:
        D[r] += 1
    else:
        D[r] = 1
ans = 0
for v in D.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Accepted

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

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

from collections import Counter
counter = Counter(S)

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
S = Counter()
ans = 0
for _ in range(n):
    s = "".join(sorted(input()))
    if S[s] > 0:
        ans += S[s]
    S[s] += 1

print(ans)

Submission status: Accepted

Solution code:
#a=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#A=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','R','V','W','X','Y','Z']

import sys
import numpy as np

n=int(sys.stdin.readline())
s=list([sorted(list(sys.stdin.readline().strip())) for _ in range(n)])
s.sort()

pre=''
cnt=0
ans=0
for i in range(n):
    now=s[i]
    if pre==now:
        cnt+=1
    else:
        pre=now
        ans+=(cnt+1)*cnt//2
        cnt=0
ans+=(cnt+1)*cnt//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
print(sum(comb(n, 2, exact=True) for n in Counter("".join(sorted(input())) for _ in range(int(input()))).values()))

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
print(sum(comb(n, 2, exact=True) for n in Counter("".join(sorted(input())) for _ in range(N)).values()))

Submission status: Accepted

Solution code:
from collections import defaultdict

a = int(input())


d=defaultdict(int)
# d={}

for _ in range(a):
     b = input()
     c = "".join(sorted(b))
     d[c] +=1
    #  if d.get(c) == None: d[c] = 0
    #  else: d[c] +=1

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


print(sum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    anag = {}
    for i in range(n):
        s = sorted(input())
        if not str(s) in anag.keys():
            anag[str(s)] = 1
        else:
            anag[str(s)] += 1
    s = 0
    for k in anag.keys():
        s += anag[k]*(anag[k]-1)//2
    print(s)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    dic = {}
    for i in range(n):
        s = "".join(sorted(input()))
        if s in dic:
            dic[s] += 1
        else:
            dic[s] = 1

    def comb(n,r,p = mod):
        r = min(r,n-r) # nCr
        res = 1
        div = 1 # nCr := n*n-1*...n-r+1 // 1*2*3...r
        for i in range(n-r+1,n+1):
            res *= i
            res %= p
        for i in range(1,r+1):
            div *= i
            div %= p
        res *= pow(div,p-2,p) # 掛け算に
        res %= p
        return res
    ans = 0
    for k,v in dic.items():
        ans += v * (v-1) // 2
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    L = [''.join(sorted(list(input()))) for _ in range(n)]
    L.sort()
    ans = 0
    for k, g in groupby(L):
        size = len(list(g))
        ans += size * (size-1) // 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    import sys
    input = sys.stdin.readline

    N = int(input())

    d = defaultdict(int)

    def string_sort(s):
        return str(sorted(s))
        # str(sorted('cba'))
        # "['a', 'b', 'c']"

        # return ''.join(sorted(s))

    ans = 0
    for _ in range(N):
        s = string_sort(input().rstrip())
        ans += d[s]
        d[s] += 1
    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    import sys
    input = sys.stdin.readline

    N = int(input())

    d = defaultdict(int)

    def string_sort(s):
        return ''.join(sorted(s))

    ans = 0
    for _ in range(N):
        s = string_sort(input().rstrip())
        ans += d[s]
        d[s] += 1
    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip()) for _ in range(n)]

set_list = {}

for i in li:
    i.sort()
    s = "".join(i)
    if s in set_list:
        set_list[s] += 1
    else:
        set_list[s] = 1

point = 0

for i in set_list.values():
    point += i*(i-1)//2

print(point)

Submission status: Accepted

Solution code:
from sys import stdin
from math import factorial

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

n = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip()) for _ in range(n)]

set_list = {}

for i in li:
    i.sort()
    s = "".join(i)
    if s in set_list:
        set_list[s] += 1
    else:
        set_list[s] = 1

point = 0

for i in set_list.values():
    if i >= 2:
        point += combinations_count(i,2)

print(point)

Submission status: Accepted

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

ans = 0
for v in S.values():
  if v < 2: ans += v
  else: ans += v * (v+1) // 2
print(ans)

Submission status: Accepted

Solution code:
# Problem C - Green Bin

from collections import Counter

# input
N = int(input())
s_list = []
for i in range(N):
    s = sorted(input())
    s = "".join(s)
    s_list.append(s)

# initialization
ct = Counter(s_list)
ans = 0

# count
for c in ct.most_common():
    tmp_ct = c[1]
    if tmp_ct==1:
        continue
    ans += tmp_ct * (tmp_ct-1) // 2

# output
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [input() for i in range(N)]

memo = defaultdict(int)
L = []
for i in range(N):
    l = list(S[i])
    l.sort()
    l = "".join(l)
    memo[str(l)] += 1
# print(memo)
ans = 0
for m in memo.values():
    ans += m * (m - 1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
D = {}
cnt = 0

for i in range(N):
    S=''.join(sorted(input()))#文字列で受け取る
    A.append(S)

for s in A:
    if s in D:
        cnt += D[s]
        D[s] += 1 #D[s]=これまでのs登場回数
    else:
        D[s]=1
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
d = defaultdict(lambda: 0)
for _ in range(n):
    s = list(input())
    s.sort()
    d[''.join(s)] += 1
ans = 0
for v in d.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    dict = {}
    s = [",".join(sorted(input())) for _ in range(n)]
    count = 0
    for i in range(n):
        if s[i] in dict:
            count += dict[s[i]]
            dict[s[i]] += 1
        else:
            dict[s[i]] =1

    print(count)
resolve()

Submission status: Accepted

Solution code:
n = int(input())

from collections import defaultdict
dic = defaultdict(int)

for _ in range(n):
    s = list(input())
    s = tuple(sorted(s))
    dic[s] += 1

ans = 0
for s in dic:
    ans += dic[s] * (dic[s] - 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:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    D = defaultdict(lambda : 0)
    ans = 0
    for i in range(n):
        d = [0]*26
        s = input()
        for j in s:
            d[ord(j)-ord("a")] += 1
        td = tuple(d)
        ans += D[td]
        D[td] += 1
    print(ans)
    return

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

Submission status: Accepted

Solution code:
n = int(input())
import collections
chk = collections.deque([])
for i in range(n):
  s = input()
  s = sorted(s)
  chk.append(''.join(s))
#chk = list(chk)
c = collections.Counter(chk)
c = dict(c)
ans = 0
for i in c.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
# MOD = 10 ** 9 + 7
# INF = float("inf")


def main():
    n = int(input())
    l = []
    for i in range(n):
        s = sorted(input())
        l.append(''.join(s))
    l_cnt = Counter(l)
    ans = 0
    for i in l_cnt.values():
        ans += i*(i-1) // 2
    print(ans)


    print()
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = [input()for  _ in range(n)]
cnt = {}
for x in s:
    p = list(x)
    p.sort()
    b = "".join(p)
    if "".join(p) not in cnt:cnt[b] = 1
    else:cnt[b] +=1
ans = 0
for key in cnt:
    v = cnt[key]
    ans +=v*(v-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
# s=[input() for i in range(n)]
s=[]
for i in range(n):
  S=input()
  S=list(S)
  S.sort()
  S=''.join(S)
  s.append(S)
from collections import defaultdict as dd
d=dd(int)
for ss in s:
  d[ss]+=1
ans=0
for k,v in d.items():
  ans+=v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
l = []
for i in range(N):
  s = input()
  ls = []
  for j in range(10):
    ls.append(s[j])
  ls.sort()
  sm = ""
  for j in range(10):
    sm += ls[j]
  l.append(sm)

cnt = 0
c = collections.Counter(l)
for i in c.values():
  cnt += i*(i-1)//2
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
Sn = [ ''.join(sorted(input())) for _ in range(N)]
Sn_key = {}
for s in Sn:
    if s not in Sn_key:
        Sn_key[s] = 1
    else:
        Sn_key[s] += 1
cnt = 0

for v in Sn_key.values():
    if v >= 2:
        cnt += v * (v-1) // 2
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

cnt = defaultdict(int)

ans = 0
for _ in range(N):
    s = list(input())
    s.sort()
    s = str(s)
    ans += cnt[s]
    cnt[s] += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
counter = defaultdict(int)
N = int(input())
for i in range(N):
    counter[tuple(sorted(list(input())))] += 1
ans = 0
for n in counter.values():
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = {}
for _ in range(N):
  a = "".join(sorted(input()))
  if a not in s:
    s[a] = 0
  else: s[a] += 1

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

Submission status: Accepted

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
di = {}
for i in s:
    tmp = [0]*26
    for c in i:
        tmp[ord(c)-ord('a')] += 1
    tmp = tuple(tmp)
    if tmp in di:
        di[tmp] += 1
    else:
        di[tmp] = 1

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

Submission status: Accepted

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

t = collections.defaultdict(int)
ans = 0

for _ in range(N):
    s = list(input())
    s.sort()
    t[''.join(s)] += 1


for x in t:
    ans += (t[x] * (t[x] - 1)) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
counts = dict()
for i in range(N):
    s = input()
    s_lst = list(s)
    s_lst.sort()
    s = "".join(s_lst)
    if s in counts:
        counts[s] += 1
    else:
        counts[s] = 1

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

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = 0

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

print(ans)