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: Time Limit Exceeded

Solution code:
N = int(input())
S = []
ans = 0
ANS = {}
for i in range(N):
  si = input()
  new_str_list = sorted(si)
  s = "".join(new_str_list)
  if s in S:
    if s not in ANS:
      ANS[s] = 1
    else:
      ANS[s] += 1
    ans += ANS[s]
  else:
    S.append(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
s = [tuple(sorted(list(input()))) for _ in  range(N)]

d = defaultdict(int)
ans = 0
for i in range(N):
  if s[i] in d.keys():
    ans += d[s[i]]
    d[s[i]] += 1
  else:
    d[s[i]] += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
li = []
for _ in range(n):
  s = input()
  temp = []
  dd = defaultdict(lambda:0)
  for i in s:
    dd[i] += 1
  for key in dd:
    temp.append((key, dd[key]))
  temp.sort()
  li.append(temp)
li.sort()
ans = 0
k = 0
for i in range(n):
  if i == n-1:
    ans += (k*(k+1))//2
  else:
    if li[i] == li[i+1]:
      k += 1
    else:
      ans += (k*(k+1))//2
      k = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    words = [ "".join(sorted(list(input()))) for i in range(N)]
    w = set(words)

    pair = 0
    for word in w:
        c = words.count(word)
        if c >= 2:
            pair += c*(c-1)//2
    
    print(pair)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
ans = 0
for i in set(s):
    n = s.count(i)
    ans += n * (n - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
ans = 0
s1 = set(s)
for i in s1:
    n = s.count(i) - 1
    ans += n * (n+1) / 2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
ans = 0
s1 = set(s)
for i in s1:
    n = s.count(i) - 1
    ans += n * (n+1) / 2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
ans = 0
s1 = set(s)
for i in s1:
    ans += sum([i for i in range(0, s.count(i))])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
ans = 0
s1 = set(s)
for i in s1:
    ans += len(list(combinations([i for i in range(s.count(i))], 2)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
P=[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]
n=int(input())
A=[1]*n

for i in range(n):
	s=input()
	for j in range(10):
		A[i] *= P[ord(s[j])-97]

res=0
for i in range(n):
	for j in range(i+1,n):
		if A[i]==A[j]:
			res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
s=[]
res=0
for i in range(n):
	t=input()
	s.append(Counter(t))
for i in range(n):
	for j in range(i+1,n):
		if s[i]==s[j]:
			res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
#import itertools
import math
n=int(input())
s = [str(sorted(list(input()))) for i in range(n)]
l=[]
for si in set(s):
    l.append(s.count(si))
ans=0
for i in l:
    if i>=2:
        #x=len(list(itertools.combinations(range(i),2)))
        x = (math.factorial(i) // (math.factorial(i - 2) * math.factorial(2)))
        ans+=x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n= int(input())
s = [str(sorted(list(input()))) for i in range(n)]
l=[]
for si in set(s):
  x=0
  for sj in s:
    if si==sj:
      x+=1
  l.append(x)
ans=0
for i in l:
  x=len(list(itertools.combinations(range(i),2)))
  ans+=x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
n = int(input())
s = [0]*n
T = [0]*n
ans = 0
for i in range(n):
	s[i] = input()
	T[i] = sorted(list(s[i]))
for i in range(n-1):
	for j in range(i+1,n):
		if T[i] == T[j]:
			ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
s = [0]*n
c = {}
ans = 0
for i in range(n):
	s[i] = input()
	c[i] = collections.Counter(list(s[i]))
for i in range(n-1):
	for j in range(i+1,n):
		if c[i] == c[j]:
			ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
s_count = []
for i in range(n):
    s = input()
    cnt = collections.Counter(s)
    cnt_list = list(cnt.items())
    c_sorted = sorted(cnt_list)
    s_count.append(c_sorted)
ans = 0
for i in range(n-1):
    for j in range(i+1, n):
        if s_count[i] == s_count[j]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n=int(input())
S=[]
ans=0
for i in range(n):
    s=input()
    S.append(s)
    for j in range(i):
        if Counter(S[j])==Counter(s):
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
ans = 0

for i in range(n):
    a = sorted(input())
    l.append("".join(a))

sl = set(l)


for i in sl:
    com = l.count(i)
    ans += com*(com - 1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [sorted(list(input()))for i in range(N)]
count = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [sorted(list(input()))for i in range(N)]
count = 0
for i in range(N):
    for j in range(N):
        if S[i]==S[j]:
            count+=1
print((count-N)//2)

Submission status: Time Limit Exceeded

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


for i in range(n):
    for j in range(i,):
        if s[i] == s[j]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
s = [None for _ in range(N)]
count = [None for _ in range(N)]
ans = 0
for i in range(N):
  s[i] = input()
for i in range(N):
  count[i] = collections.Counter(s[i])
for i in range(N-1):
  for j in range(i+1,N):
    if count[j] == count[i]:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [str(input()) for _ in range(n)]
ans = 0
for i in range(n):
    s[i] = sorted(s[i])
 
 
for i in range(n):
    for j in range(i,):
        if s[i] == s[j]:
            ans += 1
 
print(ans)

Submission status: Time Limit Exceeded

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


for i in range(n):
  s=sorted(input())
  s="".join(s)
  if s in list(dic.keys()):
    dic[s]+=1
  else:
    dic[s]=1
ans=0
for i in dic.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [str(input()) for _ in range(n)]
ans = 0
for i in range(n):
    s[i] = sorted(s[i])


for i in range(n):
    for j in range(i,):
        if s[i] == s[j]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
T = []
for i in range(N):
    t = list(S())
    T.append(Counter(t))

cnt = 0
for j in range(0, N):
  for k in range(j+1, N):
    if T[j] == T[k]:
      cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = [[0]*10 for _ in range(n)]
ans = 0
for i in range(n):
    str_s = input()
    for j in range(10):
        s[i][j] = str_s[j]

for i in range(n):
    s[i].sort()
    s[i] = tuple(s[i])
set_s = set(s)

if n-len(set_s) == 0:
    print(0)
elif n-len(set_s) == 1:
    print(n-len(set_s))
else:
    for item in set_s:
        count = 0
        for j in range(n):
            if item == s[j]:
                count += 1
        ans += count*(count-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = [[0]*10 for _ in range(n)]
ans = 0
for i in range(n):
    str_s = input()
    for j in range(10):
        s[i][j] = str_s[j]

for i in range(n):
    s[i].sort()
    s[i] = tuple(s[i])
set_s = set(s)


for item in set_s:
    count = 0
    for j in range(n):
        if item == s[j]:
            count += 1
    ans += count*(count-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[0]*n
ans=0
for i in range(n):
  t=list(input())
  t.sort()
  ans+=s.count(t)
  s[i]=t
  
  
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
  tmp = sorted(input())
  if tmp in a:
    b[a.index(tmp)] += 1
  else:
    a.append(tmp)
    b.append(1)
res = [i*(i-1)//2 for i in b]
print(sum(res))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
cnt=0
l=[(sorted(list(input())))for i in range(n)]

for j in range(n-1):
    for k in range(j+1,n):
        if l[j]==l[k]:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
cnt=0
l=[]
for i in range(n):
    l.append(sorted(list(input())))

for j in range(n-1):
    for k in range(j+1,n):
        if l[j]==l[k]:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
cnt=0
l=[]
for i in range(n):
            l_i=list(input())
            l_i.sort()
            l.append(l_i)

for j in range(n-1):
            for k in range(j+1,n):
                        if l[j]==l[k]:
                                    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = []
cnt = []
for i in range(N):
    S.append(input())
    S[i] = list(S[i])
    cnt.append(Counter(S[i]))

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if cnt[i] == cnt[j]:
            ans += 1

print(ans)
#print("cnt = {}".format(cnt))

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [sorted(list(input())) for _ in range(N)]
i = 0
A = []
for a in S:
    A.append("".join(a))

count = 0
for a in A[:N]:
    count += A[i+1:].count(a)
    i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list1 = [str(input()) for  _ in range(N)]
list2 =  ["".join(sorted(list1[i])) for i in range(N)]
ans = 0
for i in range(N):
  for j in range(i+1,N):
    if list2[i] == list2[j]:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = []
for i in range(N):
    s = input()
    L.append(sorted(s))
L = sorted(L)
ans = 0
a = 0
for i in range(a,N):
    for j in range(i+1,N):
        if L[i] == L[j]:
            ans += 1
        else:
            a = j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from collections import Counter
N = int(input())
s = [''.join(sorted(input())) for _ in range(N)]

# sN = [input() for _ in range(N)]
# # counts = [Counter(s) for s in sN]
# # pool = []
# # ans = 0
# # for count in counts:
# #     if count in pool:
# #         ans += 1
# #     else:
# #         pool.append(count)
# # print(pool)
# # # print(get_unique_list(counts))
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        if sorted(s[i]) == sorted(s[j]):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from collections import Counter
N = int(input())
sN = [input() for _ in range(N)]
# counts = [Counter(s) for s in sN]
# pool = []
# ans = 0
# for count in counts:
#     if count in pool:
#         ans += 1
#     else:
#         pool.append(count)
# print(pool)
# # print(get_unique_list(counts))
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        if sorted(sN[i]) == sorted(sN[j]):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
sN = [input() for _ in range(N)]
# counts = [Counter(s) for s in sN]
# pool = []
# ans = 0
# for count in counts:
#     if count in pool:
#         ans += 1
#     else:
#         pool.append(count)
# print(pool)
# # print(get_unique_list(counts))
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        if set(sN[i]) == set(sN[j]):
            if Counter(sN[i]) == Counter(sN[j]):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
sN = [input() for _ in range(N)]
counts = [Counter(s) for s in sN]
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        if counts[i] == counts[j]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=['']*n
for i in range(n):
  s[i]=sorted(list(input()))
ans=0
for i in range(n):
  for j in range(i,n):
    if s[i]==s[j] and not i==j:
      ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as C
n=int(input())
s=['']*n
for i in range(n):
  s[i]=C(sorted(list(input())))
ans=0
for i in range(n):
  for j in range(i,n):
    if s[i]==s[j] and not i==j:
      ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

res = [""]
count = 0
for i in range(n):
  s = "".join(sorted(input()))
  if s in res:
    c = collections.Counter(res)
    count += c[s]
    res.append(s)
  else:
    res.append(s)
    
print(count)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(n):
  for j in range(i,n):
    if i != j:
      if s[i] == s[j]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def countPairs(str_arr):
	count = 0
	for i in range(N):
		for j in range(i+1,N):
			if str_arr[i] == str_arr[j]:
				count += 1
	return count


N = int(input())
str_arr = []

for i in range(N):
	str = input()
	str_arr.append(sorted(str))

print(countPairs(str_arr))

Submission status: Time Limit Exceeded

Solution code:
def isAnagram(str_l, str_r):
	if sorted(str_l)==sorted(str_r): 
		return True 
	else:
		return False


def countPairs(str_arr):
	count = 0
	for i in range(N):
		for j in range(i+1,N):
			if isAnagram(str_arr[i], str_arr[j]):
				count += 1
	return count


N = int(input())
str_arr = []

for i in range(N):
	str = input()
	str_arr.append(str)

print(countPairs(str_arr))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())

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

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

ans = 0
for comb in itertools.combinations(encoded_strings, 2):
    if comb[0] == comb[1]:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from collections import defaultdict

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

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
S = []
for _ in range(n):
    S.append(Counter(list(input())))

ans = 0
for i in range(n):
    for j in range(n):
        if i < j:
            c_i = S[i]
            c_j = S[j]
            f = 0
            for k_i, v_i in c_i.items():
                if c_j[k_i] != v_i:
                    f = 1
                    break
            if f == 0:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
anslist = []
for i in range(n):
    s = input()
    c = Counter(s)
    anslist.append(c)
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        if anslist[i] == anslist[j]:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = [Counter(input()) for _ in range(n)]

c = 0
for i in range(n):
    for j in range(n):
        if s[i] == s[j] and i != j:
            c += 1
print(c//2)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    dict = {}
    s = ["".join(sorted(input())) for _ in range(n)]
    for i in range(n):
        if s[i] in dict.keys():
            dict[s[i]] += 1
        else:
            dict[s[i]] =1
    alist = list (dict.values())
    ans = 0
    for i in range(len(alist)):
        ct = alist[i]
        ans += (ct*(ct-1))//2
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    dict = {}
    s = ["".join(sorted(input())) for _ in range(n)]
    for i in range(n):

        if s[i] in dict.keys():
            dict[s[i]] += 1
        else:
            dict[s[i]] =1
    ans = 0
    for i in dict.keys():
        ct = dict[i]
        ans += (ct*(ct-1))//2
    print(ans)

resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections

def verify_anagrams(w1, w2):
    f = lambda y: "".join(sorted(map(lambda x: x.replace(' ', '').lower(), y)))
    return f(w1) == f(w2)

N = int(input())
S = []
for i in range(N):
    S.append(input())
    
cnt = 0
for i in range(N):
    for k in range(1,N-i):
        if verify_anagrams(S[i], S[i+k]):
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

        ct = dict[i]
        ans += (ct*(ct-1))//2
    print(ans)

resolve()

Submission status: Time Limit Exceeded

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


def main():
    n = int(input())
    l = []
    cnt = []
    for i in range(n):
        s = sorted(input())
        if not s in l:
            l.append(s)
            cnt.append(1)
        else:
            i = l.index(s)
            cnt[i] += 1
    ans = 0
    for i in cnt:
        ans += i*(i-1) // 2
    print(ans)


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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
import collections
chk = collections.deque([])
for i in range(n):
  s = input()
  s = sorted(s)
  chk.append(''.join(s))
#chk = list(chk)
c = collections.Counter(chk)
ans = 0
for i in range(len(c)):
  if c.most_common()[i][1] == 1:
    break
  else:
    ans += c.most_common()[i][1]*(c.most_common()[i][1]-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

s = list(set(S))
ans = 0
for i in range(len(s)):
  a = S.count(s[i])
  if a == 1: continue
  ans += a * (a-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
l = []
for i in range(N):
  s = input()
  ls = []
  for j in range(10):
    ls.append(s[j])
  c = collections.Counter(ls)
  l.append(c)
cnt = 0
for i in range(N):
  for j in range(i+1,N):
    if l[i] == l[j]:
      cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = []
for i in range(N):
  s = input()
  ls = []
  for j in range(10):
    ls.append(s[j])
  ls.sort()
  l.append(ls)
cnt = 0
for i in range(N):
  for j in range(i+1,N):
    if l[i] == l[j]:
      cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n,*a=open(0).read().split()
a=[''.join(sorted(i)) for i in a]
p=0
for i,j in itertools.combinations(range(int(n)),2):
    #print(i,j)
    if a[i]==a[j]:
        p+=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
import collections

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


for i in range(N):
  for j in range(N):
    if i<j:
      if collections.Counter(s[i])==collections.Counter(s[j]):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

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()
    S = [''.join(sorted(iss())) for _ in range(N)]
    ret = 0
    for s in S:
        ret += S.count(s) - 1
    print(ret // 2)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    table = [sorted(list(input())) for _ in range(N)]

    cnt = 0

    for i in range(N - 1):
        for j in range(i + 1, N):
            if table[i] == table[j]:
                cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(N - 1):
    for j in range(i + 1, N):
        if sorted(table[i]) == sorted(table[j]):
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(i+1, N):
        if d[i] == d[j]:
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

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 = [input().rstrip() for _ in range(N)]

dict = {}
for s in S:
    s_list = sorted(list(s))
    s_str = "".join(s_list)
    if s_str in dict.keys():
        dict[s_str] += 1
    else:
        dict[s_str] = 1

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

print(total)


# 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: Time Limit Exceeded

Solution code:
n=int(input())
t=[]
for i in range(n):
    t.append(input())

ans=0
import itertools
for j in range(n-1):
    for k in range(j+1,n):
        if tuple(t[j]) in itertools.permutations(t[k],len(t[k])):
             ans += 1
print(ans)

Submission status: Time Limit Exceeded

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 = [input().rstrip() for _ in range(N)]
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: Time Limit Exceeded

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 = [input().rstrip() for _ in range(N)]
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)

res = []

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

Submission status: Time Limit Exceeded

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

""" Input
N, A, B = map(int, input().split())
A = list(map(int, input().split()))
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""


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


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

counter = 0
for i, s in enumerate(S, start=1):
    for j in range(i, len(S)):
        if is_anag(s, S[j]):
            counter += 1


print(counter)






# def main():
# if __name__ == '__main__':
#     main()

Submission status: Time Limit Exceeded

Solution code:
N, *S = open(0).read().split()
S = [sorted(s) for s in S]

cnt = 0
for i, s in enumerate(S):
    for j, t in enumerate(S[i+1:]):
        if s == t:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = [sorted(input()[:-1]) for _ in [0]*N]

cnt = 0
for i, s in enumerate(S):
    for j, t in enumerate(S[i+1:]):
        if s == t:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = [input()[:-1] for _ in [0]*N]

cnt = 0
for i, s in enumerate(S):
    for j, t in enumerate(S[i+1:]):
        if sorted(s) == sorted(t):
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math


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


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

anagram = [0] * len(s)
for i, si in enumerate(s):
    anagram[i] = c.count(si)

print(sum(comb(a, 2) for a in anagram if a > 1))