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:
import sys

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:0}
        else:
            for x in dic[s].keys():
                dic[s][x]+=1
            dic[s][i]=0

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

    print(count)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

num = int(input())
words = []

for i in range(num):
  words.append(Counter(input()))
  

def check_verify(word1,word2):
  return word1 == word2
  
  
count = 0

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

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) > 10000:
    k = 10000
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:
from collections import Counter

N = int(input())
words = [Counter(input()) for _ in range(N)]
ans = 0
while True:
    if not words:
        break
    target = words.pop(0)
    same_word_index = [i for i, word in enumerate(words) if target == word]
    loop_sum = len(same_word_index)
    # 同じやつをリストから消す
    [words.pop(index) for index in reversed(same_word_index)]
    # 合計に加算
    ans += int((loop_sum * (loop_sum+1)) / 2)
    
    # 後ろからも
    if not words:
        break
    target = words.pop()
    same_word_index = [i for i, word in enumerate(words) if target == word]
    loop_sum = len(same_word_index)
    # 同じやつをリストから消す
    [words.pop(index) for index in reversed(same_word_index)]
    # 合計に加算
    ans += int((loop_sum * (loop_sum+1)) / 2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
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=np.zeros(n)
for i in range(n-1):
    for j in range(n-1-i):
        if ans[i]==ans[i+j+1]:
            a[i]+=1
        else:
            break
b=0
for i in range(n):
    b+=a[i]

print(int(b))

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
        v = 1
        for k in range(52):
            if nums[i][k] != nums[j][k]:
                v = 0
                break
        r += v

print(r)

Submission status: Time Limit Exceeded

Solution code:
mst_l = [chr(i) for i in range(97,97+26)]

n = int(input())
l = [input() for _ in range(n)]
l = ["".join(map(str, [x.count(a) for a in mst_l])) for x in l]
result = 0
for i in range(n-1):
    result += l[i+1:].count(l[i])
print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
# 文字列内の文字を辞書順ソート
strings = [''.join(sorted(list(input()))) for i in range(n)]
# 重複文字列（＝元の文字列はアナグラムで一致する）と重複度の辞書
dictionaries = [[e, strings.count(e)] for e in set(strings) if strings.count(e) > 1]
sum = 0
for d in dictionaries :
    q = d[1]
    sum += int(q * (q+1) / 2) - q
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n   = input()
dic = {chr(j):i for i,j in enumerate(range(97, 97+26))}
input_list = []
for i in range(int(n)):
    input_vec   = np.zeros(26)
    for i in input():
        input_vec[dic[i]] += 1
    input_list +=[input_vec]
count = 0
for i,text_1 in enumerate(input_list[:-1]):
    for j,text_2 in enumerate(input_list[:-1]):
        if ((text_1 - text_2)**2).sum() == 0:
            count += 1
print(int(count/2))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s_list = [''.join(sorted(input())) for _ in range(n)]
cnt = 0
for i in range(len(s_list) - 1):
    for j in range(i + 1, len(s_list)):
        if s_list[i] == s_list[j]:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import json
import hashlib

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

ana = []
for i in range(n):
  alpha = {}
  for j in range(10):
    if alpha.get(str[i][j]) == None:
      alpha[str[i][j]] = 1
    else:
      alpha[str[i][j]] += 1
  ana.append(sorted(alpha.items(), key=lambda x:x[0]))

anastr = []
for i in range(n):
  anastr.append(hashlib.md5(json.dumps(ana[i]).encode()).hexdigest())

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

if __name__ == '__main__':
    n = int(input())
    inputs = [input() for _ in range(n)]

    input_chars = [dict(collections.Counter(s)) for s in inputs]
    result = 0
    for i in range(len(input_chars)):
        for j in range(i + 1, len(input_chars)):
            if input_chars[i].items() == input_chars[j].items():
                result += 1
    print(result)

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 (string2[0] in string1) and (string2[1] in string1) and (string2[2] in string1) and (string2[3] in string1) and (
                string2[4] in string1) and (string2[5] in string1) and (string2[6] in string1) and (string2[7] in string1) and (
                string2[8] in string1) and (string2[9] in string1):
            if(Counter(string1) == Counter(string2)):
                count=count+1

print(count)

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] not in dic1:
            dic1[s[i][k]] = s[i].count(s[i][k])
    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())

s = [[] for _ in range(N)]
check = []
ans = []
for x in range(N):
    for c in input():
        s[x].append(c)

for x in s:
    check.append(Counter(x))

for x in range(N):
    if check:
        cc = check.pop(0)
        ans.append(check.count(cc))


print(sum(ans))

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:
                        cnt += c(i,2)
        print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections

alpha2num = lambda c:ord(c) - ord('a')

N = int(input())
D = np.zeros([26,N])
SUM = 0

for n in range(N):
  s = input()
  c = collections.Counter(s)
  for _c in c.keys():
    i = alpha2num(_c)
    D[i, n] = c[_c]

for n in range(N):
  d = D[:,n]
  d = d.reshape(len(d),1)
  diff = D - np.dot(d,np.ones([1,N]))
  count = sum(np.square(diff))
  k = np.count_nonzero(count == 0)
  SUM = SUM + k
  
print('%d' % ((SUM-N)/2))

Submission status: Time Limit Exceeded

Solution code:
import itertools
def count(string):
    sets = sorted(list(set(string)))
    return {s: string.count(s) for s in sets}

def infer(match):
    match_num = 0
    for x, y in itertools.combinations(match, 2):
        if x == y:
            match_num += 1
    
    return match_num

N = int(input())
sents = [input() for i in range(N)]
match = [count(sents[i]) for i in range(N)]
ans = infer(match)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[]
s,a=[],[]
flag=0
for i in range(n):
 l.append(input())
for i in range(n-1):
 s=list(l[i])
 s.sort()
 for j in range(i+1,(i)+(n-i)):
  a=list(l[j])
  a.sort
  a.sort()
  flag+=1
  if s != a:
   flag-=1
print(flag)

Submission status: Time Limit Exceeded

Solution code:
import math
def ncr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n=int(input())
slist=[''.join(sorted(input())) for i in range(n)]
res=0
for s in set(slist):
    cnt = slist.count(s)
    if cnt > 1:
        res += ncr(cnt,2)
print(res)

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):
    for n2 in range(n + 1, N):
        if (S[n] == S[n2]): cnt += 1

print (cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
count = 0
for i in range(n):
    s1 = list(input())
    s1.sort()
    "".join(s1)
    count = count + s.count(s1)
    s.append(s1)
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[]
for i in range(n):
    s=input()
    s_list=list(s)
    s_list.sort()
    s_str=str(s_list)
    l.append(s_str)

#print(l)
cnt=0
list_set=set(l)
for i in range(n):
    if l.count(l[i])>1:
        cnt=cnt+l.count(l[i])-1
    l[i]=000
    #print(i,cnt)

print(cnt)

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] = 1
    else:
        cnt[s] += 1

ans = 0
for s in S:
    c = cnt[s]
    for i in range(c):
        ans += i
print(ans)

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-1):
    for j in range(i+1, n):
        if aL[i] == aL[j]:
            count +=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

wordList = []
checkList = []

for i in range(N):
    wordList.append({})
    checkList.append([])
    word = list(input())
    for a in word:
        if not(a in wordList[-1]):
            wordList[-1][a] = 1
        else:
            wordList[-1][a] += 1
    count = 0
    for k in wordList[:-1]:
        check = 0
        for w in wordList[-1].keys():
            if not(w in k):
                check = 1
                break
            else:
                if k[w]!=wordList[-1][w]:
                    check = 1
                    break
        if check==0:
            checkList[count].append(1)
            wordList.pop()
            checkList.pop()
            break
        count += 1
        
sum = 0
for i in checkList:
    if len(i)>0:
       sum +=  len(i)*(len(i)+1)/2
print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
para = [[c for c in input()] for i in range(N)]
for i in range(N):
  para[i].sort()

def check(a,b):

    if a == b :
        return True 
            
a = 0 
for i in range(N):
    for j in range(i+1,N):
        if check(para[i],para[j]):
            a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
abc = [chr(i) for i in range(97, 97+26)]
ss = []

for i in range(N):
    s = input()
    cnt = []
    for j in abc:
        num = s.count(j)
        cnt.append(num)
    ss.append(cnt)

ans = 0

for i in ss:
    k = ss.count(i)
    ans += k-1

print(int(ans/2))

Submission status: Time Limit Exceeded

Solution code:
def check(line):
    moji = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    for c in line:
        moji[ord(c)-97] += 1
    return moji


N = int(input())
s = [input() for i in range(N)]
res = []
count = 0

for x in s:
    res.append(check(x))

for k, x in enumerate(res):
    for j in range(k+1, N):
        if x == res[j]:
            count += 1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [input() for i in range(n)]
r = 0
    
for i in range(n):
    for j in range(i+1, n):
        anagram = True
        
        checked = []
        for k in range(10):
            t = l[i][k]
            if (t in checked): continue
            if not (l[i].count(t) == l[j].count(t)):
                anagram = False
                break
            checked.append(t)
        if anagram:
            r += 1

print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as counter

N = int(input())
char_counts = list(map(counter, [input() for i in range(N)]))
counts = [list(map(lambda x: x == c, char_counts[i+1:])) for i, c in enumerate(char_counts)]
match_count = sum(list(map(sum, counts)))
print(match_count)

Submission status: Time Limit Exceeded

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


N = int(input())
s = [input() for _ in range(N)]
ans = 0
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:
N = int(input())
# S = [sorted(input()) for i in range(N)]

S = []

Scount = 0
count = 0

for i in range(N):
    if i == 0:
        S.append(sorted(input()))
        Scount += 1
    else:
        tmp = sorted(input())
        # print(tmp)
        for k in range(Scount):
            if tmp == S[k]:
                count +=1

        S.append(tmp)
        Scount += 1


print(count)

# count = 0
#
# for g in range(N):
#     for k in range(N):
#         if g == k:
#             continue
#         if S[g] == S[k]:
#             print("g {} k {}".format(g,k))
#             count+=1




# print(int(count/2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

count = 0
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+1, n):
        if table[i] == table[j]:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
sl=[]
count=0
for i in range(N):
  sl.append(input())
dl=[]
for i in range(N):
  lx=list(sl[i])
  dicx={}
  for j in range(10):
    countix=lx.count(lx[j])
    dicx[lx[j]]=countix
  dl.append(dicx)
ci=0
for i in range(N):
  ci=ci+dl.count(dl[i])-1
print(int(ci/2))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A= [input() for i in range(N)]
ans = 0
for i in range(N):
    j = i+1
    a = collections.Counter(A[i])
    while j < N:
        b = collections.Counter(A[j])
        if a==b:
            ans +=1
        j+=1
print(ans)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

# 長さが10で英小文字の文字列リスト
str_list = [input() for i in range(N)]

import itertools as iter
comb_list = list(iter.combinations(str_list, 2))

# ↓みたいな辞書を作れば良い
# {s1: {"a": 2, "b": 4}}

def to_cache(cache_dict, word):
    if word in cache_dict.keys():
        return cache_dict

    cnt_dict = {}
    for s in list(word):
        if s not in cnt_dict.keys():
            cnt_dict[s] = 0
        cnt_dict[s] += 1
    cache_dict[word] = sorted(cnt_dict.items(), key=lambda x:x[0])


cache_dict = {}
cnt = 0
for comb_str in comb_list:
    w1, w2 = comb_str[0], comb_str[1]

    if w1 not in cache_dict.keys():
        cnt_dict = {}
        for s in list(w1):
            if s not in cnt_dict.keys():
                cnt_dict[s] = 0
            cnt_dict[s] += 1
        cache_dict[w1] = sorted(cnt_dict.items(), key=lambda x:x[0])

    if w2 not in cache_dict.keys():
        cnt_dict = {}
        for s in list(w2):
            if s not in cnt_dict.keys():
                cnt_dict[s] = 0
            cnt_dict[s] += 1
        cache_dict[w2] = sorted(cnt_dict.items(), key=lambda x:x[0])

    if cache_dict[w1] == cache_dict[w2]:
        cnt += 1


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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)
ans=[]
N=int(input())
kotae=[0]*N
for n in range(N):
    a=input()
    if n==0:
        ans.append(a)
        kotae[0]=1
    else:
        u=0
        for i in range(len(ans)):
            if verify_anagrams(a,ans[i])==True:
                kotae[i]+=1
                u=1
                break
        if u==0:
            kotae[len(ans)]=1
            ans.append(a)
c=0
for n in range(len(kotae)):
    c=c+kotae[n]*(kotae[n]-1)/2
print(round(c))

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 =[]
for i in range(N):
  S.append(Counter(s[i].rstrip('\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())

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 in range(N):
    for j in range(i+1, N):
        if sorted_str_arr[i] == sorted_str_arr[j]:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def str_order(str):
    temp_ls = []
    for _s in str:
        temp_ls.append(_s)

    temp_ls.sort()

    temp_val = ""
    for _st in temp_ls:
        temp_val += _st

    return(temp_val)

def pair_cal(int):
    cnt = 0
    for _i in range(1, int):
        cnt += _i

    return cnt

N = int(input())

str_ls = []
for _n in range(0, N):
    str_ls.append(str_order(str(input())))

uniq_ls = list(set(str_ls))
# print(uniq_ls)

str_ls.sort()
# print(str_ls)

total_cnt = 0

for _u in uniq_ls:
    count_num = str_ls.count(_u)
    if count_num != 1:
        total_cnt += pair_cal(count_num)

print(total_cnt)

Submission status: Time Limit Exceeded

Solution code:


import math
import itertools


def str_sort(s):
    return ''.join(sorted(s))
    
def combinations_count(n, r):
    if n == 1: return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def is_anagram(s_1, s_2, d):
    if d[s_1] != d[s_2]: return False
        
    for c in set(s_1):
        char_count_1 = s_1.count(c)
        char_count_2 = s_2.count(c)
        if char_count_1 != char_count_2:
            return False

    return True

def make_char_num_dict(S):
    d={}
    for s in S:
        d[s] = len(set(s))
    return d


N = int(input()) 
S = [str(input()) for i in range(N)] 


d = make_char_num_dict(S)

s_list = list(itertools.combinations(S, 2))



ans = 0
for s_l in s_list:
    s_1 = s_l[0]
    s_2 = s_l[1]

    if is_anagram(s_1, s_2, d) == True:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [''.join(sorted(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
'''
ans = 0
for i in range(len(S)):
  s1 = S[i]
  for j in range(i+1,len(S)):
    s2 = S[j]
    if s1 == s2:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def f(si, sj):
    for i in range(10):
        if si.count(sj[i]) != sj.count(sj[i]):
            return 0
    return 1


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:
import sys
import copy
import math

def dictCount(str):
    dict = {}


if __name__ == '__main__':
    n = int(input())
    dict_list = []
    for i in range(n):
        dict = {}
        s = input()
        for i in s:
            if i in dict:
                dict[i] = dict[i] + 1
            else:
                dict[i] = 1
        dict_list.append(dict)

    count = 0
    check = [0]*n
    for i in range(n):
        tmp=0
        if check[i] ==0:
            for j in range(i,n):
                if j > i and check[j] ==0:
                    if dict_list[i] == dict_list[j]:
                        check[j]=1
                        tmp=tmp+1
            if tmp >0:
                count = count + (1+tmp)*tmp/2
    print(int(count))

Submission status: Time Limit Exceeded

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

li2 = []
for i in range(len(li)):
    li2.append(li.count(li[i]))

li3 = list(set(li2))

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
    
num = 0   
for i in range(len(li3)):
    if li3[i] >= 2:
        num += cmb(li3[i],2)

print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for i in range(n)]
s = list(map(sorted,s))
a = len(s)  #sの要素の個数
sl =set(list("".join(i) for i in s))
ss = list("".join(i) for i in s)
b = len(sl)
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:
def encode(s):
#  r = [0]*26
#  for n in range(len(s)):
#    r[ord(s[n]) - ord('a')] += 1
#  return r
  r = list(s)
  r.sort()
  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:
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-1):
  for j in range(i+1, n):
    if is_anagrams(s[i], s[j]):
      cnt += 1
      continue
print(cnt)

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
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

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 += cmb(count, 2)
        tmp.append(e)
print(cnt)

Submission status: Time Limit Exceeded

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

def anagram(sa,sb) :
  flag=True
  sal=list(sa)
  sbl=list(sb)
  sal.sort()
  sbl.sort()
  if sal==sbl:
    return True
  else :
    return False
  """
  for i in range(10) :
    if sal[i] in sbl :
      sbl.remove(sal[i])
    else :
      flag=False
      break
  """
  #return flag
   
#print(anagram("peanutbomb","constraint"))    
count=0
for i in range(n) :
  for j in range(n) :
    if i>= j :
      continue
    if anagram(strl[i],strl[j]) ==True :
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

i=0
sortedSet=[]

while i<N:
  temp=input()
  tempSet={}
  j=0
  while j<len(temp):
    if temp[j] in tempSet:
      tempSet[temp[j]]=tempSet[temp[j]]+1
    else:
      tempSet[temp[j]]=1
    j=j+1
  #print(tempSet)
  sortedSet.append(tempSet)
  i=i+1

result=0
i=0
while i<N:
  j=i+1
  while j<N:
    if sortedSet[i]==sortedSet[j]:
      result=result+1
      #print(i,j)
    j=j+1
  i=i+1

print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
l=[]
for x in range(0,N):
    l.append(input())
A=[[0 for i in range(10)] for j in range(N)]
B=[[0 for i in range(10)] for j in range(N)]
for i in range(0,N):
    for j in range(0,10):
        if l[i][j]=='a':
            A[i][j]=0
        elif l[i][j]=='b':
            A[i][j]=1
        elif l[i][j]=='c':
            A[i][j]=2
        elif l[i][j]=='d':
            A[i][j]=3
        elif l[i][j]=='e':
            A[i][j]=4
        elif l[i][j]=='f':
            A[i][j]=5
        elif l[i][j]=='g':
            A[i][j]=6
        elif l[i][j]=='h':
            A[i][j]=7
        elif l[i][j]=='i':
            A[i][j]=8
        elif l[i][j]=='j':
            A[i][j]=9
        elif l[i][j]=='k':
            A[i][j]=10
        elif l[i][j]=='l':
            A[i][j]=11
        elif l[i][j]=='m':
            A[i][j]=12
        elif l[i][j]=='n':
            A[i][j]=13
        elif l[i][j]=='o':
            A[i][j]=14
        elif l[i][j]=='p':
            A[i][j]=15
        elif l[i][j]=='q':
            A[i][j]=16
        elif l[i][j]=='r':
            A[i][j]=17
        elif l[i][j]=='s':
            A[i][j]=18
        elif l[i][j]=='t':
            A[i][j]=19
        elif l[i][j]=='u':
            A[i][j]=20
        elif l[i][j]=='v':
            A[i][j]=21
        elif l[i][j]=='w':
            A[i][j]=22
        elif l[i][j]=='x':
            A[i][j]=23
        elif l[i][j]=='y':
            A[i][j]=24
        else:
            A[i][j]=25
    B[i]=sorted(A[i])
ans=0
for i in range(0,N):
    for j in range(i+1,N):
        if B[i]==B[j]:
            ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0
T = []
ans = [1]*N
for i in range(N):
    if S[i] in T:
        p = T.index(S[i])
        ans[p] += 1
    else:
        T.append(S[i])

pp = 0
for i in ans:
    pp += (i-1)*i//2

print(pp)

Submission status: Time Limit Exceeded

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

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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input()) 
t=[[0]*10]*N
y=[[0]*10]*N

for i in range(N):
  s=input()
  for j in range(10):
    t[i][j]=s[j]
  y[i]=sorted(t[i])
  
count=0

for i in range(0,N-1):
  for j in range(i+1,N):
    #print(i,t[i])
    #print(j,t[1])

    if y[i]==y[j]:
        count+=1

print(count)