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 examA():
    N = I()
    L = LI()
    L.sort()
    if sum(L)<=L[-1]*2:
        print("No")
    else:
        print("Yes")
    return

def examB():
    N, M = LI()
    A = [LI()for _ in range(N)]
    A.sort(key=lambda x:x[0])
    ans = 0
    for a,b in A:
        if M<=b:
            ans += a*M
            break
        ans += a*b
        M -= b
    print(ans)
    return

def examC():
    N = I()
    S = [SI()for _ in range(N)]
    D = defaultdict(int)
    for s in S:
        cur = []
        for i,j in Counter(s).items():
            cur.append((i,j))
        #print(cur)
        cur.sort()
        cur = tuple(cur)
        D[cur] += 1
    ans = 0
    for d in D.values():
        ans += (d-1)*d//2
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

def ABC137C(i):
    if i!=1:
        return math.factorial(i)//math.factorial(i-2)//2
    else:
        return 0

n,*a=open(0).read().split()
a=[''.join(sorted(i)) for i in a]
d=dict()
p=0
for i in a:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.values():
    p+=ABC137C(i)
print(p)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=[]
    for i in range(n):
        si=list(input())
        si.sort()
        s.append(si)
    s.sort()
    s.append("X")
    cnt=1
    ans=0
    for i in range(n):
        if s[i]==s[i+1]:
            cnt+=1
        else:
            ans+=cnt*(cnt-1)//2
            cnt=1
    print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import collections

N=int(input())
ans=0
s=[input() for x in range(N)]

d={}

for i in range(N):

  temp=str(sorted(s[i]))

  if temp in d.keys():
    ans+=d[temp]
    d[temp]+=1
  else:
    d[temp]=1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = 0
n = int(input())
l =[k for j,k in Counter(["".join(sorted(input())) for i in range(n)]).items() if k>=2]
for i in l:
  s += (i*(i-1))//2
print(s)

Submission status: Accepted

Solution code:
# solution

data=int(input())
array=[''.join(sorted(input())) for i in range(data)]
sets={}
count=0
for i in range(data):
    if array[i] not in sets:
        sets[array[i]]=1
    else:
        sets[array[i]]+=1
        count+=sets[array[i]]-1
print(count)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    dic = {}
    for i in range(n):
        # 文字列をソートすると、list化しちゃう~~~
        # から"".join(list)で結合させてあげるのだよ
        s = "".join(sorted(input()))
        if s in dic:
            dic[s] += 1
        else:
            dic[s] = 1
    ans = 0
    for k,v in dic.items():
        ans += (v * (v-1))//2
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = 0
    di = {}
    for n in range(N):
        s = ''.join(sorted(iss()))
        if s in di:
            di[s] += 1
            ret += di[s]
        else:
            di[s] = 0
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for _ in range(n):
  s0=list(input())
  s0.sort()
  s.append(''.join(s0))
s.sort()
ans=0
p=s[0]
pc=1
for i in range(1,n):
  if p==s[i]:
    pc+=1
  else:
    ans+=pc*(pc-1)//2
    pc=1
    p=s[i]
ans+=pc*(pc-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = '-1'


def is_anag(a, b):
    return Counter(a) == Counter(b)


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

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


# for i, s in enumerate(S, start=1):
#     if s == NIL:
#         continue
#     counter = 1
#     for j in range(i, len(S)):
#         if S[j] == NIL:
#             continue
#         if is_anag(s, S[j]):
#             counter += 1
#             S[j] = NIL
#     if counter != 1:
#         ans.append(counter * (counter - 1) // 2)

# res = []
#
# for a in ans:
#     res.append(a * (a - 1) // 2)
# print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

import sys
sys.setrecursionlimit(10**6)
table = dict()
def choose(n, r, mod=None): # not mod, or mod ≠ prime
  if r > n or r < 0: return 0
  if r == 0: return 1
  if (n, r) in table: return table[(n, r)] 
  table[(n, r)] = (choose(n - 1, r) + choose(n - 1, r - 1))
  # table[(n, r)] = (choose(n - 1, r, mod) + choose(n - 1, r - 1, mod)) % mod
  return table[(n,r)]

n = int(sys.stdin.readline().rstrip())
s = [''.join(sorted(s)) for s in sys.stdin.read().split()]

def main():
    tot = 0
    for c in Counter(s).values():
        tot += choose(c, 2)
    print(tot)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
import math
n=int(input())
s=[]
for i in range(n):
    sl=list(input())
    sl.sort()
    s.append("".join(sl))

l=list(collections.Counter(s).values())
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

c=0
for i in range(len(l)):
    k=int(l[i])
    if k!=1:c+=combinations_count(k,2)
print(c)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = '-1'


def is_anag(a, b):
    return Counter(a) == Counter(b)


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

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

print(ans)

# for i, s in enumerate(S, start=1):
#     if s == NIL:
#         continue
#     counter = 1
#     for j in range(i, len(S)):
#         if S[j] == NIL:
#             continue
#         if is_anag(s, S[j]):
#             counter += 1
#             S[j] = NIL
#     if counter != 1:
#         ans.append(counter * (counter - 1) // 2)

# res = []
#
# for a in ans:
#     res.append(a * (a - 1) // 2)
# print(sum(ans))

Submission status: Accepted

Solution code:
n=int(input())
#n,p=map(int,input().split())
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
#素因数分解した結果を2次元配列にして返す

dic={}
ans=0
for i in range(n):
    s=list(input())
    s.sort()
    st="".join(s)
    v=dic.get(st,0)
    ans+=v
    dic[st]=v+1
print(ans)

Submission status: Accepted

Solution code:
import collections
from math import factorial
A = int(input())
listA = []
total = 0
listB = {}
for i in range(A):
    B = list(input())
    B.sort()
    listA.append("".join(B))
for i in listA:
    if i in listB:
        listB[i] += 1
        total += listB[i]-1
    else:
        listB[i] = 1
print(total)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    dic = {}
    for i in range(n):
        s = "".join(sorted(input()))
        if s in dic:
            dic[s] += 1
        else:
            dic[s] = 1
    ans = 0
    for k,v in dic.items():
        ans += v * (v-1) //2 # vC2
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math
import sys
input = sys.stdin.readline
N=int(input())
d={}
for i in [0]*N:
 s = ''.join(sorted(input().strip()))
 if s in d:
  d[s]+=1
 else:
  d[s]=1
e=0
for v in d.values():
 if v>=2:
  e+=int(v*(v-1)/2)
print(e)

Submission status: Accepted

Solution code:
N, *S = open(0).read().split()
S = [str(sorted(s)) for s in S]
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:
n = int(input())
import itertools
A = []
t = 0
for i in range(n):
    A.append("".join(sorted(list(input()))))


import collections

C = collections.Counter(A)


for i in C.values():
    t += i * (i - 1) // 2
print(t)

Submission status: Accepted

Solution code:
import math
import collections


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


n = int(input())
s = [''.join(sorted(list(input()))) for _ in range(n)]
c = collections.Counter(s)
print(sum(comb(a, 2) for a in c.values() if a > 1))

Submission status: Accepted

Solution code:
n = int(input())
s = ['']*n
for i in range(n):
  s[i] = input()
dict = {}
num = 0
for i in range(n):
  k = ''.join(sorted(s[i]))
  if k not in dict.keys():
    dict[k] = 1
  else:
    num += dict[k]
    dict[k] += 1  
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
from collections import Counter
for i in range(n):
  s = input()
  s = sorted(s)
  s = ''.join(s)
  if s not in dic: dic[s] = 1
  else: dic[s] += 1
a = dic.values()
a = list(a)
ans = 0
for i in range(len(a)):
  ans += a[i]*(a[i]-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

from collections import Counter

def c2s(c):
    s = ''
    for key, val in c:
        # print(key, val)
        # s = f'{s}{key}{val}'
        s = '{}{}{}'.format(s, key, val)
    # print(s)
    return s

def choose2(n):
    if n == 1:
        return 0
    return n * (n - 1) // 2

def solve():
    n = II()
    S = ["".join(sorted(input())) for _ in range(n)]
    # print(S)

    jisho = {}
    for s in S:
        # c = sorted(Counter(s).items(), key=lambda x: x[0])
        # print(c)
        # key = c2s(c)
        jisho[s] = jisho.setdefault(s, 0) + 1
    # print(jisho)
    ans = 0
    for val in jisho.values():
        ans = ans + choose2(val)

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

from collections import Counter

def c2s(c):
    s = ''
    for key, val in c:
        # print(key, val)
        # s = f'{s}{key}{val}'
        s = '{}{}{}'.format(s, key, val)
    # print(s)
    return s

def choose2(n):
    if n == 1:
        return 0
    return n * (n - 1) // 2

def solve():
    n = II()
    S = [list(input()) for _ in range(n)]

    jisho = {}
    for s in S:
        c = sorted(Counter(s).items(), key=lambda x: x[0])
        # print(c)
        key = c2s(c)
        jisho[key] = jisho.setdefault(key, 0) + 1
    # print(jisho)
    ans = 0
    for val in jisho.values():
        ans = ans + choose2(val)

    print(ans)

if __name__ == '__main__':
    solve()

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, acos, atan, asin, log, log10
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 debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(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()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
s = [''.join(sorted(list(STR()))) for _ in range(n)]
t = set(s)
u = Counter(s)
ans = 0
for i in t:
    ans += u[i] * (u[i] - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

check = {}
ans = 0

for _ in range(n):
    word = sorted(input())
    new_word = ''.join(list(word))
    check.setdefault(new_word, 0)
    check[new_word] += 1

for k in check:
    if check[k] >= 2:
        ans += int(math.factorial(check[k])/(math.factorial(check[k]-2)*2))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
li = [0]*N
for i in range(N):
    s = list(input())
    s.sort()
    li[i] = ''.join(s)
from collections import Counter
c = Counter(li)
ans = 0
key = list(c.keys())
for i in key:
    ans += c[i]*(c[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
from time import time
from math import factorial

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

def main():
    N = int(input())
    s = []
    answer = 0
    for i in range(N):
        s.append(input())
        s[i] = ''.join(sorted(s[i]))

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

    for i in d.values():
        if i >= 2:
            answer += combinations_count(i, 2)


    print(answer)

if __name__ == '__main__':
    start = time()
    main()
    # print(time() - start)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
ans = 0
for i in range(N):
    S = list(input())
    S.sort()
    s = "".join(S)
    ans += d[s]
    d[s]+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = sorted(sorted(input()) for _ in range(N))
ans = 0
cnt = 1
for i in range(1, N):
    if s[i-1] == s[i]:
        cnt += 1
    else:
        if cnt > 1:
            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 defaultdict
N = int(input())

SS = []
for i in range(N):
  s = list(input())
  s.sort()
  SS.append(s)
  
SS.sort()


ans = 0
cnt = defaultdict(int)
for i in range(N):
  s = SS[i]
  s = ''.join(s)
  cnt[s] += 1
  
for _,v in cnt.items():
  ans += v * (v - 1) // 2
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

SS = []
for i in range(N):
  s = list(input())
  s.sort()
  SS.append(s)
  
SS.sort()

ans = 0
prev = None
cnt = 0
for i in range(N):
  s = SS[i]
  if s == prev:
    if not cnt:
      cnt = 2
    else:
      cnt += 1
  else:
    ans += cnt * (cnt - 1) // 2
    cnt = 0
  prev = s
  
ans += cnt * (cnt - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
d = {}
c = 0
for i in open(0):
    i = str(sorted(i))
    t = d.get(i, 0)
    c += t
    d[i] = t + 1
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict
d=defaultdict(int)
n,*s=open(0).read().split()
for i in s:d["".join(sorted(list(i)))]+=1
print(sum(v*(v-1)//2 for v in d.values()))

Submission status: Accepted

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

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

s.sort()

memo = 1
ans = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        memo += 1
    else:
        ans += memo*(memo-1)//2
        memo = 1
    
ans += memo*(memo-1)//2
print(ans)

Submission status: Accepted

Solution code:
def f(n):
  return (n**2-n)//2
ans=0
N=int(input())
d=dict()
for i in range(N):
  s="".join(sorted(input()))
  if s not in d:
    d[s]=0
  d[s]+=1
for i in d:
  ans+=f(d[i])
print(ans)

Submission status: Accepted

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

	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):
	tmp = list(input())
	tmp.sort()
	s.append(tmp)

s.sort()

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

if count != 1:
	ans += cmb(count,2)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
li = []
for i in range(n):
    s = list(input())
    s.sort()
    li.append("".join(s))

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

Submission status: Accepted

Solution code:
import collections
import sys

N = int(input())
s = [sorted(collections.Counter(sys.stdin.readline().rstrip()).items()) for _ in range(N)]

counted = {}
for s_count in s:
    if str(s_count) not in counted:
        counted[str(s_count)] = 1
    else:
        counted[str(s_count)] += 1


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

Submission status: Accepted

Solution code:
import collections


def calculateC(n):
    n_C_i = n * (n-1)
    return n_C_i // 2

n = int(input())
ans = []
for _ in range(n):
    s = list(input())
    s.sort()
    a = ''
    for i in range(len(s)):
        a += s[i]
    ans.append(a)
cnt = collections.Counter(ans).most_common()
aa = 0
for i in range(len(cnt)):
    if cnt[i][1] == 1:
        break
    aa += calculateC(cnt[i][1])
print(aa)

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):
  n = c[si]
  ans += n*(n-1)//2
print(ans)

Submission status: Accepted

Solution code:
#C
import collections
n = int(input())
s = []
for _ in range(n):
    s.append(input())
 
for i in range(n):
    s[i] = "".join(sorted(s[i]))
s.sort()

c = collections.Counter(s)

result = [x for x in c.values() if x > 1]

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
d = defaultdict(int)
ans = 0
for i in range(N):
    s = tuple(sorted(input()))
    ans += d[s]
    d[s] += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
d = defaultdict(int)

for i in range(N):
    s = tuple(sorted(input()))
    d[s] += 1
ans = 0
for key, value in d.items():
    while value != 0:
        ans += value - 1
        value -= 1
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
from operator import mul
def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    elif r<0: return 0
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

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


ans = 0
d = {}
for i in s:
    d[i] = d.get(i, 0) + 1

for i in d.values():
    ans += cmb(i, 2)

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
#import math
import collections
#from collections import deque
#from bisect import bisect_left
N = int(input())
#S = input()[:-1]
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = list(map(int,input().split()))
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
l = {}
for i in range(N):
    S = list(input())
    S.remove("\n")
    S.sort()
    S = tuple(S)
    if S in l.keys():
      l[S] += 1
    else:
      l[S] = 1
ans = 0
for j in l.values():
  ans += j*(j-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
3
acornistnt
peanutbomb
constraint
'''

def solve(l):
	n = len(l)
	L = []
	for s in l:
		a = [0]*26
		for i in s:
			a[ord(i)-97] += 1
		L.append(a)
	d = defaultdict(int)
	for i in L:
		d[str(i)] += 1
	# print(d)
	ans = 0
	for i in d:
		ans += d[i]*(d[i]-1)//2
	return ans
	

N = int(stdin.readline())
l = []
for nn in range(N):
	l.append(input())
ans = solve(l)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ans=0
dic={}
for i in range(N):
	a=sorted(str(input()))
	a=''.join(a)
	if a in dic.keys():
		ans+=dic[a]
		dic[a]+=1
	else:
		dic[a]=1
print(ans)

Submission status: Accepted

Solution code:
import math
def String_Sort(str):
    tmp = sorted(str)
    ans = ""
    for i in tmp:
        ans += i
    return ans

def sol(n):
    return (n*(n-1))//2

# main
li = []
n = int(input())
ans = 0
for i in range(n):
    li.append(String_Sort(input()))
li.sort()
# temporary
str = li[0]
num = 0
for i in li:
    if (i == str):
        num += 1
        continue
    else:
        if num>1:
            ans += sol(num)
        num = 1
        str = i
if (num > 1):
    ans+=sol(num)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
s_list = list()
for _ in range(N):
    s_list.append(''.join(sorted(input())))
    
count = 0
for k, v in Counter(s_list).items():
    count += v * (v-1) // 2

print(count)

Submission status: Accepted

Solution code:
N=int(input())
S=[]
for _ in range(N):
    L=sorted(input())
    S.append(''.join(L))
S.sort()
#print(S)

def comb(x):
    return x*(x-1)//2

count=1
ans=0
for i in range(N-1):
    if i<N-2:
        if S[i]==S[i+1]:
            count+=1
        else:
            if count>=2:
                ans+=comb(count)
                count=1
    else:
        if S[i]==S[i+1]:
            count+=1
            if count>=2:
                ans+=comb(count)
        else:
            if count>=2:
                ans+=comb(count)

print(ans)

Submission status: Accepted

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

L = sorted(L)

s = L[0]
c = 0
n = 1
for i in range(1,N):
  if s == L[i]:
    n +=1
  else:
    if n >= 2:
      c += n*(n-1)//2
    s = L[i]
    n = 1
    
if n >=2:
  c +=n*(n-1)//2
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import collections
n = int(input())
l = []
ans = 0
for i in range(n):
  a = list(input())
  a.sort()
  a = str(a)
  l.append(a)
c = collections.Counter(l)
for j in c.values():
  if j >= 2:
    ans += j*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = {}

for i in range(N):
    s = input()
    s = ''.join(sorted(list(s)))
    S.setdefault(s, 0)
    S[s] += 1

print(sum((x*(x-1))//2 for x in S.values() if x > 1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
strs=[]
for i in range(n):
    line=sorted(input().rstrip())
    STR=""
    for j in line:
        STR=STR+j
    strs.append(STR)
#print(strs)
x=collections.Counter(strs)
#print(x)
sm=0
for j in x:
    sm+=x[j]*(x[j]-1)//2
print(sm)

Submission status: Accepted

Solution code:
n = int(input())
a = dict()
ans = 0
for i in range(n):
    string = input()
    b = ''.join(sorted(string))
    if b in a:
        a[b] += 1

    else:
        a[b] = 1
for i in a.values():
    if i > 1:
        ans += int(i*(i-1)/2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for s in S:
    s = str(sorted(s))
    ans += d[s]
    d[s] += 1

print(ans)

Submission status: Accepted

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

"""
n = int(input())
dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    t = "".join(s)
    if t in dic.keys():
        dic[t] += dic[t] + 1
    elif t not in dic.keys():
        dic[t] = 0
print(sum(dic.values()))
"""

Submission status: Accepted

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

S.sort()
ans = 0
count = 0
for i in range(1,len(S)):
    if S[i] == S[i-1]:
        ans += count + 1
        count += 1
    else:
        count = 0
print(ans)

Submission status: Accepted

Solution code:
import collections


n = int(input())

s = []

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

cnt = collections.Counter(s)
ans = 0
for i in cnt:
    ans += cnt[i] * (cnt[i] - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
import collections

ORD_A = ord("a")


def count_chars(s):
    counter = [0 for _ in range(26)]
    for c in s:
        counter[ord(c) - ORD_A] += 1

    return counter


def main():
    n = int(input())
    anagram_counter = collections.Counter(tuple(count_chars(input())) for _ in range(n))

    combinations_times_2 = sum(v * (v - 1) for v in anagram_counter.values())
    print(combinations_times_2 // 2)


if __name__ == "__main__":
    main()