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:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    ans=0
    tc=Counter()
    for _ in range(N):
        s=input()
        c=Counter(s)
        l=tuple(c[num2alp(i)] for i in range(26))

        tc[l]+=1
    for val in tc.values():
        ans+=val*(val-1)//2
    print(ans)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit
from collections import Counter

# input = stdin.readline

# @njit
def solve(n,l):
  l_dash = ["".join(sorted(s)) for s in l]
  d = Counter(l_dash)
  return sum(v*(v-1)//2 for v in d.values())


def main():
  N = int(input())
  l = [input() for _ in range(N)]
  print(solve(N,l))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

c = Counter(s)
ans = 0

for i in c.values():
    #i!/(i-2)!/2!
    ans += int(i*(i-1)/2)

print(ans)

Submission status: Accepted

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

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


dic = {}
for i in moji:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

ans = 0
for value in dic.values():
    if value >=2:
        ans+=C(value, 2)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

print(ans)

Submission status: Accepted

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

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


dic = {}
for i in moji:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

ans = 0
for value in dic.values():
    if value >=2:
        a= C(value, 2)
        ans+=a

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial


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


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

ans = 0
for i in list(S_c.values()):
    if i != 1:
        ans += combinations_count(i, 2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
count_dict = dict()
for _ in range(n):
    s = input()
    s = ''.join(sorted(s))
    count_dict.setdefault(s, 0)
    count_dict[s] += 1

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

Submission status: Accepted

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

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

a = collections.Counter(s)

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
B = collections.Counter(s)
print(sum(i*(i-1)//2 for i in B.values()))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = []
for i in range(N):
    s = sorted(input())
    A.append(str(s))
B = collections.Counter(A)
print(sum(i*(i-1)//2 for i in B.values()))

Submission status: Accepted

Solution code:
N = int(input())
# sはそれぞれ10文字
s = [str(sorted(input())) for i in range(N)]

# 組み合わせ総数
ans = 0
# 既に見た文字列 : 累計出現回数
cnt = {}

for i in range(N):
    # 既に見た文字列の場合
    if s[i] in cnt:
        ans += cnt[s[i]]
        cnt[s[i]] += 1
    # 始めて見る文字列
    # 辞書に1を入れておく
    else:
        cnt[s[i]] = 1

print(ans)

Submission status: Accepted

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

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

listcount = collections.Counter(s)

print(sum((i-1)*i//2 for i in listcount.values()))

Submission status: Accepted

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

ans=0
cnt={}

for i in range(N):
    if s[i] in cnt:
        ans+=cnt[s[i]]
        cnt[s[i]]+=1
    else:
        cnt[s[i]]=1
        
print(ans)

Submission status: Accepted

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

counter={}
ans=0
for i in range(n):
	ss = sorted(s[i])
	ss=("").join(ss)
	if ss not in counter:
		counter[ss]=0
	else:
		counter[ss]+=1
		ans+=counter[ss]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = [list(input()) for _ in range(n)]
for i in range(n):
    s[i].sort()
    s[i] = ''.join(s[i])
counter = Counter(s)
ans = 0
for val in counter.values():
    ans += val * (val - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
#文字列を同一か判別するために全てsortして格納する
#インプットした文字列をソートして文字列として格納する
s = [''.join(sorted(input())) for _ in range(n)]
#babacaba >>> aaaabbbcとする
######################
#同一性を判定するのにはハッシュテーブルを活用すると良い
#辞書はハッシュテーブルとして高速なために使用
s_dict = {}
for i in range(n):
	if s[i] in s_dict:
		s_dict[s[i]] += 1
	else:
		s_dict[s[i]] = 1
		#辞書内になくとも自動で追加される
		#>>>{xxx:1 yyy:1 zzz:1}
ans = 0
#辞書内の加算された数字についてnCrで2つの組み合わせを検討する
for j in s_dict.values():
	ans += j*(j-1)//2
	#xxxについて、2以上あれば1組は存在するということで+
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    counter = cl.Counter()
    for i in range(N):
        s = input()
        S = []

        for char in s:
            S.append(char)
        S.sort()
        
        s = "".join(S)
        counter.update({s:1})
    ans = 0

    for item, count in counter.items():
        ans += count * (count - 1) / 2
    print(int(ans))

main()

Submission status: Accepted

Solution code:
n = int(input())
S = [input() for _ in range(n)]
ans = 0
c = {}

for s in S:
    s = "".join(sorted(s))
    if s not in c.keys():
        c[s] = 1
    else:
        ans += c[s]
        c[s] += 1


print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
j=0
L=list()
R=list()
for i in range(N):
  S="".join(sorted(list(input())))
  L.append(S)
c = collections.Counter(L)
counts=c.values()
counts=list(counts)
A=[i*(i-1)//2 for i in counts]
print(sum(A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from scipy.special import comb

n = int(input())
d = defaultdict(int)
for _ in range(n):
    d[''.join(sorted((list(input()))))] += 1

print(sum([comb(i, 2, exact=True) for i in d.values()]))

Submission status: Accepted

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

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

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

count = 0
for value in results.values():
  count += choose2(value)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().rstrip()


def main():
    n=int(input())
    wlist=[]
    for _ in range(n):
        n=input()
        n=list(n)
        n.sort()
        n=''.join(n)
        wlist.append(n)
    cnt=Counter(wlist)
    ans=0
    for c in cnt.values():
        ans+=c*(c-1)//2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline())

result = {}


def comb(n, r):
    up = 1
    down = 1
    for i in range(r):
        up *= n - i
        down *= i + 1
    return up // down


for _ in range(N):
    text = ''.join(sorted(stdin.readline().rstrip()))

    if text in result:
        result[text] += 1
    else:
        result[text] = 1

answer = 0
for v in result.values():
    answer += comb(v, 2)

print(answer)

Submission status: Accepted

Solution code:
import math 
import collections

N = int(input())
String = []
for i in range (0, N):
	S = str(input())
	S = sorted(S)
	V = str(S[0])
	for j in range (1, 10):
		V = V+str(S[j])
	String.append(V)
    
cnt = collections.Counter()
for word in String:
	cnt[word] += 1

V = cnt.most_common()

count = 0

for i in range (0, len(V)):
	count+=math.comb(V[i][1], 2)
    
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
sl = []
for i in range(n):
    s = list(input())
    s.sort()
    sl.append("".join(s))
d = Counter(sl)

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

ans = 0
val = d.values()
for i in val:
    if i > 1:
        ans += cul(i)
print(ans)

Submission status: Accepted

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


for i in range(n):
    line = input()
    line = sorted(line)
    line = str(line)
    if line not in anaglram_map:
        anaglram_map[line] = 1
    else:
        anaglram_map[line] += 1

couter = 0
for value in anaglram_map.values():
    if value >= 2:
        couter += value * (value - 1) // 2
print(couter)

Submission status: Accepted

Solution code:
import collections
n = int(input())
S = collections.defaultdict(int)
for _ in range(n):
    s = ''.join(sorted(input()))
    S[s] += 1
cnt = 0
for key,val in S.items():
    if val >1:
        cnt += val*(val-1)//2
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
dict={}
for _ in range(int(input())):
  s=''.join(sorted(input()))
  if s not in dict: dict[s]=1
  else: dict[s]+=1
print(sum([i*(i-1)//2 for i in dict.values()]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
L = ["".join(sorted(input())) for _ in range(N)]
T = Counter(L).values()
print(sum(v*(v-1)//2 for v in T))

Submission status: Accepted

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

for i in s:
    key = ''.join(sorted(i))
    sd.setdefault(key, 0)
    sd[key] += 1
    
for v in sd.values():
    ans += v * (v - 1) // 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
str_lis = ['']*N
ans_dic = defaultdict(int)
for i in range(N):
    tmp = S()
    tmp = tuple(sorted(tmp))
    ans_dic[tmp] += 1
ans = 0
for v in ans_dic.values():
    ans += comb(v, 2, exact=True)

print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

def getHyoujunn(s):
    tmp = sorted(collections.Counter(s).items(), key=lambda x: x[0])
    ret = []
    for k,v in tmp:
        for i in range(v):
            ret.append(k)
    return ''.join(ret)

n = ir()
s_list = []
for i in range(n):
    s = sr()
    s_list.append(getHyoujunn(s))
s_list.sort()
ans = []
pre = ''
add = 1
for c in s_list:
    if c == pre:
        add+=1
    else:
        ans.append(add)
        add=1
        pre = c
ans.append(add)
ansnum = 0
for num in ans:
    if num != 1:
        ansnum+=((num-1)*num)//2
print(ansnum)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
lis=[]
def cnt(n):
  ret=0
  for i in range(1,n):
    ret+=i
  return ret
for i in range(n):
  lis.append(sorted(list(input())))
co=1
lis.sort()
bef=lis[0]

for i in lis[1:]:
  if bef!=i:
    ans+=cnt(co)
    co=1
    bef=i
  else:
    co+=1
ans+=cnt(co)
print(ans)

Submission status: Accepted

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

base = []
for i in s:
    base.append(''.join(sorted(i)))
    
base_dict = {}
for i in base:
    if i not in base_dict:
        base_dict[i] = 1
    else:
        base_dict[i] += 1

ans = 0
for key, value in base_dict.items():
    temp = 0
    if value != 1:
        temp = math.factorial(value) // (math.factorial(2) * math.factorial(value-2))
    ans += temp
    
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
lis=[''.join(sorted(input(), key=str.upper)) for _ in range(N)]
v=collections.Counter(lis).values()
ans=sum(list(map(lambda x: x*(x-1)//2, v)))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def readinput():
    n=int(input())
    s=[]
    for _ in range(n):
        s.append(input())
    return n,s

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

def main(n,s):
    count={}
    for word in s:
        sword=''.join(sorted(word))
        if sword in count:
            count[sword]+=1
        else:
            count[sword]=1
    ans=0
    for k,v in count.items():
        if v>1:
            ans+=cmb(v,2)        

    return ans

if __name__=='__main__':
    n,s=readinput()
    ans=main(n,s)
    print(ans)

Submission status: Accepted

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

count = Counter(s)

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

Submission status: Accepted

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

count = Counter(s)

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

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()

N = I()
s = [''.join(sorted(S())) for i in range(N)]

from collections import Counter

c = Counter(s)
ans = 0
for i in c.keys():
    ans += c[i]*(c[i]-1)//2

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = {}
ans = 0

for i in range(n):
    s = list(input())
    s.sort()
    s_s = ''.join(s)
    if s_s in S:
        ans += S[s_s]
        S[s_s] += 1
    else:
        S[s_s] = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

SUM = 0
for val in count.values():
  if val > 1:SUM+=val*(val-1)//2
print(SUM)

Submission status: Accepted

Solution code:
def f(S):
    L=list(S)
    L.sort()
    return  "".join(L)

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

for _ in range(N):
    T=f(input())
    if T in D:
        D[T]+=1
    else:
        D[T]=1

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict

N=int(input())
count = defaultdict(int)
for i in range(N):
  s=Counter(list(input()))
  count[str(sorted(s.items()))] += 1

count = list(count.values())
SUM = 0
for c in count:
  if c>1:SUM += c*(c-1)//2
print(SUM)

Submission status: Accepted

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

counter = collections.Counter(lsS)
n=0
for i in counter.values():
    n+= (i*(i-1))//2
print(n)

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import mul
from functools import reduce
def C(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())
d = defaultdict(int)
for _ in range(N):
    s = input()
    s = tuple(sorted(list(s)))
    d[s] += 1
ans = 0
for v in d.values():
    if v > 1:
        ans += C(v, 2)
print(ans)

Submission status: Accepted

Solution code:
# ソートしてカウント
# (2以上の場合、)個数のn*(n-1)/2

n = int(input())
d = dict()
for i in range(n):
    s = list(input())
    s.sort()
    sl = ''.join(s)
    if d.get(sl) == None:
        d[sl] = 1
    else:
        d[sl] = d[sl] + 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for n in range(N):
    s[n].sort()
s.sort()

cnt=[]
i=0
for n in range(1,N):
    if s[n]==s[n-1]:
        i+=1
    else:
        cnt.append(i)
        i=0
if i!=0:
    cnt.append(i)

ans=0
for e in cnt:
    ans+=(e+1)*e//2
print(ans)

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

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

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

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

print(sum([comb(i, 2) for i in d.values()]))

Submission status: Accepted

Solution code:
from scipy.special import comb
N = int(input())
a = [input() for _ in range(N)]

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

print(sum([comb(i, 2, exact=True) for i in d.values()]))

Submission status: Accepted

Solution code:
N = int(input())
Dic = {}
ans = 0
for _ in range(N):
  st = input()
  st=''.join(sorted(st))
  if st not in Dic:
    Dic[st]=0
  else:
    Dic[st]+=1
    ans+=Dic[st]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(n):
    a=input()
    b=[]
    for j in range(10):
        b.append(a[j])
    b.sort()
    c="".join(b)
    s.append(c)
s.sort()
ans=0
t=1
for i in range(1,n):
    if s[i]==s[i-1]:
        ans+=t
        t+=1
    else:
        t=1
print(ans)

Submission status: Accepted

Solution code:
N = 3
SRR = [
    "acornistnt",
    "peanutbomb",
    "constraint",
]

N = 5
SRR = [
    "abaaaaaaaa",
    "oneplustwo",
    "aaaaaaaaba",
    "twoplusone",
    "aaaabaaaaa",
]

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

for i in range(N):
    SRR.append(input())

def calculate(n,arr):
    dict = {}
    for i in range(n):
        str = "".join(sorted(list(arr[i])))
        if dict.get(str) == None:
            dict.__setitem__(str,1)
        else:
            dict.__setitem__(str,dict.get(str) + 1)

    result = 0

    for item in dict.values():
        if item > 1:
            result += (item * (item-1)) // 2

    print(result)


calculate(N, SRR)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    import math
    N = int(input())
    Ss = [sorted([item for item in input()]) for _ in range(N)]
    
    sss = []
    for line in Ss:
        sss.append(''.join(line))
    line_cnt = collections.Counter(sss)

    cnt = 0
    for letter, num in line_cnt.most_common():
        if num >=2:
            cnt += math.comb(num, 2)

    print(cnt)
if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

# In[12]:


import collections


# In[10]:


N = int(input())
s = []
for _ in range(N):
    s.append(input())


# In[30]:


ans = 0
mydict = {}
for i in range(N):
    tmp = "".join(sorted(s[i]))
    if tmp not in mydict:
        mydict[tmp] = 1
    else:
        ans += mydict[tmp]
        mydict[tmp] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

count_by_kw = Counter()

n=int(input())
l=[]
dict={}
ans=0
for x in range(n):
  a=sorted(input())
  b=''.join(a)
  if not b in dict:
    dict[b]=x
    l.append(b)
    count_by_kw[b]+=1
  else:
    d=count_by_kw[b]
    count_by_kw[b]+=1
    
for y in range(len(l)):
  r=l[y]
  w=count_by_kw[r]
  ans+=w*(w-1)//2

print(ans)

Submission status: Accepted

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

for _ in range(N):
    s = sorted(input())
    tmp = ''.join(s)
    if tmp in dic:
        dic[tmp] += 1
    else:
        dic[tmp] = 1

ans = 0

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

print(int(ans))

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

from collections import Counter

def resolve():
    n = int(input())
    l = [input() for _ in range(n)]
    cnt = 0
    ee = dict()

    for ll in l:
        tmp = ''.join(sorted(ll))

        if tmp in ee.keys():
            cnt += ee[tmp]
            ee[tmp] += 1
        else:
            ee[tmp] = 1

    print(cnt)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
acornistnt
peanutbomb
constraint"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
oneplustwo
ninemodsix"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa"""
        output = """4"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
d={}
for i in range(n):
    s=sorted(input())
    if str(s) in d:
        d[str(s)]+=1
    else:
        d[str(s)]=1
s=0
for c in d:
    if d[c]>1:
        s+=d[c]*(d[c]-1)//2
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    cnt = defaultdict(int)
    for _ in range(n):
        s = [x for x in input()]
        s.sort()
        s = ''.join(s)
        cnt[s] += 1
    ans = 0
    for k in cnt:
        n = cnt[k]
        ans += n * (n - 1) // 2
    print(ans)

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

abc

"""

Submission status: Accepted

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

c = collections.Counter(SS)

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

print(count)