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())
words = [input() for i in range(N)] 

ans = 0
for ix, word in  enumerate(words):
    for j, compe_word in enumerate(words[ix+1:]):
        compe_word_list = list(compe_word)
        for alphabet in word:
            if alphabet in compe_word_list:
                compe_word_list.pop(compe_word_list.index(alphabet))
        if len(compe_word_list) == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import itertools

def verify_anagrams(first_word, second_word):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))
 
    return get_counter(first_word) == get_counter(second_word)
lst =[]
N = int(input())
for _ in range(N): 
    lst.append(input())
c = 0
word_lst = list(itertools.combinations(lst, 2))
for w1, w2 in word_lst:
    flg = verify_anagrams(w1, w2)
    if flg == True:
            c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = [''] * N
for i in range(N):
    tmp = sorted(input())
    s[i] = ''.join(tmp)
s.sort()
cnt = 0
index = 1
for i in range(0,N - 1,index):
    index = 1
    for j in range(i + 1, N):
        if s[i] == s[j]:
            cnt += 1
            index += 1
        else:
            break

print(cnt)

Submission status: Time Limit Exceeded

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

nums = []
for w in ws:
    v = [0] * 52
    for c in w:
        if "Z" < c:
            v[ord(c) - ord("a")] += 1
        else:
            v[ord(c) - ord("A") + 26] += 1
    nums.append(v)

r = 0
for i in range(n):
    for j in range(n):
        if j <= i:
            continue
        if nums[i] == nums[j]:
            r += 1

print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
strs = [str(sorted(Counter(input()).items(),key=lambda x:x[0])) for i in range(N)][::-1]
checkedmap={}
ans=0
for i in range(len(strs)):
    ans += strs[i + 1:].count(strs[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def c(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

cnt = 0
s = []
tmp = []
n = int(input())
for i in range(n):
    string = input()
    string = ''.join(sorted(string))
    s.append(string)

for i, e in enumerate(s):
    """
    要素が何個あるか検査　AND　tmpにないやつ
    ・１：なにもしない
    ・2以上：個数調べてCNT更新。その要素をtmpに記録する
    """
    count = s.count(e)
    if count > 1 and not e in tmp:
        cnt += c(count, 2)
        tmp.append(e)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def encode(s):
  r = [0]*26
  for n in range(len(s)):
    r[ord(s[n]) - ord('a')] += 1
  return r

N = int(input())

S = []

for n in range(N):
  s = str(input())
  S = S + [encode(s)]

S.sort()

ct = 0
M=1
ps=""
for cs in S:
  if cs==ps:
    M+=1
  else:
    ct += int((M**2-M)/2)
    M=1
    ps=cs
ct += int((M**2-M)/2)

print(ct)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [''.join(sorted(list(input()))) for i in range(N)]
b = 0
for i in a:
 b = b + a.count(i) - 1
print(int(b / 2))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
 
N = int(input())
data = [''.join(sorted(input().rstrip())) for n in range(N)]
count = 0
while data:
    number = data.count(data[0])
    if number > 1:
        count += number - 1
    del data[0]
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
s=[str(collections.Counter(sorted(list(input())))) for _ in range(N)]

print(int(sum(list(map(lambda x: x*(x-1)/2*1, list(collections.Counter(s).values()))))))

Submission status: Time Limit Exceeded

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

s_int = []
for _s in s:
    v = list(map(lambda x: ord(x)-ord('a'), _s))
    v = sum([v[i]*26**i for i in range(len(v))])
    s_int.append(v)

cnt = [0 for _ in range(N)]
for i in range(N-1):
    cnt_i = 0
    for j in range(i+1, N):
        if (cnt[j] != 0):
            continue
        if (s_int[i] == s_int[j]):
            cnt_i += 1
            cnt[j] = cnt_i

print(sum(cnt))

Submission status: Time Limit Exceeded

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

counter     =   0

for i in range(N):
    S   =   sorted(input())
    for j in range(len(A)):
        if(A[j] == S):
            counter += 1
            continue
    A.append(S)
print(counter)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
all = []
count = []
nsame = 0
for i in range(N):
  tmp = collections.Counter(list(input()))
  if i >= 1:
    for j in range(i):
      if count[j] == tmp:
        nsame = nsame + 1
  count.append(tmp)
print(nsame)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())


hash_t = {}
for i in range(N):
    a = "".join(sorted([c for c in input()]))
    if a in hash_t.keys():
        hash_t[a] += 1
    else:
        hash_t[a] = 0


def fac(n):
    """階乗"""
    res = 0
    for i in range(1, n+1):
        res += i
    return res
cnt = 0
for x in hash_t.values():
    if x == 0:
        continue
    cnt += fac(x)
print(cnt)

Submission status: Time Limit Exceeded

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

count = 0
list1 = []
for i in range(n):
    dic1 = {}
    for k in range(10):
        
        if s[i][k] in dic1:
            dic1[s[i][k]] += 1
        else:
            dic1[s[i][k]] = 1
    list1.append(dic1)
        
for i in range(n):
    for j in range(i + 1, n):
        if list1[i] == list1[j]:    
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
aL = [set(Counter(input()).most_common()) for i in range(n)]
count = 0

for i in range(n):
    for j in range(n):
        if i<j:
            if aL[i] == aL[j]:
                count +=1

print(count)

Submission status: Time Limit Exceeded

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

from collections import Counter
new_s = []
for i, s in enumerate(s_list):
    new_s.append(Counter(sorted(list(s))))

match = []
M=[i for i in range(N)]
center = int(N/2)
count = 0
if N%2==0: # 偶数の時
    for i in range(N-1):
        for i in range(center):
            match.append((M[0+i],M[-1-i]))
        M = M[:1] + M[2:] + M[1:2]
else: # 奇数の時
    for i in range(N):
        for i in range(1,center+1):
            match.append((M[i],M[-i+1]))
        M = M[1:] + M[:1] # １つずつずらす
for m in match:
    if new_s[m[0]] == new_s[m[1]]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
def input(): return sys.stdin.readline().strip()
def gcd(x, y): return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y): return (x * y) // gcd(x, y)
MOD = 10 **9 + 7

N = int(input())
# S = input()
# N, M = map(int, input().split())
# 横
# L = [int(x) for x in input().split()]
# 縦
S = [collections.Counter(input()) for _ in range(N)]
# 多次元
# L = [[int(x) for x in input().split()] for _ in range(N)]

ans = 0
for i, s in enumerate(S) :
    for ss in range(i + 1, N) :
        if s == S[ss] :
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8


def main():
    n = int(input())
    s = [input() for i in range(n)]
    e = []
    for i in range(n):
        word = [0] * 200
        for w in s[i]:
            word[ord(w)] += 1

        e.append(word)

    ans = 0
    for i in range(n-1):
        for j in range(i+1, n):
            if e[i] == e[j]:
                ans += 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0

for i in range(N):
    ans += S[i+1:].count(S[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = [''.join(sorted(input())) for i in range(n)]
c = list(set(b))
d = len(c)
cnt = [b.count(c[i]) for i in range(d)]
ans = 0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

s=list(itertools.combinations(S,2))
result=0
for anag in s:
    hikaku1=list(anag[0])
    hikaku2=list(anag[1])
    for a in hikaku1:
        if a in hikaku2:
            hikaku2.remove(a)
        else:
            break
        if not hikaku2:
            result+=1
print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = [0]*N
ans = 0
p = []
for i in range(N):
    s[i] = sorted(input())
    k = s[i]
    ans += p.count(k)
    p.append(k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

arr = []
for i in range(N):
    arr.append(str(input()))

count = 0

def sort_str(s):
    return "".join(sorted(list(s)))


sorted_str_arr = []

for item in arr:
    sorted_str_arr.append(sort_str(item))

for i, item in enumerate(sorted_str_arr):
    for j in range(i+1, N):
        if item == sorted_str_arr[j]:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def f(array):
    temp = 'abcdefghijklmnopqrstuvwxyz'
    ret = [0] * len(temp)
    cnt = 0
    for x in array:
        ret[temp.index(x)] += 1
        cnt += 1
        if cnt == 10:
            break
    return ret


N = int(input())
s = [f(input()) for i in range(N)]
check = [False] * N
cnt = 0
for i in range(N - 1):
    part = 0
    if check[i]:
        continue
    for j in range(i + 1, N):
        if check[j]:
            continue
        if s[i] == s[j]:
            part += 1
            check[j] = True
    cnt += (part + 1) * part // 2

print(cnt)

Submission status: Time Limit Exceeded

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

S_ = [[0] * 26] * N

alphabet = "abcdefghijklmnopqrstuvwxyz"

for j in range(N):
    temp = []
    for i in range(26):
        temp.append(S[j].count(alphabet[i]))
    S_[j] = temp

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

def encode(s):
    alp = [0]*26
    for c in s:
        alp[ord(c)-97] += 1
    return tuple(alp)

def main():
    N = int(sys.stdin.readline())
    S = [encode(x.rstrip()) for x in sys.stdin.readlines()]

    dic = {}
    for i, s in enumerate(S):
        if s not in dic:
            dic[s] = [i]
        else:
            dic[s].append(i)

    count = 0
    for i, s in enumerate(S):
        count += len([x for x in dic[s] if x > i])

    print(count)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

sn = []
count = 0

#for i in range(n):
#  sn.append(sorted(Counter(list(input())).items()))
for i in range(n):
  sn.append(input())


for i in range(n - 1):
  for j in range(i+1,n):
    if verify_anagrams(sn[i], sn[j]):
      count += 1
print(count)

Submission status: Time Limit Exceeded

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

for i in range(N):
    S[i].sort()
#print(S)

COUNT = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        flag = 0
        for k in range(10):
            if S[i][k] != S[j][k]:
                flag = 1
                break
        if flag == 0:
            COUNT += 1
print(COUNT)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = int(input())
s_list = []
for i in range(N):
    s_in = list(input())
    s_list.append(sorted(s_in))
count = 0
for i in range(N):
    si = s_list[i]
    for j in range(i+1, N):
        if si == s_list[j]:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

tmp = []
counter = 0
for i in range(N):
    new_str = ''.join(list(sorted(str(input()))))
    if i != 0:
        counter += tmp.count(new_str)
    tmp.append(new_str)

    #for j in range(i):
    #    if str(new_str) == str(tmp[j]):
    #        counter += 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
anagram = [''.join(sorted(input())) for _ in range(N)]
anagram_set = set(anagram)
test = lambda a: lambda b: a==b
print(sum({i:sum(range(sum(map(test(i),anagram)))) for i in anagram_set}.values()))

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8

N = int(input())


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

count = 0
s_list.sort()
while len(s_list) != 0:
    for i in range(1, len(s_list)):
        if s_list[0] == s_list[i]:
            count = count + 1
        else:
            break

    del(s_list[0])
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

import copy

def verify_anagrams(first_word, second_word):
    return Counter(first_word) == Counter(second_word)

n=int(input())
string_list=[input() for i in range(n)]
string_list2=copy.copy(string_list)
count=0
i2=0
for string1 in string_list:
    string_list2.remove(string_list[i2])
    i2=i2+1
    for string2 in string_list2:
        if(verify_anagrams(string1, string2)):
            count=count+1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
my_list = [None] * N
for i in range(N):
    s_i = sorted(input())
    my_list[i] = s_i
sum = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        for k in range(10):
            if not(my_list[i][k] == my_list[j][k]):
                break
            elif k == 9:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
lis = []
ans = 0

for i in range(N):
    lis.append("".join(sorted(input())))

for i in set(lis): 
    n = lis.count(i) - 1
    ans += int(0.5*n*(n+1))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

s = []
s_ap = s.append

for i in range(N):
    s_ap("".join(sorted(input())))

s.sort()

s_set = set(s)

result = 0

for _ in s_set:
    cnt = s.count(_)
    result += int(cnt * (cnt - 1) / 2)

print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s_li = []
for i in range(n):
    s = input()
    s_i = {}
    for j in range(10):
        s_i[s[j]] = 0
    for j in range(10):
        s_i[s[j]] += 1    
    s_li.append(s_i)

ans = 0

for i in range(n):
    for j in range(n):
        if i >= j :
            continue
        if s_li[i] == s_li[j]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
n = int(input())
s = []
ans = 0
for i in range(n):
    sn = input()
    sn = sorted(sn)
    sn = ''.join(sn)
    s.append(sn)
str_set = set(s)
for i in str_set:
    n = s.count(i)
    if n > 1:
        ans += combinations_count(n, 2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
aL = [input() for i in range(n)]
count = 0

countL = [Counter(aL[i]).most_common() for i in range(n)] 

for i in range(n):
    for j in range(n):
        if i<j:
            if set(countL[i]) == set(countL[j]):
                count +=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
 

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

def verify_anagrams(first_word, second_word):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))
 
    return get_counter(first_word) == get_counter(second_word)

count=0
cc= 0
for i in range(n):
    cc +=1
    for j in range(i+1,n,1):
        if verify_anagrams(t[i],t[j]) == True :
            count +=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

s = []
k = []
count = 0

for i in range(N):
    w = input()
    for j in range(10):
        k.append(w[j])
        k.sort()
    s.append("".join(k))
    k = []

for i in range(0, N-1):
    w2 = s[i]
    for j in range(i+1, N):
        if w2 == s[j]:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def alphas(key):
    value = {}
    for char in key:
        if value.get(char) is None:
            value[char] = 1
        else:
            value[char] += 1
    return value

def main():
    N = int(input())
    anagrams = []
    for i in range(N):
        s = input()
        anagrams.append(s)
    ans = 0
    json = {}
    for key in anagrams:
        value = alphas(key)
        for json_key in json.keys():
            if json[json_key] == value:
                ans += 1
        json[key] = value

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    for j in range(N):
        if not i == j:
            si = sorted(s[i])
            sj = sorted(s[j])
            if (si == sj):
                ans += 1
print(int(ans/2))

Submission status: Time Limit Exceeded

Solution code:
import copy
n=int(input())
string=[]
ang=0
cou=0
for c in range(n):
    string.append(input())
    
for c in range(n):
    for d in range(c+1,n):
        cou=0
        tmp=list(copy.copy(string[d]))
        for e in range(10):
            found = 0
            for f in range(10):
                if string[c][e]==tmp[f]:
                    found=1
                    cou+=1
                    tmp[f]='Z'
                    break
            if found==0:
                break
        if cou==10:
            ang+=1
print(ang)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

result = 0
for i, elm in enumerate(s):
    for j in range(i+1, N):
        if s[j] == elm:
            result += 1

print(result)

Submission status: Time Limit Exceeded

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

ab = "abcdefghijklmnopqrstuvwxyz"
ab_dict = {}
for i,c in enumerate(ab):
    ab_dict[c] = i

def count_c(s):
    count_list = [0]*26
    for c in s:
        count_list[ab_dict[c]] += 1
    for i in range(26):
        count_list[i] = str(count_list[i])
    return count_list

sc_list = []
for s in s_list:
    sc = count_c(s)
    sc = ''.join(sc)
    # print(sc)
    sc_list.append(int(sc))

ans = 0
for i,sc in enumerate(sc_list):
    for sc2 in sc_list[i+1:]:
        if sc == sc2:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
s=[Counter(input()) for i in range(n)]
s = [sorted(i.items(), key=lambda x:x[0]) for i in s]
s = ["".join(str(i)) for i in s]
dp={}
for i in range(n):
  dp.setdefault(s[i],0)
  dp[s[i]]+=1
ans=0
for i in dp.keys():
  ans+=dp[i]*(dp[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def green_bin(n,s):
    count = 0
    x = []
    for i in range(n):
        tmp = sorted(s[i])
        x.append(tmp)

    for i in range(n-1):
        si = x[i]
        for j in range(i+1,n):
            if i == j:
                continue
            else:
                sj = x[j]
                if si == sj:
                    count += 1

    return count
if __name__ == '__main__':
    n = int(input())
    s = [ input() for i in range(n)]

    print(green_bin(n,s))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

sn = []
count = 0

#for i in range(n):
#  sn.append(sorted(Counter(list(input())).items()))
for i in range(n):
  sn.append(input())


for i in range(n - 1):
  for j in range(i+1,n):
    if verify_anagrams(sn[i], sn[j]):
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for i in range(n):
    sin = input()
    sin = list(sin)
    sin.sort()
    sin = ''.join(sin)
    s.append(sin)

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

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def c(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

cnt = 0
s = []
tmp = []
n = int(input())
for i in range(n):
    string = input()
    string = ''.join(sorted(string))
    s.append(string)

for i, e in enumerate(s):
    """
    要素が何個あるか検査　AND　tmpにないやつ
    ・１：なにもしない
    ・2以上：個数調べてCNT更新。その要素をtmpに記録する
    """
    if s.count(e) > 1 and not e in tmp:
        cnt += c(s.count(e), 2)
        tmp.append(e)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

S = []
for i in range(n):
    s = input()
    s = sorted(list(s))
    s = str(s)
    S.append(s)

T = set(S)

ans = 0
cnt = [S.count(T.pop()) for i in range(len(T))]
for c in cnt:
    for i in range(c):
        ans += i

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def f(si, sj):
    for i in range(len(sj)):
        for j in range(len(si)):
            if si[j] == sj[i]:
                si = si[:j] + si[j+1:]
                break
    if len(si) == 0:
        return 1
    else:
         return 0

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

x = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = x + f(s[i], s[j])
print(x)

Submission status: Time Limit Exceeded

Solution code:
inp = int(input())
A = []
for a in range(inp):
    A.append(input())

def charcnt(s):
    cset = {}
    for c in s:
        if c in cset:
            cset[c] += 1
        else:
            cset[c] = 1
    sortedset = sorted(cset.items(), key=lambda x:x[0])
    s = ""
    for k, v in sortedset:
        s += k + str(v)
    return s

sorted = [charcnt(v) for v in A]    

cnt = 0
for i1, s1 in enumerate(sorted[:-1]):
    for s2 in sorted[i1 + 1:]:
        if s1 == s2:
            cnt += 1 
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = [0]*N
for i in range(N):
    s[i] = input()
ans = 0
p = []

for i in range(N):
    k = sorted(s[i])
    ans += p.count(k)
    p.append(k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#[int(i) for i in input().split()]
import math
n = int(input())
s = []
cnt = 0
for i in range(n):
    m = input()
    m = sorted(m)
    p = ""
    for j in range(10):
        p += m[j]
    s.append(p)
dic = {}
#print(s)

for i in range(n):
    if s[i] in dic.keys():
        dic[s[i]] += 1
    else:
        dic[s[i]] = 1
for a in dic.keys():
    if dic[a] != 1:
        cnt += (dic[a] * (dic[a]-1))//2
print(cnt)

"""
n = int(input())
cnt = 0
["a","b","c","d","e"]
for i in range(n):

print(math.factorial(4))
"""

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
s = []
ans = 0
for _ in range(N):
    c = collections.Counter(input())
    ans += s.count(c)
    s.append(c)
print(ans)

Submission status: Time Limit Exceeded

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

for j in range(len(lst)):
    for k in range(j+1, len(lst)):
        if lst[j] == lst[k]:
            counter += 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
class string:
    def __init__(self):
        self.name=''
        self.c=dict()

N=int(input())
S=list()
count=0
for i in range(N):
    s=input()
    s1=string()
    s1.name=s
    S.append(s1)

for i in range(N):
    j=0
    while j <=9:
        char=S[i].name[j]
        if char not in S[i].c:
            S[i].c[char]=1
        else:
            S[i].c[char]+=1
        j+=1

for i in range(N-1):
    j=i+1
    while j<=N-1:
        if S[i].c==S[j].c:
            count+=1
        j+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
count=0
s=[]
for i in range(N):
    s.append(input())
for t in range(N):
    for i in range(N):
        if t>=i:
            continue
        if Counter(s[t])==Counter(s[i]):
            count+=1
        if i==t:
            count-=1
print(int((count)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

s = []
for _ in range(N):
  t = sorted(list(str(input())))
  s.append(''.join(t))          

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
sl = [input() for _ in [0]*n]
for i in range(len(sl)):
  sl[i] = sorted(sl[i])
  sl[i] = ''.join(sl[i])
count = 0
sl.sort()
while len(sl)>1:
  i = 1
  flag = True
  while flag:
    if sl[-1] == sl[-(i+1)]:
      i += 1
      if i == len(sl):
        flag=False
    else:
      flag = False
  if i>1:
    count += i*(i-1)//2
  sl = sl[:-i]
  flag=True
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

string = []
count = 0
# a-z : 0-25
# print(ord('a')-97, ord('z')-97)

table = [[0 for i in range(26)] for _ in range(n)]

for i in range(n):
    for s in str(input()):
        table[i][ord(s)-97] += 1

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

Submission status: Time Limit Exceeded

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

dp = [-1]*N
ans = 0

def hashtable(s):
  a = {}
  for i in s:
    if i in a: a[i] += 1
    else:      a[i] =  0
  return a


def anagram(s, t):
  a = hashtable(s)
  b = hashtable(t)
  return a == b

def filterhaUNKO(func, li):
  a = []
  for i in li:
    if not func(i):
      a.append(i)
  return a

def sigma(N):
  return (N*(N+1)) // 2

while len(s) > 1:
  last = s.pop()
  pre_length = len(s)
  s = filterhaUNKO(lambda x: anagram(x, last), s)
  post_length = len(s)
  ans += sigma(pre_length - post_length)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(sorted(list(input())))
cnt_ans = 0
while len(s):
    remove_list = []
    cnt = 0
    for i in range(1, len(s)):
        if s[0] == s[i]:
            cnt += 1
            remove_list.append(s[i])
    cnt = int((cnt+1) * (cnt) / 2)
    cnt_ans += cnt
    remove_list.append(s[0])
    for lst in remove_list:
        s.remove(lst)
print(cnt_ans)

Submission status: Time Limit Exceeded

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

n = int(input())

#l = [i for i in range(n)]

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

count = 0
"""
count_l = []
count = 0
while 1:
    ll = copy.deepcopy(l)
    for i in range(1, len(ll)):
        if Counter(s[ll[0]]) == Counter(s[ll[i]]):
            count += 1
            l.pop(i)
    if count == 0:
        break
    count_l.append(count)
"""
for i in range(len(s)-1):
    for j in range(i+1, len(s)):
        if Counter(s[i]) == Counter(s[j]):
            count += 1

print(count)

Submission status: Time Limit Exceeded

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

def verify_anagrams(first_word, second_word):
  def get_counter(s):
    return Counter(s)
  return get_counter(first_word) == get_counter(second_word)

sn = []
count = 0

#for i in range(n):
#  sn.append(sorted(Counter(list(input())).items()))
for i in range(n):
  sn.append(input())


for i in range(n - 1):
  for j in range(i+1,n):
    if verify_anagrams(sn[i], sn[j]):
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = [input() for _ in range(n)]
alrdy = []
 
def is_anagrams(word1, word2):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))
 
    return get_counter(word1) == get_counter(word2)

cnt = 0
for i in range(n):
  for j in range(n):
    if i == j:
      continue
    if is_anagrams(s[i], s[j]):
      cnt += 1
print(cnt//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[]
for i in range(n):
  t=list(input())
  t.sort()
  l.append("".join(t))
l.sort()
length=len(l)
count=0
while True:
  if len(l)==0:break
  a=l.count(l[0])
  if a == 0:
    del l[0]
    continue
  t=0
  for i in range(1,a):
    t+=i
  count += t
  del l[0:a]
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
#S = [input() for i in range(N)]
S=[]
ans_list = []
temp_ans = 0
ans = 0

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

string_set = set(S)

for s in string_set:
    for i in S:
        if s==i:
            temp_ans += 1
    ans_list.append(temp_ans)
    temp_ans = 0

for a in ans_list:
    ans += a*(a-1)/2

print(round(ans))