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:
#coding:utf-8
from collections import Counter
N = int(input())
ls = []
for n in range(N):
    ls.append("".join(sorted(input())))
#print (ls)
cnt = Counter(ls)
#print (list(cnt.values()))
ans = [i*(i-1)/2 for i in list(cnt.values())]
print (int(sum(ans)))

Submission status: Accepted

Solution code:
N = int(input())
S = {}

cnt = 0
for n in range(N):
    s = ''.join(sorted(input()))

    if s in S.keys():
        cnt += S[s]
        S[s] += 1
    else:
        S[s] = 1

print(cnt)

Submission status: Accepted

Solution code:
import collections
#n = (int(i) for i in input().split())  
n=int(input())
prime = [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]
num=[1] * n
count = 0
for i in range(0,n):
    S = input()
    for s in S:
        num[i] = num[i] * prime[ord(s)-97]
#    for j in range(0,i):
#        if num[i] == num[j]:
#            count = count + 1
c = collections.Counter(num)
for v in c.values():
    if v > 1:
        count = count + v*(v-1)/2
#print(c)
print(int(count))

Submission status: Accepted

Solution code:
def combinations_count(all, pick=2):
    tmp_p = [x for x in range(all, all-pick, -1)]
    tmp = [x for x in range(pick, 0, -1)]
    return int(eval("*".join(map(str,tmp_p))) / eval("*".join(map(str,tmp))))

N = int(input())
s = {}
for n in range(N):
    tmp = "".join(sorted(list(input())))
    if tmp in s:
        s[tmp] = s[tmp] + 1
    else:
        s[tmp] = 0


ans = 0
for val in s.values():
    if val != 0:
        ans += combinations_count(val+1)
print(ans)

Submission status: Accepted

Solution code:
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

def sumi(i):
	return i * (i - 1) // 2

N = int(input())
mydict = {}

for i in range(N):
	s = input()
	l = list(s)
	l.sort()
	s = ''.join(l)
	#print(s)
	
	if s in mydict:
		mydict[s] += 1
	else:
		mydict[s] = 1
	
#print(mydict)
ans = 0

for i in mydict.values():
	ans += sumi(i)
	
print(ans)

Submission status: Accepted

Solution code:
# ABC 137, c
n = int(input())
S = []
D = {}
count = 0
for i in range(n):
    s = ''.join(sorted(input()))
    if s in D:
        count += D[s]
        D[s] += 1
    else:
        D[s] =1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
s = [list(input()) for _ in range(N)]
l = []
for st in s:
    st.sort()
    l.append("".join(st))
l.sort()
cnt = 0
c = 1
for i in range(N-1):
    if l[i]==l[i+1]:
        c += 1
    else:
        if c>1:
            cnt += (1+(c-1))*(c-1)//2
        c = 1
print(cnt+(1+(c-1))*(c-1)//2)

Submission status: Accepted

Solution code:
import collections
n=int(input())
ab=[list(input()) for l in range(n)]

c=[0]*n
for i in range(n):
    c[i]=sorted(ab[i])
moji=[0]*n
for i in range(n):
    moji[i] = ','.join(c[i])

c = collections.Counter(moji)
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
d=c.values()
d=list(d)
ct=0
for i in range(len(d)):
    if d[i]==2:
        ct+=1
    elif d[i]>2:
        ct+=combinations_count(d[i], 2)
print(ct)

Submission status: Accepted

Solution code:
from sys import stdin
from math import factorial as f
N = int(input())
words = []
for s in stdin:
    words.append(
        ''.join(sorted(s.strip()))
    )

words = sorted(words)
ans = 0
p = words[0]
streak = 1
s = []

for i in range(1, N):
    if p == words[i]:
        streak += 1
    else:
        s.append(streak)
        streak = 1
        p = words[i]

s.append(streak)

for n in s:
    if n == 1:
        continue
    elif n == 2:
        ans += 1
    else:
        ans += f(n) // (2 * f(n - 2))

print(ans)

Submission status: Accepted

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

cnt = 0
p = 1
tmp = 0
while p<n:
  if s_list[p-1]==s_list[p]:
    tmp += 1
  else:
    if tmp==1:
      cnt += tmp
    elif tmp>1:
      tmp += 1
      cnt += (tmp*(tmp-1))//2
    tmp = 0
  p += 1
if tmp==1:
  cnt += tmp
elif tmp>1:
  tmp += 1
  cnt += (tmp*(tmp-1))//2
print(cnt)

Submission status: Accepted

Solution code:
import math
n = int(input())
s_list = []
for i in range(n):
    l = list(input())
    l.sort()
    s = ''.join(map(str, l))
    s_list.append(s)
    #print(s)
s_list.sort()
#print(s_list)

old_s = ""
length = 1
num = 0
for s in s_list:
    if old_s == s:
        length += 1
    else:
        if length > 1:
          #print(math.factorial(length) // (math.factorial(length - 2) * math.factorial(2)))
          num += math.factorial(length) // (math.factorial(length - 2) * math.factorial(2))
        old_s = s
        length = 1
if length > 1:
	num += math.factorial(length) // (math.factorial(length - 2) * math.factorial(2))
print(num)

Submission status: Accepted

Solution code:
N=int(input())
ans=0
data={}

for i in range(N):
    s=''.join(sorted(input()))
    if s in data:
        ans+=data[s]
        data[s]+=1
    else:
        data[s]=1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
slist = {}
ans = 0
for i in range(N):
    S = []
    moji = input()
    for j in range(10):
        S.append(moji[j])
    ins = "".join(sorted(S))
    if ins in slist:
        ans += slist[ins]
        slist[ins] += 1
    else:
        slist[ins] = 1

mul = slist.values()
ansl = [n for n in mul if n!=1]

ret = 0
for x in ansl:
    ret += combinations_count(x, 2)
print(ret)

Submission status: Accepted

Solution code:
n = int(input())

s = []
for i in range(n):
    s.append(input())
a = {}
for i in range(n):
    tmp = [u for u in s[i]]
    tmp.sort()
    tmp="".join(tmp)
    if tmp in a:
        a[tmp] +=1
    else:
        a[tmp] =1

c =0
for  k,d in a.items():
    c +=d * (d-1) /2
print(int(c))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = []

import collections

for i in range(n):
    s = ns()
    s = sorted(s)
    a.append(''.join(s))

b = collections.Counter(a)
ans = 0


for k,v in b.items():
    if v > 1:
        ans += v*(v-1)//2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
List=[str(input()) for i in range(n)] 
S_List=[]
A_List=[]
ans=1
for i in range(n):
  S_List.append(sorted(List[i]))
S_List.sort()
tmp = S_List[0]
for i in range(1,n):
  if tmp == S_List[i]:
    ans+=1
  else:
    A_List.append(ans)
    ans=1
    tmp=S_List[i]
A_List.append(ans)
ANS=0
for i in range(len(A_List)):
  ANS+=int(A_List[i]*(A_List[i]-1)/2)
print(ANS)

Submission status: Accepted

Solution code:
def cmb(n, r):
    if n == 1: return 0
    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 = [input() for _ in range(n)]
S_sorted = ["".join(sorted(s)) for s in S]
S_sorted = sorted(S_sorted)

ans = 0
cnt = 1
for i in range(1, len(S_sorted)):
    if S_sorted[i] == S_sorted[i-1]:
        cnt += 1
    else:
        ans += cmb(cnt, 2)
        cnt = 1

if cnt > 1:
    ans += cmb(cnt, 2)

print(int(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict
import logging
logging.basicConfig(level=logging.DEBUG)
logging.disable(logging.CRITICAL)

def main():
    N = int(input())
    
    my_dict = defaultdict(int)
    for _ in range(N):
        s = ''.join(sorted(input()))
        if s in my_dict:
            my_dict[s] += 1
        else:
            my_dict[s] = 0

    ans = 0
    for _, v in my_dict.items():
        logging.debug("{}".format(v))
        if v == 1:
            ans += 1
        else:
            ans += ((v + 1) * v / 2)

    return ans

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

Submission status: Accepted

Solution code:
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())
dic = {}
ans = 0
for i in range(N):
    si = input()
    si_list = sorted(si)
    key = ''.join(si_list)
    if key in dic:
        dic[key] = dic[key] + 1
    else:
        dic[key] = 1

for k in dic:
    v = dic[k]
    if v >= 2:
        ans += cmb(v, 2)

print(ans)

Submission status: Accepted

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

for i in range(n):
  arr[i].sort()
ans = list(Counter([tuple(i) for i in arr]).values())

a = 0
for i in range(len(ans)):
  tmp = ans[i]
  a += tmp*(tmp-1)//2
  
print(a)

Submission status: Accepted

Solution code:
N = int(input())
count = {}
ans = 0

for _ in range(N):
    ele = "".join(sorted(input()))
    if ele in count.keys():
        ans += count[ele]
        count[ele] += 1
    else:
        count[ele] = 1
  
print(ans)

Submission status: Accepted

Solution code:
import math

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

cnt_1 = 0
x = lst[0]
i = 1
cnt_2 = 1
while i < n:

    if x == lst[i]:
        cnt_2 += 1

    else:
        x = lst[i]
        if cnt_2 != 1:        
            cnt_1 += int(math.factorial(cnt_2) / math.factorial(cnt_2 - 2) / 2)
        cnt_2 = 1
    i += 1

if cnt_2 != 1:
    cnt_1 += int(math.factorial(cnt_2) / math.factorial(cnt_2 - 2) / 2)


print(cnt_1)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    temp = input()
    S.append(''.join(sorted(temp)))
S.sort()
row = 1
ans = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        row += 1
    else:
        ans += (row*(row-1) // 2)
        row = 1
if row > 1:
    ans += (row*(row-1) // 2)
print(str(ans))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
s = []
for i in range(N):
    s.append("".join(sorted(input())))
    
ans = 0
for i in Counter(s).values():
    if i != 1:
        n = i - 1
        ans += int(n / 2 * (n + 1))
print(ans)

Submission status: Accepted

Solution code:

def verify_anagrams(w1, w2):
    f = lambda y: "".join(sorted(map(lambda x: x.replace(' ', '').lower(), y)))
    return f(w1) == f(w2)
#print(verify_anagrams(s,s2))
N = int(input())
ans = 0
unmatch_words = []
words = {}
for i in range(N):
    s = input()
    sorted_s = "".join(sorted(s))
    #print(sorted_s)
    if sorted_s in words:
        ans += words[sorted_s]
        words[sorted_s] += 1
    else:
        words[sorted_s] = 1


print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s_count = list()
for i in range(n):
    s = input()
    s_list = ""
    for j in range(26):
        s_list += str(s.count(chr(j + 97)))
    s_count.append(s_list)
s = collections.Counter(s_count)
res = 0
for i in s.values():
    if i >= 2:
        res += i * (i - 1) // 2
print(res)

Submission status: Accepted

Solution code:
from math import factorial
n=int(input())
s=[]
k=1
ans=0
for i in range(n):
  s.append(sorted(input()))
s=sorted(s)
for j in range(n-1):
  if s[j]==s[j+1]:
    k+=1
  elif s[j]!=s[j+1] and k>2:
    ans+=factorial(k)/(factorial(2)*factorial(k-2))
    k=1
  elif s[j]!=s[j+1] and k==2:
    ans+=1
    k=1
  else:
    k=1
if k>2:
  ans+=factorial(k)/(factorial(2)*factorial(k-2))
elif k==2:
  ans+=1
print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
ans = 0
k = set()
c = Counter()

for _ in range(n):
    s = ''.join(sorted(list(input().rstrip())))
    if s in k:
        c[s] += 1
    k.add(s)

ans = 0
for ck in c.keys():
    ans += c[ck] * (c[ck] + 1) // 2

print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())

si = []
base_i = ord('a')
for i in range(N):
    inp = input()
    si.append(str(sorted(inp)))

c = collections.Counter(si)

def nc2(n):
    return n*(n-1)//2

ans = 0
for i in c.values():
    if i > 1:
        ans += nc2(i)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lists = []
for i in range(n):
    a = input()
    k = []
    for i in range(10):
        k.append(a[i])
    k.sort()
    lists.append(k)
lists.sort()
lists.append([])
count = -1
ans = 0
for i in range(n):
    if lists[i] != lists[i+1]:
        c = i-count
        count = i
        ans += c*(c-1)//2
print(ans)

Submission status: Accepted

Solution code:

def get_tousa(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans

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

s_hash = {}

for i in s:
    tmp = sorted(i)
    tmp = ''.join(tmp) 
    if tmp in s_hash:
        s_hash[tmp] += 1
    else:
        s_hash[tmp] = 0
s_hash = sorted(s_hash.items(), key = lambda x: x[1])


ans = 0
for i in s_hash:
    ans += get_tousa(i[1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lS = [list(input()) for _ in range(N)]
S = [''.join(sorted(s)) for s in lS]
S = sorted(S)

cnt = 1
S.append('end')
last = S[0]
res = 0
for s in S[1:]:
    if last == s:
        cnt += 1
    else:
        res += cnt * (cnt-1) /2
        last = s
        cnt = 1

print(int(res))

Submission status: Accepted

Solution code:
import collections
from math import factorial
n = int(input())
strs = ["".join(sorted(input())) for i in range(n)]
c = collections.Counter(strs)
ans = 0
for key, val in c.items():
	if val < 2:
		continue
	elif val == 2:
		ans += 1
	else:
		ans += factorial(val) // (2 * factorial(val - 2))
print(ans)

Submission status: Accepted

Solution code:
# 2019/08/10

import sys
input=sys.stdin.readline
from collections import Counter

n=int(input())
s=[]
for i in range(n):
    a=sorted(list((input().rstrip('\n'))))
    a=''.join(a)
    s.append(a)
c=Counter(s)

cnt=0
for k,v in c.items():
    if v>1:
        cnt+=v*(v-1)//2

print(cnt)

Submission status: Accepted

Solution code:
import collections

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


def main():
    for _ in range(n):
        w = list(input())
        w.sort()
        s.append("".join(w))

    counter = collections.Counter(s)
    _, c = zip(*counter.most_common())

    ans = 0

    for num in c:
        if num == 1:
            return ans
        ans += num * (num-1) // 2

    return ans


print(main())

Submission status: Accepted

Solution code:
from scipy.misc import comb

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

#num2alpha = lambda c: chr(c+93)
#num2alpha(3) #==> C


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

#alpha2num('Y') #==> 25



s_list = [[0 for i in range(26)]for j in range(n)]

#print(s_list[26][0])

#exit()

for i in range(n):
    for j in range(10):
        #print(alpha2num(s[i][j]))
        s_list[i][alpha2num(s[i][j])] += 1
        
#print(s_list)
s_list.sort()

#print(s_list)

ans = []
tmp = 0

for i in range(n-1):
    if s_list[i] == s_list[i+1]:
        if i == n-2:
            tmp += 1
            ans.append(tmp)
        tmp += 1
    else:
        ans.append(tmp)
        tmp = 0

f_ans = 0

for i in range(len(ans)):
    f_ans += (ans[i] + 1) * ans[i] * 0.5

print(int(f_ans))

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())
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 = readi()
t = []
for i in range(n):
    s = list(read())
    s.sort()
    t.append(s)
t.sort()
index = 0
count = 0
# print(t)
d = 0
for i in range(1,n):
    if t[index] == t[i]:
        d += 1
    else:
        if d >= 1:
            d += 1
            count += cmb(d, 2)
        d = 0
        index = i
if d >= 1:
    d += 1
    count += cmb(d,2)
print(count)

Submission status: Accepted

Solution code:
a=[]
b=int(input())
c=[]
d=[]
i=0
n=0
nn=0
while i<b:
    for x in input():
        c.append(x)
    c.sort()
    a.append(''.join(c))
    c=[]
    i+=1
a.sort()
i=1
while i<b:
    if a[i-1]==a[i]:
        if n==0:
            n=2
        else:
            n+=1
    else:
        if n!=0:
            d.append(n)
            n=0
    i+=1
if n!=0:
    d.append(n)
for x in d:
    nn+=x*(x-1)//2
print(nn)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize

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
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()
a =[]

for i in range(n):
    k = list(input())
    k.sort()
    a.append(k)

a.sort()
b = s(a)

co = 0

for i in range(len(b)):
    if b[i][1] != 1:
        co += cmb(b[i][1],2)
print(co)

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for i in range(N)] 
s = []
for i in range(N):
  s+=["".join(sorted(S[i]))]
s.sort()
s+=[""]
before=s[0]
num=0
num2=1
for i in range(1,N+1):
#  print(num2)
  if s[i]==before:num2+=1
  else:
    if num2>1:
      num+=num2*(num2-1)//2
      num2=1
    before=s[i]
print(num)

Submission status: Accepted

Solution code:
from scipy.misc import comb
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]
chars = ["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"]
result = []
for n in range(N):
    res = {}
    for char in chars:
        res[char] = S[n].count(char)
    result.append(res)
result2 = Counter([str(i) for i in result ])
cnt = 0
for key in result2.keys():
    if result2[key] == 1:
        pass
    else:
        cnt += comb(result2[key],2,exact=True)
print(int(cnt))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = list(map(lambda x: ''.join(sorted(x)) ,[list(input()) for i in range(n)]))
ans = 0
for i in Counter(s).values():
    ans += int(i*(i-1) / 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append(input())
anaglams = {}
count = 0
for s in S:
    chrs = [0]*26
    for c in s:
        index = ord(c) - ord('a')
        chrs[index] += 1
    if str(chrs) not in anaglams:
        anaglams[str(chrs)] = 0
    else:
        anaglams[str(chrs)] += 1
    count += anaglams[str(chrs)]
print(count)

Submission status: Accepted

Solution code:
import collections

n=int(input())

alls=[]
for i in range(n):
  s=list(input())
  s.sort()
  s="".join(s)
  alls.append(s)
  
c = collections.Counter(alls)

#print(c.most_common())
aa=c.most_common()
n=len(aa)
allans=0
for j in range(n):
  num=aa[j][1]
  if num!=1:
    ans=num*(num-1)//2
    allans+=ans
print(allans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = [input().strip() for u in range(n)]
r = [dict(collections.Counter(m)) for m in s]
t = ["".join(["{}{}".format(k, m[k]) for k in sorted(m.keys())]) for m in r]
u = dict(collections.Counter(t))
print(int(sum([v*(v-1)/2 for v in u.values()])))

Submission status: Accepted

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

    mozi = input()
    for k in range(10):
        string[i] += (ord(mozi[k])-96)**10

string_new = sorted(string)
ite = 0
index = 0
while ite < N-1:
    index = ite + 1
    while string_new[ite] == string_new[index]:
        #print(ite, index)
        ans += index-ite
        index += 1

        if index >= N:
            break
    ite = index
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
# N = 1000
string_dict = defaultdict(int)

for i in range(N):
    inp = list(input())
    # inp = list("aaaa")
    inp.sort()

    string_dict["".join(inp)] += 1
# print(string_dict)

count = 0
for i in string_dict.values():
    count += i*(i-1)//2
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    s = sorted(list(input()))
    S.append(''.join(s))
S.sort()
cnt = 1
ans = 0
for i in range(1, N):
    if S[i] == S[i-1]:
        cnt+=1
    else:
        ans+=cnt*(cnt-1)//2
        cnt = 1
ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
s = [''.join(sorted(sys.stdin.readline().strip())) for _ in range(n)]

import collections
c = collections.Counter(s)

import math
def combination(v):
    return int(math.factorial(v) / (math.factorial(v - 2) * math.factorial(2)))
print(sum(combination(v) for v in c.values() if v != 1))

Submission status: Accepted

Solution code:
n = int(input())

conbi = 0

strings = {}
for i in range(n):
  
  notinstr = 1
  s = input()
  l = 0
  for c in s:
    l += 10**(ord(c)-ord('a'))
  #print(l)
  
  if l in strings:
    conbi += strings[l]
    strings[l] += 1
    
  else:
    strings[l] = 1
  
print(conbi)

Submission status: Accepted

Solution code:
wa = [0, 0, 1]
for i in range(2, 100005):
    wa.append(wa[-1]+i)

# print(wa)
# exit()

N = int(input())

# data = set()
data = {}

ans = 0
for i in range(N):
    s_list = list(str(input()))
    s_list = sorted(s_list)
    hoge = "".join(s_list)
    data.setdefault(hoge, 0)
    data[hoge] += 1

for v in data.values():
    ans += wa[v]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
S = {}
for _ in range(n):
    s = ''.join(sorted(input()))
    if s in S.keys():
        S[s] += 1
    else:
        S[s] = 1

for i in S.values():
    ans += (i*(i-1)) // 2
print(ans)

Submission status: Accepted

Solution code:
counter = 0

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

import collections
dic = collections.Counter(lst)

for i in dic.values():
    if i == 0:
        continue
    counter = counter +  i * (i - 1) / 2
print(int(counter))

Submission status: Accepted

Solution code:
import collections
import math

n = int(input())
s = []
for i in range(n):
    ss = input()
    s.append(tuple(sorted(collections.Counter(list(ss)).items())))
s = sorted(s)

ans = []
px = None
a = 0
for x in s:
    if x == px:
        a += 1
    else:
        px = x
        if a > 0:
            ans.append(a + 1)
        a = 0
if a > 0:
    ans.append(a + 1)
print(sum([math.factorial(a) // (2 * math.factorial(a - 2)) for a in ans]))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
anagram = Counter()
for i in range(N):
    anagram[''.join(sorted(input()))]+=1

print(int(sum(map(lambda x: x*(x-1)/2, anagram.values()))))

Submission status: Accepted

Solution code:
import collections
n = int(input())
moji = []
ans = 0
for i in range(n):
    tmp = list(input())
    tmp.sort()
    moji.append(''.join(tmp))
cnt = collections.Counter(moji)
for i in cnt.values():
    if i != 1:
        ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
dic1 = {}
for _ in range(n):
    s = list(input())
    key = ""
    dic = {}
    for i in s:
        if i in dic:
            dic[i] += 1
        else:
            dic[i] = 1
    for k in sorted(dic.keys()):
        key += "{}{}".format(k, dic[k])

    if key in dic1:
        dic1[key] += 1
    else:
        dic1[key] = 1

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

Submission status: Accepted

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

def get_sum(num):
    return num*(num-1)/2


N = int(input())
S = []
D = defaultdict(int)
ans = 0
for _ in range(N):
    s = str(sorted(input()))
    D[s] += 1

for k, v in D.items():
    ans += get_sum(v)


print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(S)
print(sum([k*(k-1)//2 for k in c.values()]))

Submission status: Accepted

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

for i in range(n):
    moji=input()
    moji2=sorted(moji)
    #print(moji2)
    mojilist.append(moji2)

mojilist=sorted(mojilist)
combo=0
answer=0
for i in range(n-1):
    if mojilist[i]==mojilist[i+1]:
        combo+=1
    else:
        if combo!=0:
            answer+=int(combo*(combo+1)/2)
        combo=0

if combo!=0:
    answer+=int(combo*(combo+1)/2)

print(answer)

Submission status: Accepted

Solution code:
import math
def combinations(n, r):
  if n == r:
    return 1
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
s = [sorted(input())]
ans = 0
for i in range(N-1):
  s_i = sorted(input())
  s.append(s_i)
s = sorted(s)
count=1
for j in range(N-1):
  if s[j] == s[j+1]:
    count+=1
  else:
    if count != 1:
      ans += combinations(count,2)
    count=1
if count != 1:
  ans += combinations(count,2)
print(ans)

Submission status: Accepted

Solution code:
import logging
from collections import Counter
import math

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

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

def C(n, r):
    return P(n, r)//math.factorial(r)

def main():
    N = int(input())
    S_ = ["".join(sorted(list(input()))) for _ in range(N)]
    
    logging.info(S_)

    counters = dict(Counter(S_))

    logging.info(counters)
    
    ans = 0
    for key, val in counters.items():
        if val != 1:
            ans += C(val, 2)

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

Submission status: Accepted

Solution code:
import math
n = int(input())
s = []
for i in range(n):
    a = input()
    a = [i for i in a]
    a = sorted(a)
    s.append(a)
count = 0
s = sorted(s)
p = 0
try:
    for i in range(len(s)):
        if s[i]==s[i+1]:
            p+=1
        else:
            count+=sum(list(range(p+1)))
            p=0
except:
    count+=sum(list(range(p+1)))
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
import math
n=int(input())
s=[['']*10]*n

def com(n, r):
    if n>=r:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0
for i in range(n):
    s[i]=list(input())
    s[i].sort()
s=map(tuple,s)
c=dict(Counter(s))
x=0
for i in c.values():
    x+=com(i,2)
print(x)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
import math

N = int(sys.stdin.readline())
s_list = ["".join(sorted(list(sys.stdin.readline().rstrip()))) for x in range(N)]
s_list = Counter(s_list)
count = 0
for i in s_list.values():
    if i == 1:
        continue
    count += (math.factorial(i) // math.factorial(i - 2)) // math.factorial(2)
print(count)

Submission status: Accepted

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

res = []
ans = 0
res.append(s_list[0])
for j in range(N-1):
    if s_list[j] == s_list[j+1]:
        res.append(s_list[j+1])
    else:
        if len(res) >=2:
            ans += (len(res) * (len(res)-1)) //2
        res = []
        res.append(s_list[j+1])

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

print(ans)