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:
count = 0
scounts = []
N = int(input())
for n in range(N):
    s = sorted(input())
    found = False
    index = -1
    for i in range(len(scounts)):
        # print("s="+str(s))
        if scounts[i][0] == s:
            # print("EQUALS : "+str(scounts[i][0])+" == "+str(s))
            found = True
            count += scounts[i][1]
            scounts[i][1] += 1
            break
        elif scounts[i][0] > s:
            # print("MORE THAN : "+str(scounts[i][0])+" > "+str(s))
            index = i
            break
        else: # scounts[i][0] > s:
            pass
            # print("LESS THAN : "+str(scounts[i][0])+" < "+str(s))

    # print("index="+str(index))
    if not found:
        if index<0:
            # print("append(["+str(s)+",1]")
            scounts.append([s,1])
        else:
            # print("insert("+str(index)+",["+str(s)+",1]")
            scounts.insert(index, [s, 1])

        # print(str(s))
        # print("DONE: "+str(scounts))
    # print("===")
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = sorted([sorted(input()) for i in range(n)])
    ans = 0
    while len(s) != 0:
        cnt = s.count(s[0])
        del s[0:cnt]
        if cnt > 1:
            ans += (cnt * (cnt - 1)) // 2
    print(int(ans))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


cnt = 0
for i in range(len(s)):
    tmp = s[i+1:].count(s[i])
    cnt += tmp

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = list()
num = 0
for i in range(n):

    s = input()
    s = list(s)
    s.sort()
    lst.append(s)


for i in range(n):
    for j in range(i+1, n):
        if (lst[i] == lst[j]):
            num = num + 1
print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
slist = [sorted(input()) for _ in range(N)]
cnt = 0

for i in range(N):
  if slist[i] in slist[i+1:]:
    cnt += slist.count(slist[i])

print(int(cnt/2))

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
count = 0
word = []

for i in range(n):
    word.append(input())

for i in range(n - 1):    
    master = list(word[i])
    for j in range(i, n):
        if i == j:
            continue
        target = list(word[j])
        for k in range(len(master)):
            l = 0
            while True:
                try:
                    if master[k] == target[l]:
                        target.pop(l)
                    l += 1
                except:
                    if len(target) == 0:
                        count += 1
                    break

print(int(count**0.5))

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys

input = sys.stdin.readline
N=int(input().strip())
S = [input().strip() for _ in range(N)]

ans = 0
for i in range(N):
    ptn = collections.Counter(S[i])
    for j in range(i+1, N):
        if ptn == collections.Counter(S[j]):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ls = [input() for i in range(N)]

ls = [sorted(list(s)) for s in ls]

hoge = 0
for _ in range(N):
    hoge += sum([1 if x==ls[0] else 0 for x in ls]) - 1
    del ls[0]

print(hoge)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for i in range(n)]
cnt = 0
for i in range(0, n-1):
    stype1 = {}
    for j in range(len(s[i])):
        if s[i][j] in stype1 :
            stype1[s[i][j]] += 1
        else:
            stype1[s[i][j]] = 1
    for k in range(i+1, n):
        stype2 = {}
        for l in range(len(s[k])):
            if s[k][l] in stype2:
                stype2[s[k][l]] += 1
            else:
                stype2[s[k][l]] = 1
        if stype1 == stype2:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s_list=[]

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

unq_list=list(set(s_list))
count=0

for j in range(len(unq_list)):
  match_n=s_list.count(unq_list[j])
  count+=sum(range(1, match_n))

print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

s=[]
so=[]
for i in range(n):
    s.append(input())
    so.append(sorted(s[i]))
count=0
for i in range(n-1):
    for j in range(n-i-1):
        if so[i]==so[n-j-1]: 
            count=count+1


print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

def listdict(s1):
    ret={}
    for i in s1:
        tmp=ret.get(i,0)
        if tmp==0:
            ret[i]=1
        else:
            ret[i]+=1
    return ret

sN=[listdict(input()) for _ in range(N)]
ret=0

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

print(ret)

Submission status: Time Limit Exceeded

Solution code:
import math

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

n = int(input())
# k,x = map(int,input().split())
s_l = []
for i in range(n):
        s_l.append(input())

for i in range(n):
        a = ''
        for x in sorted(s_l[i]):
                a += x
        s_l[i] = a

cnt = 0
# for i in range(n):
#         for j in range(1,n-i):
#                 if s_l[i] == s_l[i+j]:
#                         cnt += 1
set_s_l = set(s_l)
if len(s_l) == len(set_s_l):
        print(0)
else:
        l = []
        for val in set_s_l:
                l.append(s_l.count(val))
        for i in l:
                if i > 1:
                        if i == 2:
                                cnt += 1
                        elif i == 3:
                                cnt +=i
                        else:
                                cnt += c(i,2)
        print(cnt)


# 0 1 2 3 4
#     2

Submission status: Time Limit Exceeded

Solution code:
n =int(input())
A=[]
for I in range(n):
  moji=input()
  moji=list(moji)
  moji.sort()
  moji=''.join(moji)
  A.append(moji)
N=0
for i in range(n):
  a=A[i]
  for s in range(i+1,n,1):
    b=A[s]
    if a==b:
      N+=1
print(N)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

S = []
cnt = {}
for i in range(n):
    s = input()
    s = sorted(list(s))
    s = str(s)
    if s not in S:
        S.append(s)
        cnt[s] = 0
    else:
        cnt[s] += 1

ans = 0
for s in S:
    c = cnt[s]
    ans += c * (c+1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import Counter
  N = int(input())
  S = [input() for _ in range(N)]

  tmp = []
  d={}
  for s in S:
    x = Counter(s)
    if x in tmp:
      d[tmp.index(x)] += 1
    else:
      tmp.append(x)
      d[tmp.index(x)] = 1 
      
  cnt = 0
  for v in d.values():
    if v == 1:
      continue
    if v == 2:
      cnt += 1
    elif v == 3:
      cnt += v
    else:
      cnt += sum(list(range(1,v)))

  print(cnt)
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
def intersect_list(lst1, lst2):
    arr = []
    lst = lst1.copy()
    for element in lst2:
        try:
            lst.remove(element)
        except ValueError:
            pass
        else:
            arr.append(element)
    return len(arr)

n=int(input())
a=[list(input()) for i in range(n)]
count=0
for i in range(len(a)):
    for j in range(i+1,len(a)):
        if intersect_list(a[i],a[j])==len(a[i]):
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def sumsun(n):
  ret = 0
  for i in range(n,0,-1):
    ret = ret + i
    
  return ret

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

for i in range(N):
  hoge = input()
  s.append("".join(sorted(hoge)))
  
god = list(set(s))
  
ans = 0
itr = 0
for i in range(len(god)):
  ans = ans + sumsun(s.count(god[i])-1)
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
import itertools
sysinput = sys.stdin.readline

N = int(sysinput())
s = [sysinput() for i in range(N)]
ans = 0
S = [Counter(s[i].rstrip('\n')) for i in range(N)]
for i, j in itertools.combinations(range(N), 2):
  if S[i] == S[j]:
    ans += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for i in range(n)]
s = list(map(sorted,s))
ss = list("".join(i) for i in s)
score = 0
for j in range(len(ss)):
    for k in range(j+1,n):
        if ss[j] == ss[k]:
            score += 1
print(score)

Submission status: Time Limit Exceeded

Solution code:
import re
N = int(input())
al = "abcdefghijklmnopqrftuvwxyz"
S = []
for n in range(N):
    s = input()
    S.append(''.join(sorted(s)))

# print (S)
cnt = 0
for n in range(N):
    cnt += S[n:].count(S[n])

print (cnt - N)

Submission status: Time Limit Exceeded

Solution code:

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

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

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

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

    return result


N=int(input())

S=[]
S_count=dict()
fixed_count=dict()
for n in range(N):
    s=input()
    s=''.join(sorted(s))
    if s in S:
        S_count[s]+=1
    else:
        S_count[s]=1
    S.append(s)

for k in S_count.keys():
    if S_count[k]>1:
        fixed_count[k]=S_count[k]

key=fixed_count.keys()

total=0

for k in key:
    t=fixed_count[k]
    total+=cmb(t,2)

print(total)

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))
s_list.sort()
count = 0
for i in range(N):
    for j in range(i+1, N):
        if s_list[i] == s_list[j]:
            count += 1
        else:
            break
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[input() for _ in range(n)]
c = 0
for i in range(n):
    s[i] = "".join(sorted(list(s[i])))
    for j in range(i):
        for m in range(10):
            if list(s[i])[m:] != list(s[j])[m:]:
                break
            if m == 9:
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

new_s = []
for i, s in enumerate(s_list):
    new_s.append(set(s))

match = []
new_match = []
M=[i for i in range(N)]
center = int(N/2)

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]]:
        new_match.append(m)

a = []
count = 0
for nm in new_match:
    if [{char, s_list.count(char)}for char in list(set(s_list[nm[0]]))] == [{char, s_list.count(char)}for char in list(set(s_list[nm[1]]))]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[]
cnt=0
for i in range(n):
    s=input()
    s_list=list(s)
    s_list.sort()
    s_str=str(s_list)
    l.append(s_str)
    if l.count(l[i])>1:
        cnt=cnt+l.count(l[i])-1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools

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

check = [[0] * N for _ in range(N)]


cnt = 0
for i in range(N):
    c = []
    x = collections.Counter(moji[i])

    c.append(i)

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

        y = collections.Counter(moji[j])

        if x == y:
            c.append(j)

    if len(c) > 1:
        for u, v in itertools.combinations(c, 2):
            check[u][v] = 1
            cnt += 1

print(cnt)

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(sc)

ans = 0
for i,sc in enumerate(sc_list):
    for sc2 in sc_list[i+1:]:
        for i in range(26):
            if sc[i] != sc2[i]:
                break
            if i == 25 and sc[i] == sc2[i]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
l = list(str(input()) for _ in range(n))
d = [{} for _ in range(n)]

for m, a in enumerate(l):
    for p in range(10):
        d[m][a[p]] = a.count(a[p])
cnt = 0
for i, j in enumerate(d):
    for k in range(i+1, len(d)):
        if j == d[k]:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

# print(N, s_list)
s_list2 = []

for s in s_list:
	s_ = list(s)
	s_.sort()
	# print(s)

	s = ''
	for c in s_:
		s += c
	# print(s)
	s_list2.append(s)

# print(s_list2)
s_list3 = list(s_list2)
count = 0

for i in range(len(s_list2)):
	for j in range(i+1, len(s_list2), 1):
		if s_list2[i] == s_list2[j]:
			count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
strList = []
for i in range(N):
    inStr = input()
    inList = []
    for j in range(len(inStr)):
        inList.append(inStr[j])
    inList.sort()
    strList.append(''.join(inList))

anaglam = 0
for i in range(N):
    for j in range(i+1,N):
        if strList[i] == strList[j]:
            anaglam = anaglam + 1
print(anaglam)

Submission status: Time Limit Exceeded

Solution code:
def intersect_list(lst1, lst2):
    arr = []
    lst = lst1.copy()
    for element in lst2:
        try:
            lst.remove(element)
        except ValueError:
            pass
        else:
            arr.append(element)
    return len(arr)

n=int(input())
a=[input() for i in range(n)]
a=list(map(lambda x:list(x),a))
count=0
for i in range(len(a)):
    for j in range(i+1,len(a)):
        if intersect_list(a[i],a[j])==len(a[i]):
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lis=[]
ans=[]
for i in range(n):
    lis.append(input())
    ans.append([0,0,0,0,0,0,0,0,0,0])
    for j in range(10):
        ans[i][j]=lis[i][j]
    ans[i].sort()

ans.sort()
a=0
for i in range(n-1):
    for j in range(n-1-i):
        if ans[i]==ans[i+j+1]:
            a+=1
        else:
            break

print(int(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

def make_charcount(word):
    charcount = {}
    for c in word:
        if c not in charcount:
            charcount[c] = 1
        else:
            charcount[c] += 1
    return charcount

charcount_list_index = 0
charcount_list = {}
same_log = {}

for i in range(n):
    word = input()
    this_charcount = make_charcount(word)
    index = -1
    for i in charcount_list:
        if charcount_list[i] == this_charcount:
            index = i
            break
    if index != -1:
        if index not in same_log:
            same_log[index] = 1
        else:
            same_log[index] += 1
    else:
        charcount_list[charcount_list_index] = this_charcount
        charcount_list_index += 1

result = 0
for index in same_log:
    cnt = same_log[index]
    result += int(cnt*(cnt+1)/2)
print(result)

Submission status: Time Limit Exceeded

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

for i in range(N):
  S = input()
  letters = list(S)
  letters.sort()
  S = "".join(letters)
  Candidate.append(S)

counter = 0
for i in range(N):
  for k in range(N-i-1):
    if Candidate[i] == Candidate[i+k+1]:
      counter += 1
print(counter)

Submission status: Time Limit Exceeded

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

for i in range(n):
    s[i] = sorted(s[i])
s.sort()
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        if s[i] == s[j]:
            cnt += 1
        else:
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():

    N = int(input())
    sList = [list(input()) for _ in range(N)]
    ans = 0

    for i in range(N-1):
        pre=Counter(sList[i])
        j=i+1
        while(j!=N):
#            print(pre)
#            print(Counter(sList[j]))
            if pre==Counter(sList[j]):
                ans += 1
            j += 1

    print(ans)








    
if __name__== "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=[''.join(sorted(input())) for _ in range(n)]
an=0

for i in set(c):
    t=c.count(i)
    for j in range(t):
        an+=j

print(an)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for i in range(n) :
	s.append(sorted(input()))
c = 0
 
for i in range(len(s)) :
	if s[i] in s[i:] :
		for j in range(i+1, len(s)) :
			if s[i] == s[j] : c += 1
print(c)

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 = []

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)
"""
if len(s) > 1000:
    k = 1000
else:
    k = len(s)
    
for i in range(k-1):
    for j in range(i+1, k):
        if Counter(s[i]) == Counter(s[j]):
            count += 1

print(count)

Submission status: Time Limit Exceeded

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

k = 0
while len(S) > 0:
    ss = S[0]
    n = S.count(ss)
    k += n*(n-1)//2
    while ss in S:
        S.remove(ss)
print(k)

Submission status: Time Limit Exceeded

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

def verify_anagrams(w1, w2):
  return Counter(w1) == Counter(w2)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys

input = sys.stdin.readline
N=int(input().strip())
S = [input().strip() for _ in range(N)]

ans = 0
stack = []
for i in range(N):
    if i == 0:
        stack.append([collections.Counter(S[i]), 1])
    else:
        for j in range(len(stack)):
            if stack[j][0] == collections.Counter(S[i]):
                stack[j][1] += 1
                break
        else:
            stack.append([collections.Counter(S[i]), 1])

ans = 0
for _, s in stack:
    ans += sum([i for i in range(1, s)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
input = sys.stdin.readline

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

str_dict = [0]*(n)

for i in range(n):
    str_dict[i] = collections.Counter(strs[i])

ans = 0

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

print(int(ans/2))

Submission status: Time Limit Exceeded

Solution code:

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

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

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

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

    return result


N=int(input())

S=[]
S_count=dict()
fixed_count=dict()
for n in range(N):
    s=input()
    s=''.join(sorted(s))
    if s in S:
        S_count[s]+=1
    else:
        S_count[s]=1
    S.append(s)

for k in S_count.keys():
    if S_count[k]>1:
        fixed_count[k]=S_count[k]

key=fixed_count.keys()

total=0

for k in key:
    t=S_count[k]
    if t>=2:
        total+=cmb(t,2)

print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) 
s = [input() for i in range(N)]
alp=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
     'u','v','w','x','y','z']
LIS=[]
nu=0
for a in range(N):
  for b in range(a+1,N):
    c=0
    while c<26:
      if s[a].count(alp[c])==s[b].count(alp[c]):
        c+=1
      else:
        break
    if c==26:
      nu+=1
    else:
      pass
print(nu)

Submission status: Time Limit Exceeded

Solution code:
import collections

def main():
    N=int(input())
    counters=[]
    strCount=[0]*100000
    for i in range(N):
        anagramExists=False

        str=input()
        c=collections.Counter(str)
        for j,counter in enumerate(counters):
            anagram=True
            for key in c:
                if c[key]!=counter[key]:
                    anagram=False
                    break
            if anagram:
                anagramExists=True
                strCount[j]+=1
        if anagramExists==False:
            counters.append(c);

    total=0
    for n in strCount:
        total+=(n*n+n)//2

    print(total)

main()

Submission status: Time Limit Exceeded

Solution code:
import collections

def main():
    n = int(input())
    c = [[] for i in range(n)]
    count = 0
    for num,i in enumerate(c):
        c[num] = collections.Counter(input())

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

    print(count)


if __name__ == '__main__' :
    main()

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
a=0
while True:
  if a>=length:break
  c=l.count(l[a])
  if c == 0:
    a+=1
    continue
  else:
    t=0
    for i in range(1,c):
      count+=i
    a+=c
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(input())
alphabet = [chr(ord("a") + i) for i in range(26)]
count = [[0] * 26 for _ in range(n)]
for i in range(n):
    for s in sys.stdin.readline().strip():
        count[i][alphabet.index(s)] += 1

uniq = set(list(map(tuple, count)))
ans = 0
for u in uniq:
    n = count.count(list(u))
    ans += n * (n - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s_list = []
n_list = []
ana = 0
for i in range(n):
    s = sorted(input())
    #print(s)
    if len(s_list) > 0:
        for num, s_i in enumerate(s_list):
            is_exist = False
            if s_i == s:
                n_list[num] += 1
                is_exist = True
                break
        if not is_exist:
            s_list.append(s)
            n_list.append(0)
    else:
        s_list.append(s)
        n_list.append(0)


for n_i in n_list:
    if n_i > 0:
        if n_i == 1:
            ana += 1
        else:
            ana += math.factorial(n_i+1) / math.factorial(n_i-1) / 2
print(int(ana))

Submission status: Time Limit Exceeded

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

import collections
import sys
input = sys.stdin.readline


def main():
    n = int(input())
    s = [None] * n
    for i in range(n):
        s[i] = str(input()).replace("\n", "")

    ans = [None] * n
    index = 0
    for i in s:
        count_word = dict()
        sorted_word = list()
        for j in i:
            if not j in sorted_word:
                count_word[j] = i.count(j)
                sorted_word.append(j)

        ans[index] = count_word
        index += 1

    pairs = list()
    sorted_num = list()
    for i in range(n):
        if i in sorted_num:
            continue
        for j in range(i + 1, n):
            if ans[i] == ans[j]:
                pairs.append(i)
                sorted_num.append(j)

    count = 0
    for v in collections.Counter(pairs).values():
        if v >= 2:
            count += int(v * (v - 1) / 2) + v
        else:
            count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A=[]
B=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
d=''
count = ''
countw = 0
for i in range(0,N):
    A.append(input())
    for j in range(0, len(B)):
        d = str(A[i].count(B[j]))
        count += d
    A[i]=int(count)
    count=''
for i in range(0,N):
    for j in range(1,N-i):
        if A[i]==A[i+j]:
            countw += 1
print(countw)

Submission status: Time Limit Exceeded

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

'''
d = {}
for s in S:
  if s not in d:
    d[s] = 0
  d[s] += 1
  
ans = 0
for k, v in d.items():
  for i in range(v):
    for j in range(i+1,v):
      ans += 1
'''
def helper(s):
  d = {}
  for c in s:
    if c not in d:
      d[c] = 0
    d[c] += 1
  return d

ans = 0
for i in range(len(S)):
  s1 = helper(S[i])
  for j in range(i+1,len(S)):
    s2 = helper(S[j])
    if s1 == s2:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
hoge = [0]*N
cnt = 0
for i in range(N):
  hoge[i] = list(input())

for i in hoge:
  i = i.sort()

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
s=[sorted(list(input())) for i in range(N)]
s=sorted(s)
count=0
l=[]
for i in range(N-1):
    if s[i][0]==s[i+1][0]:
        l.append(i)
        l.append(i+1)
l.append(N-1)
s=[ s[i] for i in range(N) if i in l  ]
s_l=len(s)
if s_l==0 or s_l==1:
    print(0)
else:
    l=[]
    for i in range(s_l-1):
        if s[i][1] == s[i+1][1]:
            l.append(i)
            l.append(i+1)
    l.append(s_l-1)
    s=[ s[i] for i in range(N) if i in l  ]
    s_l=len(s)
    for i in range(s_l-1):
        for j in range(i+1,s_l):
            if s[i][0]==s[j][0]:
                if s[i]==s[j]:
                    count+=1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[[0 for i in range(10)] for j in range(n)]
for i in range(n):
  s[i]=list(input())
  s[i].sort()
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:
import math


n = int(input())
s = [input() for _ in range(n)]
S = [0]*n
goukei = 0
for i in range(n):
    S[i] = "".join(sorted(s[i]))
C = set(S)
for i in C:
    if S.count(i) >= 2:
        goukei += math.factorial(S.count(i)) // (math.factorial((S.count(i))-2) * math.factorial(2))
print(goukei)

Submission status: Time Limit Exceeded

Solution code:
import sys

n=int(input())
l=[]
for i in range(n):
    l.append(input())
alp=[chr(_) for _ in range(97,97+26)]
res=[]
ans=0

for j in l:
    ll=list(j)
    ll.sort()
    aa="".join(ll)
    tmp2=0
    for z in res:
        if aa==z:
            tmp2+=1
    ans+=tmp2
    res.append(aa)

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
def anagram( w1, w2 ):
    letters = [0]*256
    for c in w1:
        letters[ord(c)] += 1
    
    for i in range(len(w2)):
        letters[ord(w2[i])] -= 1
        if letters[ord(w2[i])] < 0:
            return False
    
    return True


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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

string = []
count = 0

for i in range(n):
    string.append(sorted(input()))

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
import itertools
sysinput = sys.stdin.readline

N = int(sysinput())
s = [sysinput() for i in range(N)]
ans = 0
for i, j in itertools.combinations(range(N), 2):
  if Counter(s[i].rstrip('\n')) == Counter(s[j].rstrip('\n')):
    ans += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
l=[]
for i in range(n):
    s=input()
    l.append(Counter(s))
ans=0
dfs=[1]*n
for i in range(n):
    if dfs[i]!=0:
        a=1
        for j in range(i+1,n):
            if dfs[j]!=0 and l[i]==l[j]:
                dfs[j]=0
                a+=1
        ans+=max(0,a*(a-1)//2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
dictss = {}
ggg = []

for o in range(num):
  s = input()
  dic = {}
  for i in range(len(s)):
    if s[i] in dic:
      dic[s[i]] += 1
    else:
      dic[s[i]] = 1
  
  ggg.append(dic)

ans = 0
fg = 0
for i in ggg:
  fg += 1
  for u in range(fg,len(ggg)):
    if i == ggg[u]:
      ans +=1
  
print(ans)

Submission status: Time Limit Exceeded

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

def isAnagram(s1, s2):
  return 1 if s1 == s2 else 0

print( sum([sum( [isAnagram(s[i], s[j])for j in range(i+1, N)]) for i in range(N)]) )

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):
    result += s[i+1:].count(elm)

print(result)