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:
from collections import Counter
n = int(input())
l = [''.join(sorted(input())) for i in range(n)]
    
c = Counter(l)
cnt = 0
for val in c.values():
    cnt += val * (val-1) //2

print(cnt)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = [''.join(sorted(list(input()))) for i in range(N)]
c = collections.Counter(S)
print(sum([int(x * (x - 1) / 2) for x in c.values()]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
c = Counter([''.join(sorted(input())) for i in range(n)])

ans = 0
for k, cnt in c.items():
    ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 13, 2020 23:02:56 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


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


def main():
  N = int(input())
  S = [input()[:-1] for _ in range(N)]

  dic = defaultdict(int)

  for i in range(N):
    dic[''.join(sorted(S[i]))] += 1

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


if(__name__ == '__main__'):
  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

from collections import Counter
n = int(input())
list_N = []
ans = 0

for _ in range(n):
    s = sorted(input())
    list_N.append("".join(s))

list_M = list(Counter(list_N).values())

for i in list_M:
    if i != 1:
        ans += cmb(i, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
d = defaultdict(int)
count = 0
for i in range(n):
    s = "".join(sorted(input()))
    count += d[s]
    d[s] += 1
print(count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    slis = list()
    ans = 0
    sdic = {}
    for _ in range(n):
        s = input()
        s = ''.join(sorted(s))
        #print(s)
        if s in sdic:
            tmp = sdic[s]
            ans += tmp
            sdic[s] += 1
        else:
            sdic[s] = 1
            #print(sdic)
    print(ans)

    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections

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

for _ in range(n):
    a = sorted(input())
    l.append("".join(a))
    
ls = collections.Counter(l)
r = list(ls.values())

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

ans = 0
for i in r:
    ans += cul(i)
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(n):
    s = sorted(input())
    try:
        dic[''.join(s)] += 1
    except:
        dic[''.join(s)] = 1
    
ans = 0
for i in dic.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

for i in range(n):
    sortedstr = ''.join(sorted(s[i]))
    if sortedstr in sdict:
        sdict[sortedstr] = sdict[sortedstr] + 1
    else:
        sdict[sortedstr] = 1

ans = 0
for key in sdict.keys():
    cnt = sdict[key]
    ans += int(cnt * (cnt-1) / 2)
print(ans)

Submission status: Accepted

Solution code:
# やるだけ（ソートしたものをもっていればいいのと、2つえらぶさんすう）
d = {}
for _ in range(int(input())):
    s = "".join(sorted(list(input())))
    if s not in d:
        d[s] = 1
    else:
        d[s] += 1
ans = 0
for v in d.values():
    if v > 1:
        ans += v * (v - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
dict={}
ans=0
for i in range(n):
  s=str(sorted(input()))
  if s in dict:
    dict[s]+=1
    ans+=(dict[s]-1)
  else:
    dict[s]=1
print(ans)

Submission status: Accepted

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

mem = {}
ans = 0
for s in xs:
  if s in mem:
    ans += mem[s]
    mem[s] += 1
  else:
    mem[s] = 1
print(ans)

Submission status: Accepted

Solution code:
import sys

# import bisect
# map(int, sys.stdin.read().split())
from collections import Counter



def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    S = []
    for i in range(N):
        s =list(input())
        s.sort()
        s = "".join(s)
        S.append(s)

    C  = Counter(S)
    ans =0
    for key,value in C.items():
        ans +=value*(value-1)/2

    print(int(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
b=[]
for _ in range(n):
  g=str(input())
  b.append(str(sorted(g)))
import collections
c = collections.Counter(b)
#print(c)
cnt=0
for i in c:
  cnt+=c[i]*(c[i]-1)/2
print(int(cnt))

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
d = defaultdict(int)
for i in [input() for j in range(n)]:
  d["".join(sorted(i))]+=1

from math import factorial as f

ans = 0
for i in d:
  if d[i]>1:
    ans+=f(d[i])//f(2)//f(d[i]-2)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

def comb(n,r):
  return math.factorial(n)//math.factorial(r)//math.factorial(max(1,n-r))

def con(t):
  ans = ""
  for i in t:
    ans+=i[0]+str(i[1])
  return ans

n = readInt()
d = defaultdict(int)
for i in [input() for i in range(n)]:
  t = defaultdict(int)
  for j in i:
    t[j]+=1
  d[con(sorted(t.items()))]+=1
ans = 0
for i in d:
  ans+=comb(d[i],2)

print(ans)

Submission status: Accepted

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

S = defaultdict(int)

for i in range(N):
  s = sorted(input())
  s = "".join(s)
  S[s] += 1
  
res = 0
for val in S.values():
  res += val * (val-1) // 2

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
d = defaultdict(int)
sens = set()
ans = 0
for _ in range(n):
  s = sorted(input())
  s = ''.join(s)
  if s in sens:
    d[s] += 1
    ans += d[s]
  else:
    sens.add(s)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math
prm = [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]
alp = {chr(i):p for i,p in zip(range(97, 97+26),prm)}
n = int(input())
d = defaultdict(int)
nums = set([])
for _ in range(n):
  s = input()
  m = 1
  for l in s:
    m *= alp[l]
  d[m] += 1

def comb(n, r):
  if n<r:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

ans = 0
for k in d.values():
  ans += comb(k,2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
d = {}
ans = 0
for _ in range(N):
    s = str(sorted(input()))
    if s not in d.keys():
        d[s] = 0
    else:
        d[s] += 1
        ans += d[s]
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
lan = ["".join(sorted(input())) for n in range(N)]
#print(lan)
lans = collections.Counter(lan)
#print(lans)
ans = 0
for si in set(lan):
    n = lans[si]
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

dic = {}

for _ in range(N):
  s = input()
  s = sorted(s)
  s = "".join(s)
  if s not in dic:
    dic[s] = 1
  else:
    dic[s] +=1
  
rlt = 0
for k in dic:
  rlt += dic[k]*(dic[k]-1)//2
  
print(rlt)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
a = [0]*n
for i in range(n):
    for j in range(10):
        a[i] += 10**(ord(s[i][j])-97)

a.sort()

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

Submission status: Accepted

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

def makeFreqTuple(s):
    freq = [0] * 26
    for c in s:
        freq[ord(c)-ord("a")] += 1
    return tuple(freq)

def C(n, k):
    numera = 1
    for i in range(n, n-k, -1):
        numera *= i
    denomi = 1
    for i in range(k, 1, -1):
        denomi *= i
    return numera // denomi

dic = defaultdict(int)
for i in range(n):
    s = input()
    t = makeFreqTuple(s)
    dic[t] += 1

ans = 0
for k, v in dic.items():
    ans += C(v, 2)
print (ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s_dict = {}
    for _ in range(N):
        s = input()
        s_sort = "".join(sorted(list(s)))
        if s_sort not in s_dict:
            s_dict[s_sort] = 0
        s_dict[s_sort] += 1

    res = 0
    for v in s_dict.values():
        res += v*(v-1)//2

    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    S = sorted(["".join(sorted(input())) for _ in range(N)])
    #print(S)
    ans = 0
    cnt = 0
    for i in range(1, N):
        if S[i-1] == S[i]:
            if cnt == 0:
                cnt = 2
            else:
                cnt += 1
        else:
            if cnt != 0:
                ans += cnt*(cnt-1)//2
            cnt = 0
    if cnt != 0:
        ans += cnt*(cnt-1)//2
    print(ans) 
    


    
if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:


if __name__ == "__main__":
    N = int(input())
    mydict = {}
    ans = 0
    for i in range(N):
        s = list(input())
        S = [str(c) for c in s]
        S.sort()
#        print(S)
        if str(S) in mydict.keys():
            mydict[str(S)] += 1 
        else:
            mydict[str(S)] = 1
#    print(mydict)

    for i in mydict.keys():
        tmp = mydict[i]
        if tmp >= 2:
            ans += tmp*(tmp-1)//2
    print(ans)

Submission status: Accepted

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

count = 0
dic = {}

for a in s:
    if a in dic.keys():
        count += dic[a]
        dic[a] += 1
    else:
        dic[a] = 1

print(count)

Submission status: Accepted

Solution code:
from math import factorial

n = int(input())
S = [input() for _ in range(n)]
d = dict()

for s in S:
    s = ''.join(sorted(s))
    if s not in d.keys():
        d[s] = 1
    else:
        d[s] += 1

d_comb = dict()
def fact(n):
    keys = d_comb.keys()
    if n not in keys:
        d_comb[n] = factorial(n)
    return d_comb[n]

def comb(n, r):
    if n < r:
        return 0
    f_n = fact(n)
    f_nr = fact(n - r)
    f_r = fact(r)
    return f_n // (f_nr * f_r)

ans = 0
for k in d.keys():
    ans += comb(d[k], 2)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n=int(input())
    s={}
    for i in range(n):
        x=str(sorted(list(input())))
        if x in s.keys():
            s[x]+=1
        else:
            s[x]=1
    ans=0
    if s:
        for i in s.values():
            ans+=i*(i-1)//2
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter
from collections import Counter
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def ss(): return input().split()

def main():
    N = ii()
    S = Counter()
        
    for s in [input().rstrip() for _ in range(N)]:
        s = list(s)
        s.sort()
        s = "".join(s)
        S[s] += 1

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

    print(ans)

    return

main()

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
S = []
for i in range(N):
	S.append(rs())

import collections
h = collections.defaultdict(int)
for s in S:
	l = [0] * 26
	for c in s:
		n = ord(c)-ord('a')
		l[n] += 1
	h[tuple(l)] += 1

def comb(n, k):
	p, q = 1, 1
	for i in range(1, k+1):
		p = p * (n-i+1)
		q = q * i
	return p // q

ans = 0
for k, v in h.items():
	if v > 1:
		ans += comb(v, 2)
print(ans)

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import  operator

def solve():
    n = int(input())
    s = ["".join(sorted(input())) for _ in range(n)]
    cnt = 0
    past = {}
    for i in range(n):
        si = s[i]
        if si in past:
            past[si] += 1
        else:
            past[si] = 1
    for i in past.values():
        cnt += i*(i-1)//2
    print(cnt)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

lst = []

for _ in range(N):
  S = input()
  S = sorted(S)
  S = ''.join(S)
  lst.append(S)

dic = Counter(lst)

rlt = 0
for k in dic:
  rlt += (dic[k]-1)*dic[k]//2
  
print(rlt)

Submission status: Accepted

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

n = int(input())
sn = [input().strip('\n') for _ in range(n)]

mod = 10**6 + 7
hs = [[0, ''] for _ in range(mod)]

def hypo(s, Mod):
    h_num = 1
    for i in s:
        h_num = h_num * ord(i) % Mod
    return h_num

for s in sn:
    s = ''.join(sorted(s))
    h = hypo(s, mod)
    while True:
        if h >= mod :
            h = 0
        if hs[h][0] == 0:
            hs[h][0] += 1
            hs[h][1] += s
            break
        if hs[h][1] == s:
            hs[h][0] += 1
            break
        h += 1

ans = 0
for j, t in hs:
    if j >= 2:
        ans += j * (j-1) / 2

print(int(ans))

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 si in set(s):
    n = c[si]
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
dic={}
for i in range(n):
  tmp="".join(sorted(list(input().strip())))
  dic[tmp]=dic.get(tmp,0)+1
ans=0
for k in dic.keys():
  if dic[k] >= 2:
    ans+=(dic[k]*(dic[k]-1))//2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s={}
for i in range(n):
  tmp=list(input().strip())
  tmp.sort()
  tmp="".join(tmp)
  s[tmp]=s.get(tmp,0)+1
  
ans=0
for key in s.keys():
  v=s[key]
  if v<2:
    continue
  cmb=v*(v-1)
  cmb=cmb//2
  ans+=cmb
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = [''.join(sorted(list(input()))) for _ in range(n)]
s = collections.Counter(s)

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

Submission status: Accepted

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

s_dict = dict([])

for i in s:
    key = ''.join(sorted(i))
    s_dict.setdefault(key, 0)
    s_dict[key] += 1

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = ["".join(sorted(input())) for i in range(N)]
ans = 0
for i in Counter(S).values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

s_dict = dict([])

for i in s:
    key = ''.join(sorted(i))
    try:
        s_dict[key] += 1
    except KeyError:
        s_dict[key] = 1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from scipy.special import comb

n, *s = open(0).read().split()
d = {}
for i in s:
    l = "".join(sorted(j for j in i))
    if l in d:
        d[l] += 1
    else:
        d[l] = 1
print(sum(comb(i, 2,exact=True) for i in d.values()))

Submission status: Accepted

Solution code:
import collections
def main4():
    dd = collections.defaultdict(int)
    n = int(input())
    res = 0
    for i in range(n):
        s = input()
        a = sorted(s)
        dd[''.join(a)]+=1
    for i in dd.values():
        res += (i*(i-1))//2
    print(res)


if __name__ == '__main__':
    main4()

Submission status: Accepted

Solution code:
import collections
def main4():
    n = int(input())
    l = []
    for i in range(n):
        s = input()
        l.append((sorted(s)))
    l.sort()
    dd = collections.defaultdict(int)
    k = []
    for i in range(n):
        k.append(''.join(l[i]))
    res = 0
    for i in range(n):
        dd[k[i]]+=1
    for i in dd.values():
        res += (i*(i-1))//2
    print(res)

if __name__ == '__main__':
    main4()

Submission status: Accepted

Solution code:
from collections import defaultdict

n=int(input())
h=defaultdict(int)
for i in range(n):
  s=list(input().strip())
  s.sort()
  h["".join(s)]+=1

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
N = int(input())
d = defaultdict(int)
ans = 0
for i in range(N):
    l = list(input())
    l.sort()
    l = ''.join(l)
    d[l] += 1
for x in d.values():
    ans += x*(x-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s_cnt = {}

ans = 0
for _ in range(n):
    s = input()
    s = ''.join(sorted(s))
    s_cnt.setdefault(s,0)
    ans += s_cnt[s]
    s_cnt[s] += 1

print(ans)

Submission status: Accepted

Solution code:
#create date: 2020-07-03 18:05

import sys
stdin = sys.stdin
from collections import Counter

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    l = list()
    for i in range(n):
        s = ns()
        l.append("".join(sorted(s)))
    ans = 0
    for i in Counter(l).values():
        ans += (i * (i-1))//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

N = int(input())
dic_s = {}
for i in range(N):
    s = "".join(sorted(input()))
    if s in dic_s:
        dic_s[s] += 1
    else:
        dic_s[s] = 1
counter = 0
for key,value in dic_s.items():
    if value >1:
        counter += combinations_count(value,2)
print(counter)

Submission status: Accepted

Solution code:
n = int(input())

s = []

for i in range(n):
    s.append(''.join(sorted(input())))

t = {}
for i in range(n):
    if s[i] in t:
        t[s[i]] += 1
    else:
        t[s[i]] = 1

print(sum(t[k]*(t[k]-1)//2 for k in t)) # t[k] Combi 2のlistのsumが答えになる

Submission status: Accepted

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

d = defaultdict(int)
for i in range(N):
    s = ''.join(sorted(input()))
    d[s] += 1

ans = 0
for c in d.values():
    if c > 1:
        ans += c*(c-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ctr = defaultdict(int)

for i in range(N):
    tmp = [0] * 26
    for j in range(10):
        tmp[ord(S[i][j]) - ord("a")] += 1
    ctr[tuple(tmp)] += 1

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

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())

dic={}
ans=0

for i in range(N):
    si=input()
    # ci=Counter(si)
    # c=sorted(ci.elements())
    # c=sorted(Counter(si).elements())
    sorted_s=''.join(sorted(Counter(si).elements()))
    if sorted_s in dic:
        ans+=dic[sorted_s]
        dic[sorted_s]+=1
    else:
        dic[sorted_s]=1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

ans=0

for i in range(N):
    si=input()
    # ci=Counter(si)
    # c=sorted(ci.elements())
    # c=sorted(Counter(si).elements())
    sorted_s=''.join(sorted(Counter(si).elements()))
    if sorted_s in dic:
        ans+=dic[sorted_s]
        dic[sorted_s]+=1
    else:
        dic[sorted_s]=1
print(ans)

Submission status: Accepted

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

n=int(input())
dic=[]
for i in range(n):
    dic.append(sorted(list(input().rstrip())))
dic=sorted(dic)

from math import factorial

ans=0
temp=1
for i in range(1,n):
    if dic[i]==dic[i-1]:
        temp+=1
    else:
        if temp>=2:
            ans+=(factorial(temp)//(2*factorial(temp-2)))
        temp=1

if temp>=2:
    ans+=(factorial(temp)//(2*factorial(temp-2)))

print(ans)

Submission status: Accepted

Solution code:
import bisect,copy,heapq,itertools,string
from collections import *
from math import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = [sorted(input()) for i in range(n)]
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:
from collections import defaultdict
import math

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

def main():
    N = int(input())
    S = defaultdict(int)
    for _ in range(N):
        s = ''.join(sorted(list(input())))
        if s in S:
            S[s] += 1
        else:
            S.update({s:0})
    cnt = 0
    for k,v in S.items():
        if v == 0:
            continue
        elif v == 1:
            cnt += v
        else:
            cnt += combinations_count(v+1,2)
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# bc137_c.py
import math
def nCr(n,r):
    if n < 2:
        return 0
    ans = int(math.factorial(n)/(math.factorial(n-r)*math.factorial(r)))
    return ans 
import collections
N = int(input())
L = []
for i in range(N):
    S = input()
    S = sorted(S)
    L.append(str(S))
C = collections.Counter(L)
ans = 0
for i in C.values():
    ans += nCr(i,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[i] = ''.join(S[i])

t = {}
for i in range(N):
  if S[i] in t:
    t[S[i]] += 1
  else:
    t[S[i]] = 1
 
print(sum(t[k]*(t[k]-1)//2 for k in t))

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
  tmp = list(input())
  tmp.sort()				# ソート
  s.append(''.join(tmp))	# 文字列の結合
s.sort()

i, j = 0, 1
count = 0
total = 0
while j < N:
  while s[i] == s[j]:
    count += 1
    j += 1
    if j == N:
      break
  
  if count >= 1:
    tmp1, tmp2 = 1, 2
    for k in range(count+1, count+1-2, -1):
      tmp1 *= k
    total += int(tmp1/tmp2)
  i = j
  j += 1
  count = 0
print(total)

Submission status: Accepted

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

i, j = 0, 1
count = 0
total = 0
while j < N:
  while s[i] == s[j]:
    count += 1
    j += 1
    if j == N:
      break
  
  if count == 1:
    total += 1
  elif count > 1:
    tmp1, tmp2 = 1, 2
    for k in range(count+1, count+1-2, -1):
      tmp1 *= k
    total += int(tmp1/tmp2)
  i = j
  j += 1
  count = 0

print(total)

Submission status: Accepted

Solution code:
def ra(a): #圧縮された要素を非保持
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append(l)
            l=1
    ll.append(l)
    return ll

n=int(input())
print(sum([i*(i-1)//2 for i in ra(sorted([''.join(sorted(i)) for i in [input() for i in range(n)]]))]))

Submission status: Accepted

Solution code:
n = int(input())

s = [input() for _ in range(n)]

for i in range(n):
    s[i] = sorted(s[i])
    s[i] = "".join(s[i])

t = {}
for i in range(n):
    if s[i] in t :
        t[s[i]] += 1
    else :
        t[s[i]] = 1

print(sum(t[k]*(t[k]-1)//2 for k in t))

Submission status: Accepted

Solution code:
import collections

ans=0
dic={}
n=int(input())
for i in range(n):
  s=input()
  sl=[i for i in s]
  sl.sort()
  ss=tuple(sl)

  if ss not in dic:
    dic[ss]=1
  else:
    ans+=dic[ss]
    dic[ss]+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
n = int(input())
cnt = 0
for _ in range(n):
  s = "".join(sorted(input()))
  cnt += d[s]
  d[s] += 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
n = int(input())
cnt = 0
for _ in range(n):
  a = input()
  b = ''
  for i in sorted(a):
    b += i
  cnt += d[b]
  d[b] += 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
n = int(input())
for _ in range(n):
  a = input()
  g = [0]*26
  for i in a:
    g[ord(i)-97]+=1
  b = ''
  for i in g:
    b+=str(i)
  d[b]+=1
cnt = 0
for k,v in d.items():
  if v > 1:
    cnt += v*(v-1)//2
print(cnt)

Submission status: Accepted

Solution code:
import sys
import math
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    string = dict()
    ans = 0
    for _ in range(n):
        s = ''.join(sorted(input()))
        if s in string:
            string[s] += 1
        else:
            string[s] = 1
    for i in string.values():
        if i > 1:
            ans += math.factorial(i)//(2*math.factorial(i-2))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
s=["".join(sorted(input())) for _ in range(n)]

d = defaultdict(int)
for a in s:
  d[a]+=1
c=0
for a in d.values():
  if a>1:
    c+=a*(a-1)//2
print(c)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
d = dict()

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

Submission status: Accepted

Solution code:
import collections


def main():
    N = int(input())
    table = [''.join(sorted(list(input()))) for _ in range(N)]
    sort_table = set(table)
    cnt_table = collections.Counter(table)
    ans = 0
    for i in sort_table:
        num_of_i = cnt_table[i]
        if num_of_i <= 1:
            continue
        x = (num_of_i * (num_of_i - 1)) // 2
        ans += x

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
import math
def Comb(N,R):
    return math.factorial(N)//(math.factorial(N-R)*math.factorial(R))

N = int(input())
S = ['']*N
for T in range(0,N):
    S[T] = ''.join(sorted(input()))
Count = list(collections.Counter(S).values())
Sum = 0
for TT in range(0,len(Count)):
    if Count[TT]>=2:
        Sum += Comb(Count[TT],2)
print(Sum)

Submission status: Accepted

Solution code:
n = int(input())
s_cnt = {}

for _ in range(n):
    s = input()
    s = ''.join(sorted(s))
    s_cnt.setdefault(s,0)
    s_cnt[s] += 1

ans = 0
for s,cnt in s_cnt.items():
    ans += cnt*(cnt-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
count_list = []
for i in range(n):
    s = input()
    c = sorted(Counter(s).items(), key = lambda x:x[0])
    tmp = ""
    for j in range(len(c)):
        tmp += c[j][0]
        tmp += str(c[j][1])
    count_list.append(tmp)

c = Counter(count_list)
ans = 0
for i in c.values():
    if i >= 2:
        ans += i * (i - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
n = int(input())
s = [''.join(sorted(list(input()))) for _ in range(n)]

cnt = Counter(s)
ans = 0
for i in cnt.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
n = int(input())
s = [''.join(sorted(list(input()))) for _ in range(n)]

def combination(i):
    return factorial(i) // (factorial(i-2) * factorial(2))

cnt = Counter(s)
ans = 0
for i in cnt.values():
    if i == 1:
        pass
    else:
        ans += combination(i)
print(ans)