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 defaultdict
N=int(input())
charl=[]
ans=0
for _ in range(N):
    charl.append(''.join(sorted(input())))
anad=defaultdict(int)
for char in charl:
    anad[char]+=1
for anadv in anad.values():
    if anadv>1:
        ans+=anadv*(anadv-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
def resolve():
    n = int(input())
    words = []
    for _ in range(n):
        words.append(str(sorted(list(input()))))
    a = collections.Counter(words)
    ans = 0
    for i in a.values():
        ans += i*(i-1)//2
    print(ans)
resolve()

Submission status: Accepted

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

P=sorted(P)
last=P[0]
cnt=0
ans=0
for i in range(1,N):
    if P[i]==last:
        cnt+=1
        ans+=cnt
    else:
        last=P[i]
        cnt=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(n):
    x = "".join(sorted(input()))
    if x not in s:
        s[x] = 1
    else:
        ans += s[x]
        s[x] += 1

print(ans)

Submission status: Accepted

Solution code:

def main():
    N = int(input())

    S = []
    d = {}
    ans = 0
    for _ in range(N):
        t = input()
        t = "".join(sorted(t))
        if t in d:
            ans += d[t]
            d[t] += 1
        else:
            d[t] = 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
d = {}
e = []
ans = 0
for i in range(n):
  s = list(input())
  s.sort()
  s = "".join(s)
  if s in d:
    d[s] += 1
  else:
    d[s] = 1
for key,value in d.items():
  if value > 1:
    e.append(d[key])
for j in e:
  ans += j*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
#他の人の回答
import collections

N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
#sortedでリストになった文字列をjoinでもう一度文字列に
c = collections.Counter(s)
#cは辞書の形をとる
ans = 0

# sから重複を無くす
for si in set(s):
    n = c[si]
    ans += n * (n - 1) // 2 
    # n C 2を計算
print(ans)

Submission status: Accepted

Solution code:
import itertools,math
n = int(input())
word = [input() for _ in range(n)]
count = 0

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

for i in range(n):
    word[i] = sorted(word[i])

new_word = list(sorted(word))

x = 1

for i in range(n-1):
    if new_word[i] == new_word[i+1]:
        x += 1
    elif x >= 2:
        count += combinations_count(x,2)
        x = 1
if x >= 2:
    count +=  combinations_count(x,2)

print(count)

Submission status: Accepted

Solution code:
import collections

from scipy.special import comb


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

for i in range(n):
  s = sorted(input())
  s = [''.join(s)]
  aa.append(*s)

cc = collections.Counter(aa)
c = [c[1] for c in cc.items() if c[1] >1]

ans = 0
for i in range(len(c)):
  ans += comb(c[i], 2, exact=True)
  

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

n=int(input())
l=["".join(sorted(input())) for i in range(n)]
c=Counter(l).most_common()
ans=0
for i in range(len(c)):
    if c[i][1]>=2:
        ans+=combinations_count(c[i][1],2)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd

n=int(input())
dic=dd(int)
for i in range(n):
  s=sorted(input())
  dic[''.join(s)]+=1

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

Submission status: Accepted

Solution code:
def abc137c_green_bin():
    def cmb(n, r):
        """ nCrの組み合わせ数を返す """
        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())
    dict_s = {}
    for _ in range(n):
        s = ''.join(sorted(input()))
        if s in dict_s.keys():
            dict_s[s] += 1
        else:
            dict_s[s] = 1
    result = 0
    for k in dict_s:
        if dict_s[k] != 1:
            result += cmb(dict_s[k], 2)
    print(result)
abc137c_green_bin()

Submission status: Accepted

Solution code:
n = int(input())
d = dict()
for i in range(n):
  S = ''.join(sorted(list(input())))
  if S in d.keys():
    d[S] +=1
  else:
    d[S] = 1
 
ans = 0
for i in d.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
Aset = dict()
Ans = 0
for _ in range(N):
    S = input()
    NewS = ''
    S = sorted(S)
    for i in range(len(S)):
        NewS += S[i]
    if NewS not in Aset:
        Aset[NewS] = 1
    else:
        Ans += Aset[NewS]
        Aset[NewS] += 1
print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [list(input()) for i in range(n)]
cnt_dic = {}
sum_cnt = 0

for i in range(n):
    s[i].sort()
    if str(s[i]) in cnt_dic:
        cnt_dic[str(s[i])] = cnt_dic[str(s[i])] + 1
    else:
        cnt_dic[str(s[i])] = 1

for n in cnt_dic.values():
    sum_cnt += int( n * (n-1) / 2)

print(sum_cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
s = [input() for _ in range(N)]
 
# N=10^5より、O(N)で解く
# ソート（さらに扱いやすくするため、文字列に直す）することで、文字分布を得る
for i in range(N):
  s[i] = ''.join(sorted(s[i]))

# 文字分布が同じになる組み合わせの個数を求めればよい
# 各文字分布の出現個数nに対して、nC2の和を取る
s = Counter(s).most_common()
ans = 0
for p in s:
  ans += p[1] * (p[1]-1) // 2
  
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter 
N = int(input())
s = [input() for _ in range(N)]
 
# N=10^5より、O(N)で解く
for i in range(N):
  s[i] = sorted(s[i])
  s[i] = ''.join(s[i])

s = Counter(s).most_common()
ans = 0
for t in s:
  ans += t[1]*(t[1]-1)//2
  
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
lis = {}
counter = 0
N = int(input().rstrip())
for i in range(N):
  str = "".join(sorted(list(input().rstrip())))
  if str in lis:
    lis[str] += 1
    counter += lis[str]
  else:
    lis[str] = 0
print(counter)

Submission status: Accepted

Solution code:
n=int(input())
mozi=[]
for i in range(n):
    mozi.append(sorted(input()))
mozi.sort()

def hantei(x,y):
    for i in range(10):
        if x[i]!=y[i]:
            return False
    return True

def conbi(n,r):
    ue=1
    for i in range(n-r+1,n+1):
        ue*=i
 
    sita=1
 
    for i in range(1,r+1):
        sita*=i
 
 
    return (ue//sita)
i=0
ans=0
while i<n-1:
    c=1
    while i<n-1 and hantei(mozi[i],mozi[i+1]):
        c+=1
        i+=1
    ans+=conbi(c,2)
    i+=1

print(ans)

Submission status: Accepted

Solution code:
import math

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

for i in range(N):
    l = ''.join(sorted(input()))
    records.setdefault(l, 0)
    records[l] += 1

count = 0
for v in records.values():
    if v == 1:
        continue
    else:
        count += math.comb(v, 2)

print(count)

Submission status: Accepted

Solution code:
N = int(input())
d = dict()
for i in range(N):
    S = input()
    S = ''.join(sorted(list(S)))
    if S in d.keys():
        d[S] += 1
    else:
        d[S] = 1

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
S = sorted([sorted(input()) for _ in range(N)])
ans = 0
pre = '_'
precnt = 0
for s in S:
    if s == pre:
        precnt += 1
        ans += precnt
    else:
        pre = s
        precnt = 0
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 10 00:32:19 2020

@author: liang
"""

N = int(input())
d = dict()
for i in range(N):
    S = input()
    S = ''.join(sorted(list(S)))
    #print(S)
    if S in d.keys():
        d[S] += 1
    else:
        d[S] = 1

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

Submission status: Accepted

Solution code:
n = int(input())

count = 0
sn = {}
for i in range(n):
  s = list(input())
  check = "".join(sorted(s)) 
  if not check in sn:
    sn[check] = 1
  else :
    count += sn[check]
    sn[check] += 1
    
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)
print(sum([i*(i-1)//2 for i in c.values()]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
import decimal
import itertools
from itertools import product
from functools import reduce
def input():
    return sys.stdin.readline()[:-1]
def sort_zip(a:list, b:list):
    z = zip(a, b)
    z = sorted(z)
    a, b = zip(*z)
    a = list(a)
    b = list(b)
    return a, b

def sort_str(t):
    l = sorted(t)
    text = ''
    for i in range(len(t)):
        text += l[i]
    return text

def sum_stair(n):
    num = 0
    for i in range(1, n + 1):
        num += i
    return num

def main():
    N = int(input())
    s = [sort_str(input()) for _ in range(N)]

    t = {}
    for i in s:
        t[i] = 0
    for i in s:
        t[i] += 1
    ans = 0
    for i in t.values():
        ans += sum_stair(i - 1)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
N = int(input())
s = [sorted(input()) for i in range(N)]
s.sort()
ans = 0
for i in range(N-1):
    if s[i] == s[i+1]:
        start = i
        end = N-1
        while start != end:
            center = (start + end) // 2
            if s[center] == s[i]:
                if s[center+1] == s[i]:
                    start = center+1
                else:
                    start = end = center
            else:
                end = center
        ans += start - i
print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N = int(input())
C = counter()

def f(s):
    ans = 0
    for i in range(10):
        ans += 11**ston(s[i])
    return ans

ans = 0
for _ in range(N):
    s = input()
    k = f(s)
    C.add(k)
    ans += C[k]-1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [list(map(str, input())) for _ in range(N)]
S.sort()
for s in S:
    s.sort()

ans = 0
dic = {}

for s in S:
    ss = "".join(s)
    if ss in dic:
        ans += dic[ss]
        dic[ss] += 1
    else:
        dic[ss] = 1

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
S = [list(map(str, input())) for _ in range(N)]
S.sort()
for s in S:
    s.sort()

ans = 0

dic = {}

SS = []

for s in S:
    SS.append("".join(s))

for i in range(N):
    if SS[i] in dic:
        dic[SS[i]] += 1
    else:
        dic[SS[i]] = 1

for key in dic:
    if dic[key] > 1:
        ans += dic[key] * (dic[key] - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = [sorted(input()) for _ in range(N)]
Ssrt = []
for s in S:
    s = ''.join(s)
    Ssrt.append(s)
cnt = collections.Counter(Ssrt)
ans = 0
for i in cnt.keys():
    if cnt[i] >= 2:
        ans += cnt[i]*(cnt[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = []
for i in range(N):
    s = input()
    S = [s[x] for x in range(10)]
    S = sorted(S)
    s = ""
    for j in range(10):
        s += S[j]
    l.append(s)


from collections import Counter

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
ss = [input() for _ in range(n)]
count = []
l = [chr(v) for v in range(ord("a"), ord("z")+1)]
d = {c:i for i,c in enumerate(l)}
for s in ss:
    val = [0]*26
    for c in s:
        val[d[c]] += 1
    count.append(tuple(val))
from collections import Counter
c = Counter(count)
ans = sum(v*(v-1)//2 for v in c.values())
print(ans)

Submission status: Accepted

Solution code:
import collections
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N = ii()

    memo = collections.defaultdict(lambda: 0)
    for i in range(N):
        S = list(input())
        S.sort()
        memo["".join(S)] += 1
    
    ans = 0
    for _,count in memo.items():
        ans += count * (count-1) // 2

    print(ans)
        


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
s.sort()
C = collections.Counter(s)
D = C.most_common()

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

Submission status: Accepted

Solution code:
#57 C - Green Bin
import collections
N = int(input())
S = [sorted(input()) for _ in range(N)]
Ssrt = []
for s in S:
    s = ''.join(s)
    Ssrt.append(s)

cnt = collections.Counter(Ssrt)
ans = 0
for s in cnt.keys():
    if cnt[s] >= 2:
        ans += cnt[s]*(cnt[s]-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = []
c=0
for i in range(N):
    l.append(sorted(input()))
l.sort()

base =0
next = 1
ren = 1
while True:
    if l[base] == l[next]:
        ren += 1
        next += 1
    else:
        c += ren * (ren - 1) // 2
        base = next
        next += 1
        ren = 1
    if next == N:
        c += ren * (ren-1) // 2
        break
print(c)

Submission status: Accepted

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

for _ in range(N):
    s = ''.join(sorted(input(), key=str.lower))
    if s in cnt:
        cnt[s] += 1
    else:
        cnt[s] = 1

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

print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = {}
for _ in range(n):
  t = "".join(sorted(input()))
  if t not in s: s[t] = 1
  else: s[t] += 1

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    s = [''.join(sorted(list(SS()))) for _ in range(N)]

    cnt = collections.Counter(s)
    ans = sum([i * (i - 1) // 2 for i in cnt.values()])

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
from collections import defaultdict
dic = defaultdict(int)

for _ in range(N):
    s = list(input())
    s.sort()
    dic[''.join(s)] += 1

ans = 0
for n in dic.keys():
    ans += dic[n]*(dic[n]-1)//2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    cnt = Counter()
    ans = 0
    for i in range(n):
        s = "".join(sorted(input()))
        if cnt[s] == 0:
            cnt[s] = 1
        else:
            ans += cnt[s]
            cnt[s] += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


def combination(n, k):
    num = 1
    den = 1
    for i in range(k):
        num *= n-i
        den *= i+1
    return num//den


ct = 1
ans = 0
tmp = S[0]
for i in range(1, N):
    if S[i] == tmp:
        ct += 1
    else:
        if ct >= 2:
            ans += combination(ct, 2)
        ct = 1
        tmp = S[i]
if ct >= 2:
    ans += combination(ct, 2)

print(ans)

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline

class common_function():
    """
        1. よく使いそうで予め用意してあるものをまとめた
        2. よく使いそうな関数群をまとめた
    """
    def nCk(self, n:int, k:int):
        """
            mod を使用しない combination nCk を求めるメソッド
            1回 の nCk を求めるのに O(k) かかる.
        """
        k = min(k, n-k)
        numer = 1
        for i in range(n, n-k, -1):
            numer *= i
        denom = 1
        for i in range(k, 1, -1):
            denom *= i
        return numer // denom

def main():
    common = common_function()
    N = int(input())
    slist = []
    tlist = []
    for _ in range(N):
        s = list(input()[:-1])
        s.sort()
        slist.append(s)
    slist.sort()
    cnt = 1; tlist.append(cnt)
    for i in range(N-1):
        if slist[i] == slist[i+1]:
            tlist.append(cnt)
        else:
            cnt += 1
            tlist.append(cnt)
    tlistcount = list(collections.Counter(tlist).items())
    tlistcount.sort(key=lambda x: x[1], reverse=True)
    ans = 0
    for t0, t1 in tlistcount:
        if t1 == 1:
            break
        ans += common.nCk(t1, 2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict,Counter

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

for s in S:
  cur = sorted(s)
  cur = "".join(map(str,cur))
  D[cur] += 1
cnt = 0
for c in D.values():
  cnt += c*(c-1)//2
ans = cnt
print(ans)

Submission status: Accepted

Solution code:
import math
def comb(n, r):
  if n-r>=0:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  else:
    return 0
    
n=int(input())
l=[]
for i in range(n):
  s=sorted(input())
  l.append(s)
l.sort()
cnt=1
ans=0

for i in range(n-1):
  if l[i]==l[i+1]:
    cnt+=1
  else:
    ans+=comb(cnt,2)
    cnt=1
    
ans+=comb(cnt,2)    
    
print(ans)

Submission status: Accepted

Solution code:
import math

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

n = int(input())
d = dict()
for _ in range(n):
    s = "".join(sorted([c for c in input()]))
    d[s] = d.get(s, 0) + 1

ans = 0
for v in d.values():
    if v >= 2:
        ans += c(v, 2)
print(ans)

Submission status: Accepted

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


from collections import Counter
L = Counter(L)

def num(n):
    return n*(n-1)//2
    
cnt = 0  
for v in L.values():
    cnt +=num(v)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

# 入力 (各文字列をソートしておく)
N = int(input())
S = [''.join(sorted(input())) for _ in range(N)]

# Counter でヒストグラムに
num = Counter(S)

# 各文字列ごとに個数 x を扱う
result = sum(x * (x - 1) // 2 for x in num.values())
print(result)

Submission status: Accepted

Solution code:
from collections import defaultdict

dd = defaultdict(int)
n = int(input())
ans = 0
for _ in range(n):
    m = list(input())
    m.sort()
    tmp = "".join(m)
    ans += dd[tmp]
    dd[tmp] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = {}
res = 0

for i in range(N):
  A = list(input())
  A.sort()
  if str(A) in D:
    res += D[str(A)]
    D[str(A)] += 1
  else:
    D[str(A)] = 1
    
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

c = collections.Counter(abc)
cnt = 0

for i in c:
  num = c[i]
  if num != 1:
    cnt += num*(num-1) // 2
 
print(cnt)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=[]
a=0

for i in range(n):
    b=list(input())
    b.sort()
    b=str(b)
    s.append(b)

c=collections.Counter(s)

for i in c.values():
    if i>1:
        a+=i*(i-1)//2

print(a)

Submission status: Accepted

Solution code:
N=int(input())
ans=0
li=[]
for i in range(N):
    s=list(input())
    s.sort()
    li.append(s)
li.sort()
temp=1
for j in range(N-1):
    if li[j]==li[j+1]:
        temp+=1
    else:
        ans+=temp*(temp-1)//2
        temp=1
        
ans+=temp*(temp-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
import math


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()

z = {}
ans = 0

for i in range(n):
    s = "".join(sorted(input()))
    try:
        z[s] += 1
    except:
        z[s] = 1

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

print(ans)

Submission status: Accepted

Solution code:
import collections as c

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

for i in range(n):
    t = list(input())
    t.sort()
    t = str(t)
    s.append(t)

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

Submission status: Accepted

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

Submission status: Accepted

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


def read():
    N = int(input().strip())
    S = []
    for i in range(N):
        S.append(input().strip())
    return N, S


def solve(N, S):
    words = defaultdict(int)
    for s in S:
        key = "".join(list(sorted(s)))
        words[key] += 1
    
    ans = 0
    for key in words:
        n = words[key]
        if n >= 2:
            ans += n * (n-1) // 2
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
N = int(input())
S = [0]*N
for i in range(N):
    S[i] = ''.join(sorted(str(input())))
answer = 0
numdic = {}
for i in range(N):
    if S[i] in numdic:
        answer += numdic[S[i]]
        numdic[S[i]] +=1
        
    else:
        numdic[S[i]] = 1
    
print(answer)

Submission status: Accepted

Solution code:
from sys import stdin
import copy

n = int(input())
S = [stdin.readline()[:-1] for i in [0]*(n)]
#S.append(stdin.readline())
s = []
for i in S:
    s.append("".join(sorted(i)))
s.sort()
arr = []
temp = 1
for i in range(n-1):
    if temp>1 and s[i]!=s[i+1]:
        arr.append(temp)
        temp = 1
    if s[i]==s[i+1]:
        temp += 1
if temp>1:
    arr.append(temp)

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

Submission status: Accepted

Solution code:
N = int(input())
M = [input() for i in range(N)]
 
M_sorted = [''.join(sorted(m)) for m in M]
d = {}
ans = 0
 
for m in M_sorted :
    d.setdefault(m,0)
    ans += d[m]
    d[m] += 1
 
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

ans = 0
d = {}

for i in range(N):
    txt = "".join(sorted(input()))
    d.setdefault(txt,0)
    
    ans += d[txt]
    d[txt] += 1
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(sorted(input()))
A.sort()
ans = 0
c = 1
for i in range(N-1):
  if A[i] == A[i+1]:
    c += 1
  else:
    ans += c * (c-1) // 2
    c = 1
ans += c * (c-1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    k = int(input())
    a = sorted(["".join(sorted(input())) for _ in range(k)])
    d = Counter(a)
    return int(sum(val*(val-1)/2 for val in d.values()))



print(main())

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, Ss):
    for i in range(N):
        Ss[i] = ''.join(sorted(list(Ss[i])))
    Ss.sort()
    ans = 0
    flg = False
    counter = 0
    for i in range(N - 1):
        if Ss[i] == Ss[i + 1]:
            if not flg:
                flg = True
                counter = i
        elif flg:
            ans += (i - counter + 1) * (i - counter) // 2
            flg = False
    if flg:
        ans += ((N - 1) - counter + 1) * ((N - 1) - counter) // 2
    print(ans)


if __name__ == '__main__':
    # S = input()
    N = int(input())
    # N, M = map(int, input().split())
    Ss = [input() for _ in range(N)]
    # As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(N, Ss)

Submission status: Accepted

Solution code:
n=int(input())
def moji(x):
    s=list(x)
    s.sort()
    return "".join(s)
li=[moji(input()) for i in range(n)]
li.sort()
k=1
res=0
for i in range(1,n):
    if li[i]==li[i-1]:
        k+=1
    else:
        res+=k*(k-1)//2
        k=1
res+=k*(k-1)//2
print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    strs = defaultdict(int)
    for _ in range(N):
        str = list(input().rstrip())
        strs[''.join(sorted(str))] += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

from collections import defaultdict

def main():
	n=II()
	d=defaultdict(int)
	for _ in range(n):
		s=list(RD())
		s.sort()
		d[tuple(s)]+=1

	ans=0

	for _,v in d.items():
		ans+=(v*(v-1))//2
	print(ans)









if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = [tuple(sorted(input())) for _ in range(n)]

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
s = [str(sorted(input())) for _ in range(N)]
c = Counter(s)

cnt = []
for i in c.values():
    cnt.append(i * (i - 1)//2)
print(sum(cnt))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
s = [str(sorted(input())) for _ in range(N) ]
c = Counter(s)
cnt = []

for i in c.values():
    cnt.append(i * (i -1)//2)
print(sum(cnt))