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.

Solution (Python):
n=int(input())
a=[list(input()) for i in range(n)]
for i in range(n):
    a[i]=sorted(a[i])
import collections
import math
d=collections.defaultdict(int)
for i in a:
    d[''.join(i)]+=1
ans=0
for i in d.values():
    if i>2:
        ans+=math.factorial(i)//(math.factorial(i-2)*math.factorial(2))
    elif i==2:
        ans+=1
print(ans)

Solution (Python):
import sys
from collections import Counter
M = {}
N = int(input())
ans = 0
for _ in range(N):
  S = input()
  S = ''.join(sorted(S))
  if S not in M:
    M[S] = 1
  else:
    ans += M[S]
    M[S] += 1
print(ans)

Solution (Python):
n = int(input())
l = {}
count = 0

for i in range(n):
    s = ''.join(sorted(input()))
    if i == 0:
        l[s] = 1
        continue

    
    if s in l:
        count+=l[s]
        l[s] += 1
    else:
        l[s] = 1

print (count)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    d = defaultdict(int)
    ans = 0
    for _ in range(N):
        S=str(sorted(ST()))
        ans += d[S]
        d[S]+=1
    print(ans)
        

if __name__ == '__main__':
    main()

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    d = defaultdict(int)
    ans = 0
    for _ in range(N):
        S=str(sorted(ST()))
        ans += d[S]
        d[S]+=1
    print(ans)
        

if __name__ == '__main__':
    main()

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

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

 
a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)

 
y = 0
for j in a:
    y += j*(j-1)/2
 
print(int(y))

Solution (Python):
import itertools
n = int(input())
l = ["".join(sorted(input())) for i in range(n)]
d = {}
cnt = 0

for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    cnt += d[l[i]]
    d[l[i]] += 1

print(cnt)

Solution (Python):
n = int(input())
L = []
for i in range(n):
  s = input()
  cur = []
  for j in range(10):
    cur.append(s[j])
  cur.sort()
  t = ''
  for j in range(10):
    t += cur[j]
  L.append(t)
L.sort()
ans = 0
L.append('end')
temp = 1
for i in range(1,n+1):
  if L[i] != L[i-1]:
    ans += temp*(temp-1)//2
    temp = 1
  else:
    temp += 1
print(ans)

Solution (Python):
N = int(input())
words = []
for i in range(N):
  s = str(input())
  s_sort = ''.join(sorted([ch for ch in s]))
  words.append(s_sort)

words_sort = sorted(words)

counter = 0
target_word = ''
counts = []
for word in words_sort:
  if word == target_word:
    counter += 1
  else:
    counts.append(counter)
    counter = 1
    target_word = word

counts.append(counter)

from math import factorial

ans = 0
for cnt in counts:
  if cnt > 1:
    ans += factorial(cnt) // factorial(cnt-2) // 2

print(ans)

Solution (Python):
n = int(input())
l = ["".join(sorted(input())) for _ in range(n)]
ans = 0
d = {}
for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    ans += d[l[i]]
    d[l[i]] += 1
print(ans)

Solution (Python):
n=int(input())
dic={}
ans=0
for _ in range(n):
    S=''.join(sorted(list(input())))
    if S not in dic.keys():
        dic[S]=1
    else:
        ans+=dic[S]
        dic[S]+=1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

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

ss.sort()

ans = 0
while len(ss) != 0:
    f = ss.pop(0)
    c = 1
    while len(ss) > 0:
        d = ss[0]
        if f == d:
            ss.pop(0)
            ans += c
            c += 1
        else:
            break

print(ans)

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

Solution (Python):
import sys, 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

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

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

dic = defaultdict(int)
for x in S:
	dic[x] += 1

wa = 0
for n in dic.values():
	wa += int(n*(n - 1)/2)

print(wa)

Solution (Python):
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
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

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)]
dic = defaultdict(int)
for x in s:
	dic[x] += 1
ans = 0
for n in dic.values():
	ans += n*(n-1)//2
print(ans)

Solution (Python):
import sys, 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

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

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

ku = []
kugiri = 0
for i in range(N - 1):
	if S[i] == S[i + 1]:
		kugiri += 1
		if i == N - 2:
			ku.append(kugiri + 1)

	else:
		ku.append(kugiri + 1)
		kugiri = 0

wa = 0
for i in range(len(ku)):
	if ku[i] == 1:
		continue
	wa += comb_count(ku[i], 2)

print(wa)

Solution (Python):
import collections
n = int(input())
s_list = ["".join(sorted(input())) for _ in range(n)]
s_dict = {}
ans = 0
for s in s_list:
    if s in s_dict:
       ans += s_dict[s]
       s_dict[s] += 1
    else:
        s_dict[s] = 1
print(ans)

Solution (Python):
def main():
  import sys
  import math
  import collections
  n=int(input())
  word=["".join(sorted(str(sys.stdin.readline().rstrip()))) for _ in range(n) ]
  dic=collections.Counter(word)
  ans=0
  for k in dic.values():
    if k>1:
      ans+=math.factorial(k)//math.factorial(k-2)//2
  print(ans)
  
if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  import math
  import collections
  n=int(input())
  word=["".join(sorted(str(input()))) for _ in range(n) ]
  dic=collections.Counter(word)
  ans=0
  for k in dic.values():
    if k>1:
      ans+=math.factorial(k)//math.factorial(k-2)//2
  print(ans)
  
if __name__=="__main__":
  main()

Solution (Python):


def main():
    N=int(input())
    r=[]
    for i in range(0,N):
        r.append(sorted(input()))
    r.sort()
    s=1
    ans=0
    for i in range(0,N-1):
        if r[i]==r[i+1]:
            s=s+1
        else:
            ans+=s*(s-1)//2
            s=1
    print(ans+s*(s-1)//2)

if __name__ == '__main__':
    main()

Solution (Python):
def letternumber(S):
    alphabet=['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']
    s=[0 for i in range(0,26)]
    for i in range(0,10):
        s[alphabet.index(S[i])]+=1
    return s

def main():
    N=int(input())
    r=[]
    for i in range(0,N):
        r.append(letternumber(input()))
    r.sort()
    s=1
    ans=0
    for i in range(0,N-1):
        if r[i]==r[i+1]:
            s=s+1
        else:
            ans+=s*(s-1)//2
            s=1
    print(ans+s*(s-1)//2)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
d = {}
ans = 0
for i in range(N):
    s = input()
    s = "".join(sorted(list(s)))
    d.setdefault(s, -1)
    d[s] += 1
    ans += d[s]
print(ans)

Solution (Python):
def main():
    N = int(input())
    from collections import defaultdict
    cnt = defaultdict(int)
    for _ in range(N):
        s = input()
        s = "".join(sorted(s))
        cnt[s] += 1

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
s_list = []
for _ in range(N):
  s_list.append(''.join(sorted(list(input()))))

from collections import Counter
c = Counter(s_list)

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

Solution (Python):
N=int(input())
s=[0]*N
essence={}
count=0
for i in range(N):
   s_i="".join(sorted(input()))
   s[i]=s_i
   if s[i] not in essence:
      essence.setdefault(s[i],1)
   else:
      count+=essence[s[i]]
      essence[s[i]]+=1

print(count)

Solution (Python):
n = int(input())
S = ["".join(sorted(input())) for _ in range(n)]
ans = 0
dict = {}
for s in S:
    tmp = dict.get(s, 0)
    ans += tmp
    dict[s] = tmp + 1
print(ans)

Solution (Python):
def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

N = int(input())
s = [0]*(N+1)
for i in range(1, N+1):
    s[i] = input()

ss = {}
for i in range(1, N+1):
    now = "".join(sorted(list(s[i])))
    if now in ss:
        ss[now] += 1
    else:
        ss[now] = 1

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

print(ans)

Solution (Python):
N = int(input())
S = {}
for i in range(N):
  s = "".join(sorted(input()))
  try:
    S[s]+=1
  except:
    S[s]=1
ans = 0

for i in S.keys():
  n = S[i]
  ans += n*(n-1)//2

print(ans)

Solution (Python):
n = int(input())
res = dict()
for i in range(n):
    m = input()
    m = sorted(m)
    m = ''.join(m)
    if m in res:
        res[m] += 1
    else:
        res[m] = 1

for i in res:
  res[i] = res[i]*(res[i]-1)//2
print(sum(res.values()))

Solution (Python):
from collections import defaultdict

N = int(input())
d = defaultdict(int)

for _ in range(N):
    d[tuple(sorted(input()))]+=1

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

print(ans)

Solution (Python):
n=int(input())
d=dict()
for i in range(n):
    s="".join(sorted(input()))
    if s in d:
        d[s]+=1
    else:
        d[s]=1
ans=0
for k in d.values():
    ans+=k*(k-1)//2
print(ans)

Solution (Python):
n = int(input())
str_l = []

for i in range(n):
    str_l.append("".join(sorted(input())))

str_l.sort()
ans = 0
num = 0
st = "a"

for j in range(n):
    if str_l[j] == st:
        num += 1
        ans += num
    else:
        num = 0
        st = str_l[j]

print(ans)

Solution (Python):
N = int(input())
count_dict = {}
add_dict = {}
for _ in range(N):
    s = "".join(sorted(list(input())))
    if s in count_dict:
        count_dict[s] += add_dict[s]
        add_dict[s] += 1
    else:
        count_dict[s] = 0
        add_dict[s] = 1

print(sum(count_dict.values()))

Solution (Python):
def nC2(num):
  if num <= 1:
    return 0
  else:
    return int(num * (num-1) / 2)

n = int(input())
text_dict = {}
for _ in range(n):
  sorted_text = ''.join(sorted(list(input())))
  if sorted_text in text_dict.keys():
    text_dict[sorted_text] = text_dict[sorted_text] + 1
  else:
    text_dict[sorted_text] = 1

count = 0
for text, c in text_dict.items():
  count += nC2(c)
  
print(count)

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
for s in ss:
    d[''.join(sorted(s))] += 1
ans = 0
for v in d.values():
    ans += v * (v-1) // 2
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = [str(sorted(input())) for _ in range(N)]
count = 0
for i in Counter(S).values():
  count += i*(i-1)//2
print(count)

Solution (Python):
n = int(input())
a = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = "".join(s)
    a.setdefault(s, 0)
    a[s] += 1
count = 0
for i in a.values():
    count += i * (i - 1) // 2
print(count)

Solution (Python):
N = int(input())
from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    s = input()
    s = sorted(s)
    s = "".join(s)
    d[s] += 1

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

print(ans)

Solution (Python):
n = int(input())
S = []
for i in range(n):
    s = input()
    li = sorted(list(s))
    S.append(li)
ans = 0
S.sort()
fac = S[0]
cnt = -1
for i in range(n):
    if S[i] == fac:
        cnt += 1
    else:
        fac = S[i]
        ans += max((cnt+1)*cnt//2,0)
        cnt = 0
ans += max((cnt+1)*cnt//2,0)
print(ans)

Solution (Python):
n=int(input())
d=dict()

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():
  ans+=x*(x-1)//2
print(ans)

Solution (Python):
N = int(input())
S = [input() for i in range(N)]
for i in range(N):
    S[i] = ''.join(sorted(S[i]))

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

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

Solution (Python):
from functools import lru_cache
from itertools import groupby

@lru_cache(maxsize=None)
def comb(n, r):
    if r == 1:
        return n
    if n < r:
        return 0
    return comb(n-1, r-1) + comb(n-1, r)

for n in range(10**5):
    comb(n, 2)

N = int(input())
S = tuple(sorted(tuple(sorted(input())) for _ in range(N)))

print(sum(comb(len(tuple(g)), 2) for _, g in groupby(S)))

Solution (Python):
from collections import Counter

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

S_cnt = Counter(S)

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

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


anagrams = defaultdict(int)
for _ in range(N):
    chars = input()
    d = defaultdict(int)
    tuple_d = tuple(sorted(chars))
    anagrams[tuple_d] += 1

tmp = [i-1 for i in anagrams.values()]
print(sum([n*(n+1)//2 for n in tmp]))

Solution (Python):
n=int(input())
x={}
cnt=0
for i in range(n):
  a=''.join(sorted(input()))
  if a in x:x[a]+=1
  else:x[a]=1

for a in x:
  cnt+=x[a]*(x[a]-1)//2
print(cnt)

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

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

Solution (Python):
import collections

n = int(input())
a = [0]*n
count = 0
for i in range(n):
  a[i] = list(input())
  a[i].sort()
  d = ""
  for j in a[i]:
    d += j
  a[i] = d
c = collections.Counter(a)
for i in c.values():
  count += (i-1) * i // 2
print(count)

Solution (Python):
n = int(input())
ls = [[_ for _ in input()] for _ in range(n)]

lssorted = [''] * n

for i in range(n):
	ls[i].sort()
	lssorted[i] = ''.join(ls[i])

lssorted.sort()


cnt = 1
ans = 0

for j in range(1, n):
	if lssorted[j] == lssorted[j - 1]:
		cnt += 1
		if j == n - 1:
			ans += cnt * (cnt - 1) / 2
	else:
		ans += cnt * (cnt - 1) / 2
		cnt = 1

print(int(ans))

Solution (Python):
import sys
from collections import defaultdict

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	n = int(input())
	dd=defaultdict(lambda :0)
	for i in range(n):
		s = input()
		a=list(s)
		a.sort()
		s=''.join(a)
		dd[s]+=1
		
	ret=0
	for k in dd.keys():
		r=dd[k]
		ret += r*(r-1)//2
	print(ret)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
import math

n = int(input())
s = [0]*n
ans = {}
ans_v = 0

for i in range(n):
    s[i] = "".join(sorted(list(input())))
    
s_set = set(s)

for i in s_set:
    ans[i] = 0
    
for i in s:
    ans[i] += 1

for j in ans.values():
    if j >= 2:
        ans_v += math.factorial(j) /( (math.factorial(j - 2)* math.factorial(2)))
    
print(int(ans_v))

Solution (Python):
import collections

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

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

Solution (Python):
from collections import Counter

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

cnt = 0

for key in c:
    val = c[key]
    cnt += val*(val-1)//2

print(cnt)

Solution (Python):
N = int(input())
words = [[] for i in range(N)]
for i in range(N):
  s = input()
  l = []
  for t in s:
    l.append(t)
  l.sort()
  words[i] = l
words.sort()

materials = [1]
for i in range(1, N):
  if words[i] == words[i-1]:
    materials[-1] += 1
  else:
    materials.append(1)
print(sum([i * (i-1) // 2 for i in materials]))

Solution (Python):

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, s: "List[str]"):
    l = [''.join(sorted(list(i))) for i in s]
    from collections import Counter
    c = Counter(l)

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

    ans = 0
    for k, v in c.most_common():
        if v >= 2:
            ans += combination(v, 2)

    print(ans)

    return



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

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

def solve(n, S):
    counts = defaultdict(lambda: 0)
    for s in S:
        counts[''.join(sorted(list(s)))] +=  1
    total = 0
    for v in counts.values():
        if v > 1:
            total += int(v * (v - 1) / 2)
    return total

_n = int(input())
_S = [input() for _ in range(_n)]
print(solve(_n, _S))

Solution (Python):


import collections

def main():
    N = int(input())
    INF = 10**9+7
    h = []
    prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101]
    for _ in range(N):
        s = str(input())
        num = 1
        for i in range(10):
            num *= prime[ord(s[i])-97]
        h.append(num)
        
    c = collections.Counter(h)
    T = list(c.values())
    ans = 0
    for use in T:
        ans += (use*(use-1))//2
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
hantei_list = [1 for _ in range(n)]
moji_list = [str(input()) for _ in range(n)]
prime_number = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
for i in range(n):
  for j in range(10):
    hantei_list[i] *= prime_number[ord(moji_list[i][j])- ord('a')]


appeared = dict()

counter = 0
for i in hantei_list:
  if not i in appeared:
    appeared[i] = 1
  else:
    counter += appeared[i]
    appeared[i] += 1
print(counter)

Solution (Python):
N = int(input())
S = dict()

ans = 0
for _ in range(N):
  s = list(str(input()))
  string = "".join(sorted(s))
  if S.get(string) is None:
  	S[string] = 1
  else:
    ans += S[string]
    S[string] += 1
     
print(ans)

Solution (Python):
import collections

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

c = collections.Counter(s)

ans = 0
for si in set(s):
    n = c[si]
    ans += n * (n - 1) // 2
print(ans)

Solution (Python):
n = int(input())
s = []
for _ in range(n):
    s.append(input())


cnt = 0
tmpdic = {}
for i in s:
    tmp = "".join(sorted(i))
    if tmp in tmpdic.keys():
        cnt += tmpdic[tmp]
        tmpdic[tmp] += 1
    else:
        tmpdic[tmp] = 1


print(cnt)

Solution (Python):
N = int(input())

S = []
for _ in range(N):
    tmp=sorted(input())
    S.append(tmp)

for i in range(len(S)):
    S[i]=''.join(S[i])

import collections
from math import factorial

res=0
dic = {}
for tmp in S:
    if tmp in dic.keys():
        res+=dic[tmp]
        dic[tmp]+=1
    else:
        dic[tmp]=1

print(int(res))

Solution (Python):
n = int(input())
str_dict = {}
for _ in range(n):
	s = "".join(sorted(input()))
	if s in str_dict:
		str_dict[s] += 1
	else:
		str_dict[s] = 1
ans = 0
for v in str_dict.values():
	for i in range(1, v):
		ans += i
print(ans)

Solution (Python):
from collections import defaultdict
import math

N = int(input())
li = [input() for i in range(N)]
d = defaultdict(int)

count = 0

for i in li:
  s = sorted(list(i))
  t = "".join(s)

  if d[t] != 0:
    count += d[t]
  d[t] += 1
    
print(count)

Solution (Python):
from collections import defaultdict
import math

N = int(input())
li = [input() for i in range(N)]
d = defaultdict(int)

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

count = 0

for i in li:
  s = sorted(list(i))
  t = "".join(s)

  d[t] += 1

for i in d:
  if d[i] > 1:
    count += combinations_count(d[i], 2)
    
print(count)

Solution (Python):

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


a = []
x = 1
for i in range(1,n): 
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0
for j in a: 
    y += j*(j-1)/2

print(int(y))

Solution (Python):

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


a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0
for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):

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


a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0
for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):

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


a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0
for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):

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


a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0
for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):
n = int(input())
s = []
for _ in range(n):
    
    
    
    s += [''.join(sorted(input()))]
    
s = sorted(s)



a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)


y = 0

for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):
n = int(input())
s = []
for _ in range(n):
    
    
    
    s += [''.join(sorted(input()))]
    
s = sorted(s)
s.append('X')


a = []
x = 1
for i in range(1,n+1):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1

y = 0

for j in a:
    y += j*(j-1)/2

print(int(y))

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

MOD = int(1e09) + 7

def main():
    N = cin.int()
    S = cin.stringList(N)
    D = _collections.defaultdict(int)
    for s in S:
        D["".join(sorted(s))] +=1
    ans = 0
    for v in D.values():
        ans += v * (v - 1) // 2
    print(ans)
    return

if __name__ == "__main__":
    main()

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

def main():
    n = int(input())
    str_list = [input() for _ in range(n)]

    count = 0

    for i,s in enumerate(str_list):
        str_list[i] = sorted(s)

    str_list = sorted(str_list)

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

    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = []
d = {}
for i in range(n):
	x = input()
	l = list(x)
	list.sort(l)
	ll = "".join(l)
	if ll in d:
		d[ll] += 1
	else:
		d[ll] = 1

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

Solution (Python):
N = int(input())
l = []

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

l.sort()
count = ans = 0

for i in range(N-1):
    if (l[i] == l[i+1]):
        count += 1
        ans += count
    else:
        count = 0

print(ans)

Solution (Python):
from collections import defaultdict

anagrams = defaultdict(int)
for _ in range(int(input())):
    anagrams[str(sorted(input()))] += 1

total = 0
for k, v in anagrams.items():
    total += v * (v - 1) / 2

print(int(total))

Solution (Python):
import math, collections

def combinations_count(n):
  d = 2
  if n < d:
    return 0
  return math.factorial(n) // (math.factorial(n - d) * math.factorial(d))
      

rows = int(input()) 
str_list = [input() for i in range(rows)]
sorted_str_list = map(lambda s: ''.join(sorted(s)), str_list)
  
counter = collections.Counter(sorted_str_list)
ans = sum(map(lambda v: combinations_count(v), counter.values()))
print(ans)