Problem:


Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
3
acornistnt
peanutbomb
constraint


Sample output:
1

Submission status: Accepted

Solution code:
def factorial(n):
    a = 1
    for i in range(1, n + 1):
        a *= i
    return a

def nCr(n,r):
    if r > n:
        return 0
    return factorial(n) // factorial(r) // factorial(n - r)

def nC2(n):
    if n < 2:
        return 0
    else:
        return n * (n-1) // 2


####
N = int(input())
sorted_dic = {}
for i in range(N):
    s = "".join(sorted(input()))
    v = 0
    if s in sorted_dic:
        v = sorted_dic[s]
    sorted_dic[s] = v + 1

ans = 0
for k in sorted_dic:
    v = sorted_dic[k]
    #print(k,v,factorial(v),factorial(2), factorial(v - 2), nCr(v, 2))
    ans = ans + nC2(v)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

sList = {}

for i in range(N) :
    s = list(input())
    s.sort()
    s = ''.join(s)

    if s in sList :
        sList[s] += 1
    else :
        sList[s] = 1

ans = 0
for _, num in sList.items() :
    ans += num * (num - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = []
cnt = 0
c = {}

for i in range(N):
    s = "".join(sorted(list(input())))
    if s not in c.keys():
        c[s] = 1
    else:
        c[s] += 1
        cnt += c[s]-1
        
    S.append(s)

print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
data = []
N = int(stdin.readline().rstrip())
for _ in range(N):
    data.append(tuple(sorted(input())))


ans = dict()

for i in data:
    if i in ans:
        ans[i] += 1
    else:
        ans[i] = 1

from scipy.misc import comb
res = 0
for j in ans.values():
    res += comb(j, 2, exact=True)
print(res)

Submission status: Accepted

Solution code:
import collections
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


n=int(input())

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

c=collections.Counter(S)
ans=0
for i in c.values():
    if i==2:
        ans+=1
    elif i>2:
        ans+=cmb(i,2)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

n=int(input())
l=[['0']*10 for _ in range(n)]
for i in range(n):
        a=str(input())
        for j in range(10):
                l[i][j]=a[j]
        l[i].sort()
l.sort()

c=0
d=[]
for j in range(n-1):
        if l[j]==l[j+1] and j==n-2:
                c+=2
                d.append(c)
        elif l[j]==l[j+1]:
                c+=1
        else:
                c+=1
                d.append(c)
                c=0
aa=0
for k in range(len(d)):
        aa+=d[k]*(d[k]-1)//2
print(aa)

Submission status: Accepted

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

import sys
from operator import mul
from functools import reduce

input = sys.stdin.readline

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

def comb(a, b):
    n = a
    for _ in range(b):
        n -= 1
        a *= n

    for i in range(1, b):
        b *= i
    return int(a / b)


def main():
    n = int(input())
    m = {}
    for l in sys.stdin:
        s = ''.join(sorted(l.strip()))
        if s in m:
            m[s] += 1
        else:
            m[s] = 1
    cnt = 0
    for k, v in m.items():
        if v == 1:
            continue
        if v == 2:
            cnt += 1
        else:
            cnt += cmb(v, 2)
    print(cnt)

main()

Submission status: Accepted

Solution code:
def int_multiple():
    return  [int(c) for c in input().split()]

def int_single():
    return int(input())

def str_multiple():
    return [c for c in input().split()]

def str_single():
    return input()

# start
N = int_single()


hash = []

d = {}

primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101]

for i in range(N):
    tmp = str_single()
    checksum = 1
    for ch in tmp:
        checksum *= primes[ord(ch)-97]

    if checksum in d:
        d[checksum] += 1
    else:
        d[checksum] = 1

res = 0
for key in d:
    if d[key] >= 2:
        res += int((d[key] * (d[key] - 1)) / 2)

print(res)

Submission status: Accepted

Solution code:
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0:
        return 1
    over = reduce(lambda x, y: x * y, range(n, n - r, -1))
    under = reduce(lambda x, y: x * y, range(1, r + 1))
    return over // under

N = int(input())

strings = {}
for i in range(N):
    key = ''.join(sorted(list(input())))
    strings.setdefault(key, 0)
    strings[key] += 1
ans = 0

for k, v in strings.items():
    if v > 1:
        ans += cmb(v, 2)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for _ in range(n):
    s.append(input())
for i in range(n):
    s[i]=sorted(s[i])
s.sort()
num=0
last=1
counter=[]
while num<n-1:
    if s[num]!=s[num+1]:
        num+=1
        counter.append(last)
        last=1
    elif s[num]==s[num+1]:
        num+=1
        last+=1
if last>1: counter.append(last)

b=[i for i in counter if i>1]
ans=0
for v in b:
    ans+=int(v*(v-1)/2)
print(ans)

Submission status: Accepted

Solution code:
import collections
from operator import mul
from functools import reduce

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

c = collections.Counter(words)
nums = list(c.values())

def cmb(n,r):
    if n == 1: return 0
    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

print(sum([cmb(i,2) for i in nums]))

Submission status: Accepted

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

dic = {}
count_dic = {}
n = int(input())

for i in range(n):
    x = input().rstrip('\n')
    text = ''.join(sorted(list(x)))
    if text not in dic:
        dic[text] = 0
        count_dic[text] = 1
    else:
        dic[text] += count_dic[text]
        count_dic[text] += 1

ans = sum(list(dic.values()))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
d=defaultdict(int)
ans=0
for _ in range(n):
    d["".join(sorted(input()))]+=1
# print(d)
for key,i in d.items():
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

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

cnt = 0

for i, item in enumerate(s):
    s[i] = ''.join(sorted(s[i]))
s = sorted(s)


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


cnt2 = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        if cnt2 == 0:
            cnt2 += 2
        else:
            cnt2 += 1
    else:
        if cnt2 != 0:
            f = combinations_count(cnt2, 2)
            cnt += f
            cnt2 = 0

if cnt2 != 0:
    f = combinations_count(cnt2, 2)
    cnt += f
    cnt2 = 0
print(cnt)

Submission status: Accepted

Solution code:
import collections
import math
def cmb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
count = 0
l_s =[]
for i in range(N):
    s = input()
    l = list(s)
    l.sort()
    s = ''.join(l)
    l_s.append(s)
d_s = collections.Counter(l_s)
for i in d_s.values():
        count+=cmb(i,2)
print(count)

Submission status: Accepted

Solution code:

n = int(input())

# c_hash = {}
c_list = []
# n = 3
for i in range(n):
    s = input()
    c_list.append(''.join(sorted(s)))

# print(sorted(c_list))

c_hash = {}
for c in c_list:
    if c not in c_hash:
        c_hash[c] = 0
    c_hash[c] += 1
count = 0    
for k, v in c_hash.items():
    if v >= 2:

        
        # temp = factorial(v) / factorial(2) / factorial(v - 2)
        # temp = len(list(combinations(range(v),2)))
        temp = v * (v-1)/ 2
        count += temp

print(int(count))

Submission status: Accepted

Solution code:
characters = ['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']
numbers = []
for i in range(26):
    numbers.append(1*10**(i))

def calc(str):
    x = 0
    for i in str:
        ind = characters.index(i)
        x += numbers[ind]
    return x

n = int(input())
s = [input() for i in range(n)]
d = {}
for i in s:
    num = str(calc(i))
    if num in d:
        d[num] += 1
    else:
        d[num] = 1

ans = 0
for i in d:
    #print(i,d[i])
    if d[i] >= 2:
        ans +=  d[i] * (d[i]-1) / 2

print(int(ans))

Submission status: Accepted

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

s.sort()

ans = 0
pre_same = 1
for i in range(1,N):
#    print("a,s[i]:" + s[i]+",pre_same:" + str(pre_same) + ",ans:" + str(ans))
    if(s[i-1] == s[i]):
        pre_same = pre_same +1
    else:
        if(pre_same > 1):
            ans = ans + (pre_same * (pre_same-1) / 2)
        pre_same = 1
#    print("b,s[i]:" + s[i]+",pre_same:" + str(pre_same) + ",ans:" + str(ans))

if(pre_same > 1):
    ans = ans + (pre_same * (pre_same-1) / 2)

print(int(ans))

Submission status: Accepted

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

ans = []
tmp = 1
for idx,val in enumerate(s):
        if idx == 0:
                continue
        if val == s[idx - 1]:
                tmp += 1
        else:
                ans.append(tmp)
                tmp = 1
else:
        ans.append(tmp)

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

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

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

ans = 0
for v in Counter(S).values():
    if v!=1: ans += cc(v,2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(''.join(sorted(input())))
s.sort()
count = 0
ans = 0
for i in range(1,N):
    if(s[i]==s[i-1]):
        count  = count+1
        ans = ans+count
    else:
        count = 0

print(ans)

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

N = int(input())						# 5

from collections import defaultdict
D = defaultdict(int)

for i in range(N):
	s = input()
	l = sorted(s)
	s = "".join(l)
	D[s]+=1

p("D ",D)


ans=0
for k in D:
	i = D[k]
	ans += i*(i-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
c = Counter(tuple(sorted(input())) for _ in range(int(input())))
print(sum(x * (x - 1) // 2 for x in c.values()))

Submission status: Accepted

Solution code:
import sys
import collections


def input():
    return sys.stdin.readline()[:-1]


def main():
    n = int(input())
    s = ["".join(sorted(list(input()))) for _ in range(n)]
    c = collections.Counter(s)
    ans = 0
    for k in c.values():
        ans += k*(k-1)//2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
def com(n,r):
    if n<r:
        return 0
    return int(factorial(n) // factorial(r) // factorial(n-r))

N = int(input())
S = [[0 for j in range(10)] for i in range(N)]
for i in range(N):
    s = str(input())
    for j in range(10):
        S[i][j] = s[j]
    S[i].sort()
S.sort()
ans = 0
i = 0
j = 1
while i < N:
    tmp = 0
    while i + j < N and S[i] == S[i + j]:
        tmp += 1
        j += 1
    if tmp != 0:
        ans += com(tmp+1, 2)
    i += j
    j = 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for _ in range(n):
    s1=sorted(input())
    s1=''.join(s1)
    s.append(s1)
s.sort()
cnt=[1]*len(set(s))
j=0
for i in range(n-1):
    if s[i]==s[i+1]:
        cnt[j]+=1
    else:
        cnt[j]=cnt[j]*(cnt[j]-1)//2
        j+=1
cnt[-1]=cnt[-1]*(cnt[-1]-1)//2
print(sum(cnt))

Submission status: Accepted

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

dic = {}

for _ in range(n) :
  s = "".join(sorted(input()))
  if (s in dic):
    dic[s] += 1
  else:
    dic[s] = 0
  
res = 0
for v in dic.values():
  if v > 0:
    res += v*(1+v)/2

print(int(res))

Submission status: Accepted

Solution code:
import math

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

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

ans_d = {}
ans = 0
for _ in range(N):
    sl.append(input())


for s in sl:
    key = "".join(sorted(s))
    if(key not in ans_d.keys()):
        ans_d[key] = 1
    else:
        ans_d[key] += 1


print(sum([combinations_count(ans,2) for ans in ans_d.values() if ans>1]))

Submission status: Accepted

Solution code:
N = int(input())

sList =[0] * N

for i in range(N):
    sList[i] = ''.join(sorted(list(input())))

sList = sorted(sList)

ans = 0
tmp = 1

for i in range(N-1):
    if sList[i] == sList[i+1]:
        tmp += 1
    else:
        ans += (tmp * (tmp - 1)) // 2
        tmp = 1
if sList[N-2] == sList[N-1]:
    ans += (tmp * (tmp - 1)) // 2
            
print(ans)

Submission status: Accepted

Solution code:
#C
N = int(input())
pairdic = {}
s = ''.join(sorted(input()))
pairdic[s] = 1
for i in range(1, N):
    s = ''.join(sorted(input()))
    if s in pairdic:
        pairdic[s] += 1        
    else:
        pairdic[s] = 1
result = 0
for i in pairdic:
    result += (pairdic[i] * (pairdic[i]-1))//2
print(result)

Submission status: Accepted

Solution code:
import collections
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
n=int(input())
s=list(list(input()) for i in range(n))
count=0
for i in range(n):
    s[i].sort()
s.sort()

for i in range(n):
    s[i]=''.join(map(str, s[i]))
    
c = collections.Counter(s)
d=c.most_common()

for i in range(len(d)):
    if d[i][1]>1:
        count+=combinations_count(d[i][1], 2)
    else:
        break
print(count)

Submission status: Accepted

Solution code:
import collections
N = int(input())
s_list = []
for i in range(N):
  s_list.append(''.join(sorted(input())))
s_list = collections.Counter(s_list)
def f(n):
  return int(n*(n+1)/2)
result = sum([f(n-1) for n in s_list.values()])
print(result)

Submission status: Accepted

Solution code:
import collections

N = int(input())
d = {}
for i in range(N):
    s=input()
    c = "".join(sorted(s))
    if c not in d.keys():
        d[c] = 1
    else:
        d[c]+=1

answer = 0
for k in d.keys():
    if d[k]>1:
        answer += d[k]*(d[k]-1)/2

print(int(answer))

Submission status: Accepted

Solution code:
import string


n = int(input())

Ss = list()
for i in range(n):
    Ss.append(input())

# import random
# n = 100000
# Ss = list()
# for i in range(n):
#     Ss.append(random.sample(string.ascii_lowercase, 10))

count_dict = dict()
for s in Ss:
    count = [0 for i in range(len(string.ascii_lowercase))]
    for j in range(len(s)):
        count[ord(s[j]) - ord('a')] += 1
    count = tuple(count)
    if count in count_dict:
        count_dict[count] += 1
    else:
        count_dict[count] = 1
# print(count_dict)
ans = 0
for value in count_dict.values():
    ans += value * (value - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for x in range(N):
    A.append([y for y in input()])
    A[-1].sort()
A.sort()
count = 0
l = 1
r = 0
for i in range(1,N):
    if A[r] == A[i]:
        l += 1
    else:
        count += l * (l - 1) / 2
        l = 1
        r = i
count += l * (l - 1) / 2
print(int(count))

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = []
dic = collections.defaultdict(int)
for sent in range(n):
  sent_s = "".join(sorted(input()))
  dic[sent_s] += 1

ans = 0
for key,val in dic.items():
  ans += (val*(val-1))/2
print(int(ans))

Submission status: Accepted

Solution code:
moji = ['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']

ans = 0
n = int(input())
s1 = ['' for _ in range(n)]
for j in range(n):
    s = input()
    a = ''
    for i in  moji:
        a += i + str(s.count(i))
    s1[j] = a
s1.sort()
cnts = []
before = s1[0]
cnt = 0
for S in s1:
    if before == S:
        cnt += 1
    else:
        cnts.append(cnt)
        before = S
        cnt = 1
cnts.append(cnt)
for i in cnts:
    if i == 1:
        continue
    ans += i * (i - 1) / 2
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())

s = {}
ans = 0
for i in range(n):
  _s = sorted(input())
  __s = ''
  for _ in _s:
    __s += _
  try:
    ans += s[__s]
    s[__s] += 1
  except:
    s[__s] = 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

#from collections import Counter
dic = {}
for i in range(N):
    s = list(input())
    s_uniq = list(set(s))
    s_uniq.sort()
    key = ""
    for j in range(len(s_uniq)):
        s_cnt = s.count(s_uniq[j])
        key += s_uniq[j]+str(s_cnt)
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

ans = 0
for v in dic.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
# N = 3
N=int(input())
# N = 10**5

alphas =list("abcdefghijklmnopqrstuvwxyz")
d = {}
for i,alpha in enumerate(alphas):
    d[alpha] = i

# print(d)

a = []
for n in range(N):
    s = input()
    # s = "acornistnt"
    # s = "aaaaaaaaaa"
    sl = [0 for i in range(26)]
    for ss in s:
        sl[d[ss]] += 1
    a.append(tuple(sl))

des = {}
for aa in a:
    if aa not in des:
        des[aa] = 0
    des[aa] += 1

# print(des)

# fact_cache = {}


cnt = 0
for k in des:
    ddd = des[k]
    cnt += (ddd*(ddd-1))//2
print(cnt)

Submission status: Accepted

Solution code:
import collections

def cmb(n):
    sum = 0
    for i in range(n):
        sum += i
    return(sum)


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

ss_num = collections.Counter()

for s in S:
    ss_num[tuple(sorted(s))] += 1

ans = 0
for s,n in ss_num.items():
    ans += cmb(n)
print(ans)

Submission status: Accepted

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

sscounts = Counter(ss)

ans = 0
for s, n in sscounts.items():
    if n > 1:
        ans += n * (n - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for i in range(n):
    s = tuple(sorted(input()))
    if s in d:
        d[s] += 1
    else:
        d[s] = 0

c = 0
for i in d.values():
    c += (i+1)*i//2
         
print(c)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7


# max_n = 100000
# max_k = 2

# factorial = [1,]  # 0! = 1
# tmp_fac = 1
# for i in range(1,max(max_n+1, max_k+1)):
#     tmp_fac *= i
#     factorial.append(tmp_fac)  # O(1)

from scipy.misc import comb  # must write exact=True to use

# def combinations(n, k):
#     return factorial[n] // factorial[k] // factorial[n-k]

def main(): 
    N = II()
    S = []
    for _ in range(N):
        s = tuple(sorted(list(SI())))
        S.append(s)

    S.sort()

    prev = None
    ans = 0
    same_count = 0
    for i in S:
        if prev == i:
            same_count += 1
            # prev = i
        else:
            # ans += combinations(same_count, 2)
            ans += comb(same_count, 2, exact=True)
            same_count = 1
            prev = i
    ans += comb(same_count, 2, exact=True)
    # ans += combinations(same_count, 2)


    print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())

d = dict()

for i in range(n):
  s = "".join(sorted(list(input())))
  d[s] = d.get(s, 0) + 1

count = 0

for i in d.items():
  count += (i[1] * (i[1] - 1)) // 2

print(count)

Submission status: Accepted

Solution code:
import collections
import math

n = int(input())
str_list = []
for i in range(n):
    str_list.append(''.join(sorted(input())))

ans = 0
for i in collections.Counter(str_list).values():
    if i >=2 :
        ans += math.factorial(i)//math.factorial(i-2)//math.factorial(2)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
c = {}
for _ in range(N):
    s = ''.join(sorted(list(input())))
    if s in c.keys():
        c[s] += 1
    else:
        c[s] = 0
ans = 0
for k in c.values():
    ans += (k * (k + 1)) // 2
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

N = int(input())
#k,x = map(int,input().split())

ans=0
S=[]
for i in range(N):
    s=list(input())
    s.sort()
    S.append(''.join(x for x in s))

S.sort()

for key, value in groupby(S):
    l = len(list(value))
    ans += (l*(l-1) // 2)


print(str(ans))

Submission status: Accepted

Solution code:
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  # setrlimit works on ubuntu (and atcoder), but not on WSL
  #resource.setrlimit(RLIMIT_STACK, (128*1024*1024,hard))

n = inn()
alpha = [ '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' ]
h = {}
for i in range(n):
    ch = {}
    for c in alpha:
        ch[c] = 0
    s = input().strip()
    for j in range(10):
        ch[s[j]] += 1
    x = (ch['a'], ch['b'], ch['c'], ch['d'], ch['e'], ch['f'], ch['g'], ch['h'], ch['i'], ch['j'], ch['k'], ch['l'], ch['m'], ch['n'], ch['o'], ch['p'], ch['q'], ch['r'], ch['s'], ch['t'], ch['u'], ch['v'], ch['w'], ch['x'], ch['y'], ch['z'])
    if x in h:
        h[x] += 1
    else:
        h[x] = 1

sum = 0
for x in h:
    y = h[x]
    if y>1:
        sum += (y*(y-1))//2
print(sum)

Submission status: Accepted

Solution code:
# import itertools

N = int(input())
S = []
for i in range(N):
    S.append(input())
# print(N, S)

for i in range(len(S)):
    s = S[i]
    t = list(s)
    t.sort()
    S[i] = ''.join(t)

S.sort()
# print(S)

ans = 0
i = 0
count = 0
while i < N - 1:
    si = S[i]
    sj = S[i + 1]
    if si != sj:
        # print(si, i, count)
        ans += count * (count + 1) // 2
        count = 0
    else:
        count += 1
        if i == N - 2:
            ans += count * (count + 1) // 2
    i += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
ss = [input() for i in range(N)]
sdic = defaultdict(int)
ans = 0
for s in ss:
  dic = defaultdict(int)
  for c in s:
    dic[c] += 1
  key = tuple(sorted(dic.items()))
  sdic[key] += 1
  if sdic[key] >= 2:
    ans += sdic[key] - 1
print(ans)

Submission status: Accepted

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

# print(S)

import collections

c = collections.Counter(S)

# print(c)

ary = c.values()

import math

def combinations_count(n, r = 2):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


for i in ary:
    if i != 1:
        cnt += combinations_count(i)


print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

s = []
for _ in range(n):
    s += [''.join(map(str, (sorted(list(str(input()))))))]

import collections

c = collections.Counter(s)

from math import factorial

ans = 0

for x in c.values():
    if x >= 2:
        ans += factorial(x)//(factorial(x-2)*2)

print(ans)

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

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

c = Counter(s)
l = len(c.most_common())
a = c.most_common()

cnt = 0
for i in range(l):
    if a[i][1] < 2:
        continue
    cnt += combinations_count(a[i][1], 2)

print(cnt)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(S)
ans = 0
for count in c.values():
    ans += count * (count-1) // 2

print(ans)

Submission status: Accepted

Solution code:
def npr(n,r):
    if n == r:return 1
    ans1 = 1
    ans2 = 1
    for i in range(1,r+1):
        #print((n-r+i),i)
        ans1 *= i
        ans2 *= (n-r+i)
    return ans2//ans1

N = int(input())
dics = {}
sl = []
for _ in range(N):
    s = input()
    sl.append(''.join(sorted(s)))
for c in sl:dics[c] = dics.get(c,0)+1
g = 0
for k in dics.keys():
    if dics[k] >=2:
        g += npr(dics[k],2)
print(g)

Submission status: Accepted

Solution code:
import sys
from math import factorial

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    string_type = {}
    for s in inputs:
        count = {}
        s_list = list(s)
        s_list.sort()
        for c in s_list:
            if c in count:
                count[c] += 1
            else:
                count[c] = 1
        key = ""
        for k in count.keys():
            key += "{}{}".format(k, count[k])
        if key in string_type:
            string_type[key] += 1
        else:
            string_type[key] = 1

    comb_count = 0
    for v in string_type.values():
        if v != 1:
            if v == 2:
                comb_count += 1
            else:
                comb_count += factorial(v) // factorial(v-2) // factorial(2)
    return comb_count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Submission status: Accepted

Solution code:

N = int(input())

data = {}
for n in range(N):
	s0 = input()
	s = ''.join(sorted(s0))
	if s in data:
		data[s] += 1
	else:
		data[s] = 1

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

Submission status: Accepted

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

counter = {}
for s in S:
    if s in counter:
        counter[s] += 1
    else:
        counter[s] = 0

    
ans = 0
for v in counter.values():
    for i in range(1, v + 1):
        ans += i
print(ans)

Submission status: Accepted

Solution code:

n = int(input())
lines = [input() for x in range(n)]

order_lines = dict()
sum = 0
for i,line in enumerate(lines):
    sort_characters = "".join(sorted(line))
    if sort_characters not in order_lines:
        order_lines[sort_characters] = []
    else:
        sum += len(order_lines[sort_characters])
    order_lines[sort_characters].append(i)
print(sum)

Submission status: Accepted

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

id = 1
now = s[0]
count = 0
ans = 0
while True:
    if id == N:
        if count != 0:
            ans += count*(count+1)//2
        break
    if s[id] == now:
        id += 1
        count += 1
        continue
    else:
        ans += count*(count+1)//2
        count = 0
        now = s[id]
        id += 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)
sum = 0
for i in c.values():
    sum += i * (i-1) // 2

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
str_list = {}
for i in range(N):
  str_dict = {}
  for j in input():
    if j in str_dict:
      str_dict[j] += 1
    else:
      str_dict[j] = 1
  key = "".join([str(k)+str(v) for k, v in sorted(str_dict.items(), key=lambda x:x[0])])
  if key in str_list:
    str_list[key] += 1
  else:
    str_list[key] = 1
  
result = sum([i*(i-1)//2 for i in str_list.values()])
print(result)

Submission status: Accepted

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

res = 0
cnt = 0
tmp = ''
for word in s:
    if word != tmp:
        tmp = word
        res += (cnt * (cnt-1)) // 2
        cnt = 1
    else:
        cnt += 1
else:
    res += (cnt * (cnt-1)) // 2

print(res)

Submission status: Accepted

Solution code:
import collections
from operator import mul
from functools import reduce

def comb(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

N = int(input())
S = [input() for i in range(N)]
# print(S)
S_new = []
for s in S:
    S_new.append(''.join(sorted(s)))
# print(S_new)
c = collections.Counter(S_new)
# print(c)
cnt = 0
for num in c.values():
    if num > 1:
        if num == 2:
            cnt += 1
        else:
            cnt += comb(num, 2)# a = comb(n, r)
print(cnt)

Submission status: Accepted

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

S_c = collections.Counter(S)
S_cl = list(S_c.values())

ans = 0
for c in S_cl:
    ans += c*(c-1)//2

print(ans)