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 math
from collections import Counter, defaultdict

def ncr(p):
	return p*(p-1)//2

n = int(input())

dct = defaultdict(int)

for i in range(n):
	counter = Counter(input().strip())
	tpl = tuple(sorted(counter.items()))
	dct[tpl] += 1


ans = sum(ncr(val) for val in dct.values() if val>1)
print(ans)

Submission status: Accepted

Solution code:
import math
from collections import Counter, defaultdict

def ncr(n, r):
	f = math.factorial
	return f(n)//f(r)//f(n-r)

n = int(input())

dct = defaultdict(int)

for i in range(n):
	counter = Counter(input().strip())
	tpl = tuple(sorted(counter.items()))
	dct[tpl] += 1


ans = sum(ncr(val, 2) for val in dct.values() if val>1)
print(ans)

Submission status: Accepted

Solution code:
dic = {}
cou = 0
lop = int(input())
for i in range(lop):
    N = input()
    N = list(N)
    N.sort()
    N = "".join(N)
    if N in dic:
        dic[N] += 1
    else:
        dic[N] = 1
lis = list(dic.values())
print(sum(list(map(lambda x: int((x * (x - 1)) / 2), lis))))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力


def main():
    # a, b
    n = int(input())
    s = []
    for _ in range(n):
        s.append(input())
    print(process(n, s))


def process(n, s):
    s = list(map(lambda s: ''.join(sorted(s)), s))
    hash = {}

    count = 0
    for i in range(0, n):
        if s[i] in hash:
            hash[s[i]] += 1
        else:
            hash[s[i]] = 1
        count += hash[s[i]] - 1
    return count


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 21 22:49:30 2019

@author: Yamazaki Kenichi
"""

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

for i in range(N):
    S[i] = ''.join(sorted(S[i]))
S.sort()
ans = 0
counta = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        counta += 1
    else:
        ans += counta*(counta+1)//2
        counta = 0
ans += counta*(counta+1)//2
print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
s = [0 for i in range(N)]
for i in range(N):
    s[i] = list(str(input()))
# print(s)

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

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

ans = 0
for key in dic.keys():
    if dic[key] > 1:
        ans += dic[key]*(dic[key] - 1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ss = ["".join(sorted(input())) for i in range(n)]
cnt = 0
counter = Counter(ss)
for k, v in counter.items():
  cnt += v*(v-1)/2
print(int(cnt))

Submission status: Accepted

Solution code:
from collections import *
 
n = int(input())
dic = defaultdict(int)
for i in range(n):
  dic["".join(sorted(input()))] += 1
 
ans = 0
for k,v in dic.items(): 
  ans += int(v*(v-1)/2)
  #ans += v*(v-1)//2
print(ans)

Submission status: Accepted

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

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

ans = 0
cnt = 1
for i in range(1, N):
  for j in range(10):
    if s[i][j] != s[i - 1][j]:
      break
  else:
    cnt += 1
    continue
  ans += cnt * (cnt - 1) // 2
  cnt = 1
ans += cnt * (cnt - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
N, *s = open(0).read().split()
sl = [''.join(sorted(c)) for c in s]
count = 0
dict = {}
for s in sl:
  if s in dict:
    count += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(count)

Submission status: Accepted

Solution code:
from math import factorial
N = int(input())
P = {}
ans = 0
def counter(s):
    ret = [0]*26
    for ss in s:
        ret[ord(ss)-ord('a')] += 1
    return tuple(ret)
for _ in range(N):
    s = input()
    c = counter(s)
    if not c in P.keys():
        P[c] = 1
    else:
        P[c] += 1
for v in P.values():
    if v > 1:
        ans += factorial(v) // factorial(2) // factorial(v-2)
print(ans)

Submission status: Accepted

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

    n = int(input())
    ans = 0
    used_s = {}
    for _ in range(n):
        s = "".join(sorted(input()))
        if not s in used_s.keys():
            used_s[s] = 0
        else:
            used_s[s] += 1
            ans += 1 * used_s[s]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

n = int(input())
ans = 0
used_s = {}
for _ in range(n):
    s = "".join(sorted(input()))
    if not s in used_s.keys():
        used_s[s] = 0
    else:
        used_s[s] += 1
        ans += 1 * used_s[s]

print(ans)

Submission status: Accepted

Solution code:
N= int(input())
s = []

for _ in range(N):
    s.append(input())

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

import collections
c = collections.Counter(s)
d = [i for i in c.values()]

counter = 0
from math import factorial
for i in d:
    if i > 1:
        counter += i*(i-1)/2

print(int(counter))

Submission status: Accepted

Solution code:
# coding: utf-8
import copy

N = int(input())

d = dict()

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

    if s in d.keys():
        res += d[s]
        d[s] += 1
    else:
        d[s] = 1

print(res)

Submission status: Accepted

Solution code:
import collections
from scipy.misc import comb

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

for i in range(len(s)):
    s[i] = tuple(sorted(s[i]))
    
c = collections.Counter(s)

cnt = 0
for c_ in c.values():
    cnt += comb(c_, 2, exact=True)
    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(n):
    tmp = ''.join(sorted(input()))
    if tmp in dic:
        dic[tmp] = dic[tmp]+1
    else:
        dic.setdefault(tmp, 0)
ans = 0
for i in dic:
    ans += 1/2 * dic[i] * (dic[i]+1)
print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter()

for si in s:
    si = "".join(sorted(si))
    c[si] += 1

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
d={}
for i in range(N):
    l=[0]*26
    for s in input():
        l[ord(s)-97]+=1
    l=tuple(l)
    if l in d:d[l]+=1
    else:d[l]=1

ans=0
for c in d:
    if d[c]==2:ans+=1
    elif d[c]!=1:ans+=d[c]*(d[c]-1)//2
print(ans)

Submission status: Accepted

Solution code:
# C
from collections import *

n = int(input())
dic = defaultdict(lambda: 0)
for i in range(n): dic["".join(sorted(input()))] += 1

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

Submission status: Accepted

Solution code:
n,*S=open(0).read().split()

from collections import defaultdict
dic = defaultdict(int)

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

print(sum([(v*(v-1))//2 for v in dic.values()]))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
from collections import defaultdict

def solve():
    N = int(input())
    
    d = defaultdict(int)
    for _ in range(N):
        s = input()
        l_s = list(s)
        l_s.sort()
        s = "".join(l_s)
        d[s] += 1

    ans = 0
    for key, value in d.items():
        # 和の公式
        ans += (((value-1)+1) * (value-1)) / 2
    print(int(ans))

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import collections

N =int(input())
s = ["".join(sorted(input())) for _ in range(N)]
c = collections.Counter(s)
cnt = 0
ans =0
for i in set(s):
    cnt = c[i]
    ans += cnt*(cnt-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
def nCr(n, r):
    """
    Calculate the number of combination (nCr = nPr/r!).
    The parameters need to meet the condition of n >= r >= 0.
    It returns 1 if r == 0, which means there is one pattern
    to choice 0 items out of the number of n.
    """
 
    # 10C7 = 10C3
    r = min(r, n-r)
 
    # Calculate the numerator.
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i
 
    # Calculate the denominator. Should use math.factorial?
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i
 
    return numerator // denominator
 
N = int(input())
Sum = 0
lists = []
for i in range(N):
    s = str(input())
    s_list = list(s)
    s_list.sort()
    m = "".join(s_list)
    lists.append(m)
c = collections.Counter(lists)
set_lists = list(set(lists))
for i in range(len(set_lists)):
    cnt = c[str(set_lists[i])]
    if cnt == 1:
        pass
    else:
        Sum += nCr(cnt,2)
print(Sum)

Submission status: Accepted

Solution code:
import collections
 
N = int(input())
Sum = 0
lists = []
for i in range(N):
    s = str(input())
    s_list = list(s)
    s_list.sort()
    m = "".join(s_list)
    lists.append(m)
c = collections.Counter(lists)
set_lists = list(set(lists))
for i in range(len(set_lists)):
    n = c[str(set_lists[i])]
    Sum += n * (n - 1) // 2
lists.sort()
print(Sum)

Submission status: Accepted

Solution code:
import collections

N = int(input())
Sum = 0
lists = []
for i in range(N):
    s = str(input())
    s_list = list(s)
    s_list.sort()
    m = "".join(s_list)
    lists.append(m)
c = collections.Counter(lists)
for i in set(lists):
    n = c[i]
    Sum += n * (n - 1) // 2
lists.sort()
print(Sum)

Submission status: Accepted

Solution code:
from collections import *
print(sum(v * (v-1)//2 for v in Counter(frozenset(Counter(input()).items()) for _ in range(int(input()))).values()))

Submission status: Accepted

Solution code:
from collections import *
print(sum(v * (v-1)//2 for v in Counter(tuple(sorted(Counter(input()).items())) for _ in range(int(input()))).values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n=int(input())
texts=[''.join(sorted(input())) for i in range(n)]
#print(texts)
c=collections.Counter(texts)
#print(c)
ans=sum([i*(i-1)/2 for i in c.values()])
print(int(ans))

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = ["".join(sorted(input())) for _ in range(n)]
c = collections.Counter(s)
ans = 0
for si in set(s):
  a = c[si]
  ans += a * (a - 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 key in c:
  n = c[key]
  ans += n * (n-1)/2

print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
dic = {}
for i in range(n):
    s = input()
    s = str(sorted(s))
    tmp = dic.get(s,0)
    ans += tmp
    dic[s] = tmp + 1

print(ans)

Submission status: Accepted

Solution code:
n =  int(input())

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

d = {}

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

ans = 0
for k, v in d.items():
    ans += int(v * (v - 1) / 2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ss = []
cnt = 0

# for _ in range(n):
#     ss.append(input())

# for i, s1 in enumerate(ss[0:n-2+1]):
#     for s2 in ss[i + 1:n-1+1]:
#         # print(s1, s2)
#         if sorted(s1) == sorted(s2):
#             cnt += 1

for _ in range(n):
    ss.append(input())

map = {}
for s in ss:
    sorted_s = ''.join(sorted(s))
    if sorted_s in map.keys():
        map[sorted_s] += 1
    else:
        map[sorted_s] = 1


# map = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# map = {'a': 10 ** 5}  # maxテスト
# print(map)

# あとはxC2の組み合わせの総数を求めれば良い


for n in map.values():
    if n > 1:
        # cnt += factorial(n) // (2 * factorial(n-2))
        cnt += (n * (n - 1)) // 2
    # print(n)
    # print(cnt)

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = [''.join(sorted(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
ans = sum((i * (i - 1)) // 2 for i in t.values())

print(ans)

Submission status: Accepted

Solution code:
import copy
import math
import sys

sys.setrecursionlimit(10 ** 7)

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


def split(word):
    return [char for char in word]


normalized_s_list = sorted([''.join(sorted(split(s))) for s in s_list])

uniqued_s_list = sorted(list(set(normalized_s_list)))

j = 0
counter = 0
result = 0
for s in normalized_s_list:
    if s == uniqued_s_list[j]:
        counter += 1
    else:
        result += (1 / 2) * counter * (counter - 1)
        j += 1
        counter = 1  # このとき s と uniqued_s_list[j] は同じはずなので

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

Submission status: Accepted

Solution code:
import math

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

def C(n, r):
    return P(n, r)//math.factorial(r)

N = int(input())
arr = {}
ans = 0
for _ in range(N):
    S = input()
    tmp_arr = []
    for i in range(10):
        tmp_arr.append(S[i])
    tmp_arr.sort()
    moji_sorted = ''
    for i in tmp_arr:
        moji_sorted += i
    if moji_sorted in arr:
        arr[moji_sorted] += 1
    else:
        arr[moji_sorted] = 1
# print(arr)

for i in arr.values():
    if i-2 >= 0:
        ans += C(i,2)

print(ans)

Submission status: Accepted

Solution code:
def main(ss):
    sorted_ss = ["".join(sorted(s)) for s in ss]
    # print(sorted_ss)
    visited = dict()
    count = 0
    for s in sorted_ss:
        if s in visited:
            count += visited[s]
            visited[s] += 1
        else:
            visited[s] = 1

    return count


n = int(input())
ss = []
for _ in range(n):
    ss.append(input())

print(main(ss))

Submission status: Accepted

Solution code:
import collections
n = int(input())
c = collections.Counter()
total = 0
for i in range(n):
    sn = input()
    l = list()
    sed = ''
    for j in range(10):
        l.append(sn[j])
    l.sort()
    for x in l:
        sed += x
    c[sed] += 1
for y in list(c.values()):
    total += y*(y-1)//2
print(total)

Submission status: Accepted

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

list = {}
for i in S:
  i=''.join(sorted(i))
  if i in list:
    list[i] = list[i] + 1
  else:
    list[i] = 1

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

Submission status: Accepted

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

dic = {}

for i in range(N):
    s = str(sorted(S[i]))
    if s not in dic:
        dic[s] = 0
    dic[s] += 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())

# スペース区切りの整数の入力
ar = []
for i in range(N):
  l = list(input())
  l.sort()
  ar.append("".join(l).__hash__())

# 数を数える
list = {}
for i in ar:
  if i in list:
    list[i] = list[i] + 1
  else:
    list[i] = 1
  
count = 0
for i in list.values():
  n = i - 1
  count = count + int(((n * (n + 1)) / 2))

print(count)

Submission status: Accepted

Solution code:
n = int(input())

rifa = {}
ans = 0
for i in range(n):
    s = input()
    s = str(sorted(s))
    tmp = rifa.get(s, 0)
    ans += tmp
    rifa[s] = tmp + 1
print(ans)

Submission status: Accepted

Solution code:
import statistics as stat
import collections as cl

N = int(input())

s_li = []
s_dic = {}
c = 0
for i in range(N) :
    s = ''.join(sorted(list(input())))
    # con = s_li.count(s)
    # if (con >= 1) :
    #     c += con
    # s_li.append(s)
    if (s in s_dic) :
        c += s_dic[s]
        s_dic[s] += 1
    else :
        s_dic[s] = 1

print(c)

Submission status: Accepted

Solution code:
import collections
n=int(input())
L=["".join(sorted(input()))for _ in range(n)]
C=collections.Counter(L)
a=0
for i in set(L):
    n=C[i]
    a+=n*(n-1)//2
print(a)

Submission status: Accepted

Solution code:
N = int(input())
s = [input() for i in range(N)]
ans ={}
num = 0

for i in range(N):
    s[i] = ''.join(sorted(s[i]))
    if s[i] in ans.keys():
        ans[s[i]] += 1
        num += ans[s[i]]
    else:
        ans[s[i]] = 0

print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
d = collections.Counter(s)
i = 0

for k in d:
  if d[k] > 1:
    i += int(d[k] * (d[k] - 1) / 2)

print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
dic = { c: i for i, c in enumerate('abcdefghijklmnopqrstuvwxyz')}

n = int(input())
strs = [tuple(sorted(map(lambda x: dic[x], input())))  for _ in range(n)]

a = {}
for s in strs:
  if s not in a:
    a[s] = 1
  else:
    a[s] += 1

print(sum(item[1]*(item[1]-1)//2 for item in a.items()))

Submission status: Accepted

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

for i in range(N):
    S = input()
    # ソートして一般化する
    S = sorted(S)
    str = ""
    for s in S:
        str += s
    d[str] += 1

# 足し上げる
ans = 0
for k in d.keys():
    num = d[k]
    ans += num*(num-1)//2

print(ans)

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
from collections import defaultdict
MOD = 10 ** 9 + 7

N = int(input())

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

for s in sdic:
    ans += sdic[s] * (sdic[s] - 1) // 2

print(ans)

Submission status: Accepted

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

count = 0
appeared = {}

for ss in s:
  sss = "".join(sorted(ss))
  if sss in appeared.keys():
    count += appeared[sss]
    appeared[sss] += 1
  else:
    appeared[sss] = 1

print(count)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc137/tasks/abc137_c
# C - Green Bin
import collections
import itertools
import math

n = int(input().split()[0])
s_list = []
c_list = []

for _ in range(n):
    s = input().split()[0]
    s_list.append(''.join(sorted(s)))

counter = collections.Counter(s_list)

def C(n, k):
    return math.factorial(n) / (math.factorial(k) * math.factorial(n-k))

p_list = [C(value, 2) for key, value in counter.items() if value > 1]
ans = int(sum(p_list))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=[]
a=[2,3,5,7,11,13,17,23,29,31]

i=0
while(i<n):
	stri=input()
	stri=list(map(ord,stri))
	stri=list(map(lambda h: h-97,stri))
	stri.sort()
	# print(stri)
	j=0
	number=1
	leng=len(stri)
	while(j<leng):
		number*=a[j]**(stri[j])
		j+=1
	d.append(number)
	i+=1
d.sort()
# print(d)
j=0
super=0
while(j<n):
	pfinder=d[j]
	summer=0
	notFound=True
	# print("Here",j)
	while( notFound and j<n):
		if(d[j]==pfinder):
			summer+=1
			j+=1	
		else:
			notFound=False
	# print(summer)
	super+=int(summer*(summer-1)/2)

print(super)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def comb(x):
    a = 1
    for i in range(1, x+1):
        a *= i
    return a//2


def main():
    N = int(input())
    M = {}
    for _ in range(N):
        s = ''.join(sorted(input()))
        if (s in M.keys()):
            M[s] += 1
            continue
        M[s] = 1

    print(sum([m*(m-1)//2 for m in M.values()]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
s_list = {}
out = 0

for i in range(N):
	s_in =''.join(sorted(list(input())))
	if s_in in s_list:
		s_list[s_in]+=1
	else:
		s_list[s_in]=1
    
for val in s_list.values():
  out += int(val*(val-1)/2)
    
print(out)

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce

n = int(input())

s = []
for _ in range(n):
    s.append(reduce(lambda x, y: x + y, sorted(input())))

# AC
print(sum([x * (x - 1) // 2 for x in Counter(s).values()]))

Submission status: Accepted

Solution code:
from collections import Counter

def list2str(l):
    s = ""
    for c in l:
        s += c
    return s

n = int(input())

s = []
for _ in range(n):
    s.append(list2str(sorted(input())))

"""
# TLE
cnt = 0
for i in range(1, n):
    for j in range(i):
        if s[j] == s[i]:
            cnt += 1
print(cnt)
"""

"""
# AC
print(sum([x * (x - 1) // 2 for x in Counter(sorted(s)).values()]))
"""

cnt = 0
a = Counter()
for b in s:
    if b in a:
        cnt += a[b]
    a[b] += 1
print(cnt)

Submission status: Accepted

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


def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


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

cnt_s = Counter(lst_s)

ans = 0
for v in cnt_s.values():
    if v == 1:
        continue
    else:
        ans += cmb(v, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def list2str(l):
    s = ""
    for c in l:
        s += c
    return s

n = int(input())

s = []
for _ in range(n):
    s.append(list2str(sorted(input())))

"""
# TLE
cnt = 0
for i in range(1, n):
    for j in range(i):
        if s[j] == s[i]:
            cnt += 1
"""

print(sum([x * (x - 1) // 2 for x in Counter(sorted(s)).values()]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


N = _i()

s_list = []
for _ in range(N):
    s = list(_s())
    s.sort()
    s_list.append(''.join(s))

s_list.sort()
s_list.append('')

ans = 0
pre = ''
cur = 0
for s in s_list:
    if s == pre:
        cur += 1
    else:
        ans += (cur + 1) * cur // 2
        cur = 0
        pre = s

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
string_list=["".join(sorted(list(input()))) for i in range(n)]
counter=Counter(string_list)
cnt=0
for k, v in counter.items():
  cnt+=int(v*(v-1)/2)
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s={}
for i in range(n):
    ss=list(input())
    ss.sort()
    ss=tuple(ss)
    try:
        s[ss]+=1
    except:
        s[ss]=1
tot=0
for i in s:
    if s[i]>1:
        tot+=(s[i]*(s[i]-1))//2
print(tot)

Submission status: Accepted

Solution code:
N = int(input())

mydic = {}

for i in range(N):
    mystr = "".join(sorted(input()))
    if mystr in mydic:
        mydic[mystr] += 1
    else:
        mydic[mystr] = 1
#print(mydic)
mycnt = 0
for i in mydic.values():
    if i > 1:
        mycnt += i*(i-1)//2
print(mycnt)

Submission status: Accepted

Solution code:
import sys, re
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 fractions import gcd

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

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

s.sort()

ans = 0
count = 0
tmp = ""
for i in s:
	if tmp == i:
		count += 1
	else:
		ans += count*(count+1)//2
		count = 0
		tmp = i
ans += count*(count+1)//2
print(ans)

Submission status: Accepted

Solution code:

qt = int(input())
sl = [input() for i in range(qt)]
d = {}
count = 0

for s in sl:
    s = "".join(sorted(s))
    if s in d:
        d[s] += 1
    else:
        d[s] = 1

for e in d.values():
    if e > 1:
        count += e * (e - 1) // 2

print(count)

Submission status: Accepted

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

d = {}
for s in S:
  s = "".join(sorted(s))
  if s in d:
    d[s] += 1
  else:
    d[s] = 1

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

Submission status: Accepted

Solution code:
import math as m

qt = int(input())
sl = [input() for i in range(qt)]
d = {}
count = 0

for s in sl:
    s = "".join(sorted(s))
    if s in d:
        d[s] += 1
    else:
        d[s] = 1

for e in d.values():
    if e > 1:
        count += m.factorial(e) // (m.factorial(e - 2) * 2)

print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = [""] * N

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

count = 0

for i in Counter(S).values():
    count += i * (i - 1) // 2

print(count)

Submission status: Accepted

Solution code:
#import pysnooper
import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy

setrecursionlimit(10**6)

n=int(stdin.readline().rstrip())
s=Counter(["".join(sorted(list(stdin.readline().rstrip()))) for _ in range(n)]).most_common()
cnt=0
for i in s:
    cnt+=sum(range(1,i[1]))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
list_anagram = [''.join(sorted(str(input()))) for i in range(N)]
 
list_anagram.sort()
ans = 0
 
x = 1
 
for i in range(1, N):
    if i != N - 1 and list_anagram[i] == list_anagram[i - 1]:
        x += 1
    elif i != N - 1 and list_anagram[i] != list_anagram[i - 1]:
        ans = ans + x * (x - 1) // 2
        x = 1
    elif i == N - 1 and list_anagram[i] == list_anagram[i - 1]:
        x += 1
        ans = ans + x * (x - 1) //2
    else:
        ans = ans + x * (x - 1) // 2
 
print(ans)

Submission status: Accepted

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

n = int(input())
d = defaultdict(int)
for _ in range(n):
	s = ''.join(sorted(list(input().strip())))
	d[s] += 1
ans = 0
for x in d.values():
	ans += x * (x - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections,functools
n=int(input())
texts=[''.join(sorted(input())) for i in range(n)]
#print(texts)
c=collections.Counter(texts)
ans=int(sum([i*(i-1)/2 for i in c.values()]))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
D = defaultdict(int)
for i in range(N):
    S = ''.join(sorted(input()))
    D[S] += 1

ans = 0
for key in D:
    M = D[key]
    if M >= 2:
        ans += M*(M-1)//2
    
print(ans)

Submission status: Accepted

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

count = 0
appeared = {}
for i in range(N):
    S[i] = "".join(sorted(list(S[i])))
    if S[i] in appeared:
        count += appeared[S[i]]
        appeared[S[i]] += 1
    else:
        appeared[S[i]] = 1
print(int(count))

Submission status: Accepted

Solution code:
N = int(input())
S_list = []

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

S_list.sort()
s_compare = S_list[0]
ans = 0
count = 1

for i in range(1,N):
    if S_list[i] == s_compare:
        count += 1
    else:
        ans += count * (count - 1) / 2
        count = 1
        s_compare = S_list[i]

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

Submission status: Accepted

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

d = {}
ans = 0
for s in S:
    s = "".join(sorted(s))
    if s in d:
        ans += d[s]
        d[s] += 1
    else:
        d[s] = 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = []
    for i in range(N):
        s = input()
        s = sorted(s)
        s = ''.join(s)
        S.append(s)
    S.sort()
    SS = sorted(list(set(S)))
    SN = []
    M = len(SS)
    l = 0
    r = len(S)
    for i in range(M):
        for j in range(l, r):
            if S[j] != SS[i]:
                r = j
                break
        SN.append(r - l)
        l = r
        r = len(S)        

    res = 0
    for sn in SN:
        res += sn * (sn - 1) // 2

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

Submission status: Accepted

Solution code:
def number(x):
  return int((1 + (1 + 8*x)**0.5) / 2)


N = int(input())
d = {}
for i in range(N):
  s = ''.join(sorted(input()))
  if s in d.keys():
    d[s] += number(d[s])
  else:
    d[s] = 0


print(sum(d.values()))

Submission status: Accepted

Solution code:
import sys

n = int(input())
originals = [input() for i in range(n)]

ret = {}
count = 0
for original in originals:
  str = "".join(sorted(list(original)))
  if str in ret:
    count += ret[str]
    ret[str] += 1
  else:
    ret[str] = 1
print(count)