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:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for _ in range(N):
        s=list(input())
        s.sort()
        s=''.join(map(str, s))
        dd[s]+=1
        
    ans=0
    
    for k,v in dd.items():
        ans+=(v*(v-1))//2
        
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
s_dict = {}
res = 0
for i in range(n):
    word = ''.join(sorted(input()))
    if word in s_dict.keys():
        res += s_dict[word]
        s_dict[word] += 1
    else:
        s_dict[word] = 1

print(res)

Submission status: Accepted

Solution code:
N = int(input())
# ソートすればアナグラムは同じ文字列になる
s = [str(sorted(input())) for i in range(N)]

ans = 0

# 既に見た文字列 : 累計出現回数
cnt = {}

for i in range(N):
    # 既に見た文字列の場合
    if s[i] in cnt:
        # 既にn個ある時、新しく1個増えると組み合わせはn通り増える
        ans += cnt[s[i]]
        cnt[s[i]] += 1
    # 始めて見る文字列
    else:
        cnt[s[i]] = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
cnt_s = Counter([" ".join(sorted(input())) for i in range(n)])
print(sum(v*(v - 1)//2 for v in cnt_s.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
d = {}
for i in range(N):
    s = list(map(str, input()))
    s.sort()
    s = "".join(s)
    d[s] = d[s] + 1 if s in d else 1
ans = 0
for i in d.values():
    ans += i * (i-1) // 2
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    n = I()
    lst = []
    for _ in range(n):
        st = S()
        st = list(st)
        st.sort()
        lst.append("".join(st))

    ans = 0
    dic = collections.Counter(lst)
    for key,value in dic.items():
        if value >= 2:
            ans += value*(value-1)//2
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

sort_s = []
for s in S:
    sort_s.append(''.join(sorted(s)))

count = dict(Counter(sort_s))

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

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
w = [""] * n
for i in range(n):
    w[i] = list(input())
    w[i].sort()
    w[i] = "".join(w[i])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=[''.join(sorted(input())) for _ in range(N)]
ans=0
import collections
from math import factorial
c = collections.Counter(S)
for i in c.values():
    if 2<=i:
        ans+=factorial(i)//(factorial(i-2)*factorial(2))

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=["".join(sorted(input())) for _ in range(N)]
d={}
ans=0
for s in S:
  if s in d:
    ans+=d[s]
    d[s]+=1
  else:
    d[s]=1
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, gcd
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 functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    S = [INPUT() for _ in range(N)]

    l = []
    for s in S:
        tmp_l = []
        for c in s:
            tmp_l.append(ord(c))
        tmp_l = sorted(tmp_l)

        tmp_s = ""
        for n in tmp_l:
            tmp_s += chr(n)
        l.append(tmp_s)

    C = Counter(l)
    ans = 0
    for c in C.values():
        if c > 1:
            ans += factorial(c) // (factorial(c - 2) * factorial(2))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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 = [input() for _ in range(N)]

new_S = []
for s in S:
    tmp = [_ for _ in s]
    tmp = ''.join(sorted(tmp))
    new_S.append(tmp)

c = Counter(new_S)

def combinations_count(n, r):
    if n < 2:
        return 0
    else:
        return factorial(n) // (factorial(n - r) * factorial(r))

ans = 0
for n in c.values():
    ans += combinations_count(n, 2)
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 fractions import gcd
from bisect import bisect, bisect_left, bisect_right

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)]

L = []
for s in S:
    c = "".join(sorted(s))
    L.append(c)
L.sort()
S = set(L)
# print(L)
C = Counter(L)
# print(C)
cnt = 0
for s in S:
    cnt += C[s] * (C[s] - 1) // 2
print(cnt)

Submission status: Accepted

Solution code:
from collections import*
print(sum(i * (i - 1) // 2 for i in Counter("".join(sorted(input())) for _ in [0] * int(input())).values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

ans = 0
word = {}
for i in range(N):
    S_word = sorted(input())
    key = "".join(S_word) 
    if key in word:
       word[key] += 1
       ans += word[key]
    else:
        word[key] = 0

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

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

    return result

N = int(input())
S = []
for _ in range(N):
    string = sorted(input())
    string = "".join(string)
    S.append(string)

S_counter = Counter(S)

ans = 0
for i in S_counter.values():
    if i != 1:
        number = cmb(i, 2)
        ans = ans + number

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

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

    return result

N = int(input())
S = []
for _ in range(N):
    string = sorted(input())
    string = "".join(string)
    S.append(string)

S_counter = Counter(S)

ans = 0
for i in S_counter.values():
    if i != 1:
        number = cmb(i, 2)
        ans = ans + number

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-08-18 14:31:47 +0900
# LastModified: 2020-08-18 14:42:13 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    n = int(input())
    ans_box = dict()
    for _ in range(n):
        s = sorted(map(str, input()))
        if ''.join(s) in ans_box.keys():
            ans_box[''.join(s)] += 1
        else:
            ans_box[''.join(s)] = 1
    ans = 0
    for key, value in ans_box.items():
        if value == 1:
            pass
        else:
            ans += value*(value-1)//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n  = int(input())

d = {}
for _ in range(n):
    string = "".join(sorted(list(input())))
    if string not in d:
        d[string] = 1
    else:
        d[string] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

dic = {}
for _ in range(N):
    s = ''.join(sorted(input()))

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

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

Submission status: Accepted

Solution code:
import collections

n=int(input())
lst=[]
ans=0

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

c=collections.Counter(lst)

for i in set(lst):
    x=c[i]

    ans+=x*(x-1)//2


print(ans)

Submission status: Accepted

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

import sys
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)


N = int(input())
S = []
for i in range(N):
    s = [c for c in input()]
    s.sort()
    S.append("".join(s))

counter = Counter(S)

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


print(res)

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]))

S_lst = {}
for i in range(N):
  if S[i] in S_lst:
    S_lst[S[i]] += 1
  else:
    S_lst[S[i]] = 1

#nC2 の計算
sum_ = 0
for key in S_lst:
    sum_ += int((S_lst[key] * (S_lst[key] - 1)) / 2)

print(sum_)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())

s = [0] * N

patterns = defaultdict(int)

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

    list_string = list(s[i])
    list_string.sort()

    list_string = "".join(list_string)
    patterns[list_string] += 1

ans = 0
for p in patterns.values():
    if p >= 2:
        ans += comb(p, 2)

print(ans)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
dict_a = {}

for i in range(n):
    s = "".join(sorted(input()))
    if s in dict_a:
        dict_a[s] += 1
        cnt += dict_a[s]
    else:
        dict_a[s] = 0

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = ["".join(sorted(input())) for _ in range(n)]
k = Counter(s)
ans = 0
for i, j in k.most_common():
	ans += j*(j-1)//2
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] = sorted(S[i])
S = sorted(S)
ans = 0
sa = 1
for i in range(N):
    if i == N-1:
        if sa == 1:
            break
        else:
            ans += int(sa * (sa - 1) / 2)
            break
    if S[i] == S[i+1]:
        sa += 1
    else:
        if sa == 1:
            continue
        else:
            ans += int(sa * (sa - 1) / 2)
            sa = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
count =0
store_list=[]
dictA={}
ans_list=[1]*n
for _ in range(n):
    s = input()
    s_list = list(s)
    s_list.sort()
    a =tuple(s_list)
    dictA.setdefault(a,0)
    dictA[a] +=1 
    #print(dictA)
#print(sum(ans_list))
total = 0
for value in dictA.values() :
    total += value*(value-1)/2
       
print(int(total))

Submission status: Accepted

Solution code:
N = int(input())

count = 0
dict = {}
for i in range(N):
    s = ''.join(sorted(input()))
    if(s in dict):
        dict[s] += 1
        count += dict[s]
    else:
        dict[s] = 0
print(count)

Submission status: Accepted

Solution code:
N=int(input())
List = []
ListMid = []
dictA = {}
res =0
for i in range(N):
  S = input()
  ListMid = list(S)
  ListMid.sort()
  a = tuple(ListMid)
  dictA.setdefault(a,0) 
  dictA[a] += 1
for value in dictA.values():
  if value == 1:
    pass
  else:
    res += value*(value-1)//2
print(res)

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]))
count = {}
for i in range(N):
    if s[i] in count:
        count[s[i]] += 1
    else:
        count[s[i]] = 1
#print(count)
print(sum(count[k] * (count[k] -1) // 2 for k in count))

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
  tmp = list(input())
  tmp.sort()
  s.append("".join(tmp))

s.sort()
m = {}
for i in range(len(s)):
  if s[i] in m.keys():
    m[s[i]] += 1
  else:
    m[s[i]] = 1

v = [int(x * (x-1) / 2) for x in list(m.values())]
print(sum(v))

Submission status: Accepted

Solution code:
from collections import Counter
# from fractions import gcd # >=Python3.5 # lcm（最小公倍数） = (a*b)//gcd(a,b)
# from fractions import Fraction
# from math import gcd # <Python3.5
# from math import sqrt

# input = sys.stdin.readline
# def main():

#---------------------------------------------------------

n = int(input())

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

c = Counter(clist)

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
S = [tuple(sorted(input())) for _ in range(N)]
c = collections.Counter(S)
ans = 0
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 = [''.join(sorted(input())) for _ in range(N)]
c = collections.Counter(S)
ans = 0
for i in set(S):
    n = c[i]
    ans += n*(n-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
    i = list(map(str,input()))
    i.sort()
    A.append(i)

A.sort()
tmp = A[0]
cnt = 0
ans = 0
for i in range(1,n):
    if tmp == A[i]:
        cnt += 1
    else:
        tmp = A[i]
        for j in range(cnt):
            ans += cnt
            cnt -= 1
if cnt >= 1:
    for j in range(cnt):
        ans += cnt
        cnt -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ss = sorted([sorted(input()) for _ in range(n)])
ans = 0
cnt = 1
for i in range(n - 1):
    if ss[i] != ss[i + 1]:
        ans += cnt * (cnt - 1) // 2
        cnt = 0
    cnt += 1
ans += cnt * (cnt - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input());

dic = {};
count = 0;
for i in range(N):
  chars = list(input());
  chars.sort();
  sorted_str = "".join(chars);
  # print(sorted_str);
  
  if sorted_str in dic:
    dic[sorted_str] += 1;
    count += dic[sorted_str];
  else:
    dic[sorted_str] = 0;
  
print(count);

Submission status: Accepted

Solution code:
N = int(input())
S = []
for _ in range(N):
	t = ''.join(sorted(input()))
	S.append(t)
SS = sorted(S)
ans = 0
t = 0
import math
for i in range(1, N):
	if SS[i - 1] == SS[i]:
		t += 1
		ans += t
	else:
		t = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
    
dic = dict()
ans = 0
for i in s:
    if "".join(sorted(i)) in dic:
        ans += dic["".join(sorted(i))]
        dic["".join(sorted(i))] +=1
    else:
        dic["".join(sorted(i))] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

strings = {}

for i in range(n):
    s = "".join(sorted(list(input())))
    if s in strings:
        strings[s] += 1
    else:
        strings[s] = 1

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
D = {}
for _ in range(int(input())):
    d = {}
    for s in sorted(input().rstrip()):
        if s in d:
            d[s] += 1
        else:
            d[s] = 1
    ds = "".join((k + str(v)) for k, v in d.items())
    if ds in D:
        D[ds] += 1
    else:
        D[ds] = 1

print(sum((v * (v - 1)) // 2 for v in D.values()))

Submission status: Accepted

Solution code:
def get( s ):
    sum=0
    s = sorted(s) 
    for i in range(len(s)):
        sum += int(ord(s[i])*(1<<(i*5)))
    return sum

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

H=[0]*n
for i in range(n):
    H[i]=get(S[i])

H.sort()
c=0
ans=0
for i in range(n-1):
    if H[i]==H[i+1]:
        c+=1
    else:
        ans += c*(c+1)//2
        c=0
ans += c*(c+1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
d = {}
ans = 0
for i in range(N):
    t = str(sorted(input()))
    if (d.get(t, "Not") == "Not"):
        d[t] = 1
    else:
        ans += d[t]
        d[t] += 1
print(ans)

Submission status: Accepted

Solution code:
S=[]
n = int(input())
for i in range(n):
    s=''.join(sorted(input()))
    S.append(s)
import collections
from scipy.special import comb
a=0
Sd=list(set(S))
c = collections.Counter(S)
for p in  range(len(Sd)):
    if c[Sd[p]] >=2:
        a  += comb(c[Sd[p]], 2, exact=True)
print(a)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = list(sorted(input()) for _ in range(n))

l = []
for li in s:
    st = ''
    for x in li:
        st += x
    l.append(st)

c = collections.Counter(l)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

t = []

for i in range(n):
    s = list(input())
    s.sort()
    t.append(s)

t.sort()

s = 0
c = 0
l = []

for i in range(n):
    if t[i] == l:
        c += 1
    else:
        s += c * (c+1) // 2
        c = 0
        l = t[i]

if c > 0:
    s += c * (c+1) // 2

print(s)

Submission status: Accepted

Solution code:
import math
from collections import defaultdict,deque
#from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()

"""========main code==============="""

#t=1
t=ii()
d=defaultdict(int)
for _ in range(t):
    k=ip()
    k.sort()
    k="".join(k)
    d[k]+=1
ans=0    
for i in d.keys():
    ans+=(d[i]*(d[i]-1))//2
print(ans)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    s = [sorted(list(input().rstrip())) for _ in range(n)]
    s.sort()

    ans = 0
    trial = s[0]
    tri_num = 0
    for i in s[1:]:
        if trial == i:
            tri_num += 1
        else:
            ans += tri_num*(tri_num+1)//2
            tri_num = 0
            trial = i
    if tri_num != 0:
        ans += tri_num*(tri_num+1)//2
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
s=[str(sorted(input())) for i in range(N)]
c=collections.Counter(s)
print(sum((v*(v-1))//2 for v in c.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = {}

for i in range(n):
  tmp = ''.join(sorted(input()))
  if tmp in s:
    s[tmp] += 1
  else:
    s[tmp] = 1

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

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for _ in range(n):
    s = list(input())
    s.sort()
    js = ''.join(s)
    if js in d:
        d[js] += 1
    else:
        d[js] = 1

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

Submission status: Accepted

Solution code:
N = int(input())

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

table = {}

ans = 0
for s in S:
    if s in table:
        table[s] += 1
    else:
        table[s] = 1

for k in table:
    ans += table[k] * (table[k] - 1)

ans //= 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = []
ans = 0
for i in range(n):
    a.append(sorted(input()))
a.sort()
flg = 0
for i in range(1,n):
    if a[flg] != a[i]:
        z = i - 1 - flg
        ans += (1 + z) * z // 2
        flg = i
z = n - 1 - flg
ans += (1 + z) * z // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [[0]*26 for _ in range(N)]
for i in range(N):
    for s in input():
        S[i][ord(s)-ord("a")] += 1
    S[i] = tuple(S[i])
res = 0
for value in Counter(S).values():
    res += value*(value-1)//2

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import math


def main():
    N = int(input())
    S = ["".join(sorted(input())) for i in range(N)]
    cnt = collections.Counter(S)
    ans = 0
    for v in cnt.values():
        if v != 1:
            ans += math.factorial(v) // (math.factorial(v - 2) * 2)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

N = I()
s = [sorted(list(_S())) for _ in range(N)]
ss = [0] * N
for i in range(N):
    ss[i]=''.join(s[i])

c = collections.Counter(ss)

# nC2
def nCr(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

key, counts = zip(*c.most_common())
ans = 0
for  count in counts:
    if count > 1:
        ans += nCr(count,2)
print(ans)
# list(map(lambda n,r: nCr(n,r),counts))

Submission status: Accepted

Solution code:
n=int(input())
arr=[]
from collections import Counter
c=Counter([])
for _ in range (n):
    s=input()
    z=[item for item in s]
    z.sort()
    z=tuple(z)
    c[z]+=1
ans=0
for item in c.values():
    ans+=(item*(item-1))//2
print (ans)

Submission status: Accepted

Solution code:
n = int(input())
S = ["".join(sorted(input())) for _ in range(n)]
counters = {}

for s in S:
    if s in counters:
        counters[s] += 1
    else:
        counters[s] = 1

ans = 0
for v in counters.values():
    if v == 1: continue
    ans += int(v * (v - 1) / 2)
print(ans)

Submission status: Accepted

Solution code:
import sys
input()
ans,d=0,{}
for ln in sys.stdin:
    s=''.join(sorted(ln))
    ans+=d.setdefault(s,0)
    d[s]+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
input()
ans,c=0,{}
for ln in sys.stdin:
    s=''.join(sorted(ln))
    ans+=c.setdefault(s,0)
    c[s]+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
N=int(input())
ans,c=0,{}
for _ in range(N):
    s=''.join(sorted(next(sys.stdin)))
    if s not in c:
         c[s]=0
    ans+=c[s]
    c[s]+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = ["".join(sorted(list(input()))) for _ in range(n)]
s.sort()
ans=0
pairs=1
for i in range(n-1):

    if s[i]==s[i+1]:
        pairs += 1
    else:      
        ans += pairs*(pairs-1)/2
        pairs =1
if pairs != 1:
    ans += pairs*(pairs-1)/2
    pairs =1
print(int(ans))

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
alp = [chr(i) for i in range(97, 97+26)]
L = []
for i in range(N):
    s = list(input())
    L.append(s)
for i in range(N):
    L[i].sort()
L.sort()

ans = 0
cnt = 1
for i in range(N-1):
    if L[i]==L[i+1]:
        cnt += 1
    else:
        ans += cnt*(cnt-1)//2
        cnt = 1
if cnt > 1:
    ans += cnt*(cnt-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
C = Counter(["".join(sorted(input())) for n in range(N)])
print(sum(n*(n-1)//2 for n in C.values()))

Submission status: Accepted

Solution code:
n = int(input())
counter = dict()
for i in range(n):
  s = ''.join(sorted(input()))
  if not s in counter:
    counter[s] = 0
  counter[s] += 1
print(sum(x * (x-1) // 2 for x in counter.values()))

Submission status: Accepted

Solution code:
import sys
N = int(input())
counts = {}
for _ in range(N):
    s = tuple(sorted(sys.stdin.readline()))
    counts[s] = counts.get(s, 0) + 1
ans = 0
for count in counts.values():
    if count > 1:
        ans += (count * (count - 1) // 2)
print(ans)

Submission status: Accepted

Solution code:
import collections


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    s = [''.join(sorted(list(input()))) for _ in range(n)]
    c = collections.Counter(s)
    cVals = c.values()
    cSum = 0
    for cVal in cVals:
        cSum += cVal * (cVal - 1) // 2
    print(cSum)


if __name__ == '__main__':
    main()