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())
counter = defaultdict(int)

for _ in range(N):
  s = input()
  alpha = [0] * 26
  a = ord("a")
  for c in s:
    alpha[ord(c)-a] += 1 
  counter[tuple(alpha)] += 1

res = 0
for k,v in counter.items():
  res +=v*(v-1)//2

print(res)

Submission status: Accepted

Solution code:
N = int(input())
s = {}
for _ in range(N):
    input_s = list(input())
    input_s = sorted(input_s)
    input_s = "".join(input_s)
    if input_s not in s.keys():
        s[input_s] = 0
    s[input_s] += 1
ans = 0
for key, value in s.items():
    if value >= 2:
        ans += value * (value - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(input())
    d = dict()

    for _ in range(N):
        s = str(sorted(input()))
        if s in d:
            d[s] += 1
        else:
            d[s] = 1

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

    print(ans)


if __name__ == '__main__':
    solve()

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())
string = {}
res = 0
for i in range(N):
  s = str(sorted(input()))
  if s in string:
    string[s] += 1
  else:
    string[s] = 0
for key in string:
  res += comb(string[key] + 1, 2)
print(res)

Submission status: Accepted

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


count = 0
hashset = {}

for i in range(len(S)):
    key = ''.join(sorted(S[i]))
    if key in hashset:
        count += hashset[key]
        hashset[key] += 1
    else:
        hashset[key] = 1

print(count)

Submission status: Accepted

Solution code:
# 754~
# 解法 801
# ソートO(10*log10)して，連想配列のkeyにソートされた文字列，valueにその文字列の出現回数 O(N)
import math
N = int(input())
S=[]
for _ in range(N):
    S.append(input())

dic = {}
for s in S:
    s = list(s)
    s.sort()
    s = ''.join(s)
    if not s in dic:
        dic[s] = 1
        continue
    dic[s] += 1

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

Submission status: Accepted

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

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

r=collections.Counter(A)
r=r.most_common()

cnt=0
for j in range(len(r)):
    if r[j][1]==1:
        cnt+=0
    else:
        cnt+=((r[j][1]-1)*r[j][1])//2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
import collections
d = collections.defaultdict(int)
ans = 0
for i in range(n):
    sl = sorted([j for j in input()])
    ss = ''.join(sl)
    ans += d[ss]
    d[ss] += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)

n=l()[0]
A=[list(input()) for i in range(n)]
d={}
for i in range(n):
    x=tuple(sorted(A[i]))
    if(x not in d):
        d[x]=0
    d[x]+=1
ans=0
for ks in d:
    ans+=(d[ks]*(d[ks]-1)//2)
print(ans)

Submission status: Accepted

Solution code:
import collections

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc137/tasks/abc137_c

from collections import defaultdict

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

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

ans = 0
for k, v in d.items():
    ans += nC2(v)
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from operator import mul
from functools import reduce
from collections import Counter

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


n = int(readline())
s = ["".join(sorted(list(readline().decode().rstrip()))) for _ in range(n)]

c = Counter(s)

l = [cmb(v,2) for k, v in c.items() if v >=2]
print(sum(l))

Submission status: Accepted

Solution code:
import collections

n = int(input())

lis = ["".join(sorted(input())) for _ in range(n)]
    
c = collections.Counter(lis)

ans = 0
for i in set(lis):
    t = c[i]
    ans += t * (t-1) //2
    
print(ans)

Submission status: Accepted

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

ans = 0
counter_list = []

for _ in range(N):
    s = input()
    counter_list.append("".join(sorted(s)))

ans = 0
wordcount = {}
for word in counter_list:
    if word in wordcount:
        wordcount[word] += 1
        ans += (wordcount[word])
    else:
        wordcount[word] = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
d = defaultdict(int)
for i in range(n):
  s = tuple(sorted(list(input())))
  d[s] += 1

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    S = [list(input().strip("\n")) for _ in range(N)]
    for i in range(N): S[i].sort()
    S.sort()
    S.append(["E", "N", "D"])

    ans = 0
    current = "".join(S[0])
    same = 0
    for i in range(1, N+1):
        tempS = "".join(S[i])
        if tempS == current:
            same += 1
        else:
            ans += (1 + same) * same // 2
            same = 0
            current = tempS
    print(ans)

    return  0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

from itertools import permutations
from math import factorial, hypot

#############
# Main Code #
#############

N = getN()
S = [''.join(sorted(list(input()))) for i in range(N)]
ans = 0
for i in Counter(S).items():
    ans += i[1] * (i[1] - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = []
count = 0
for i in range(N):
    S.append(sorted(input()))
S = sorted(S)
a = 'sing'
for j in range(N):
    if a != S[j]:
        f = 0
        a = S[j]
    else:
        f += 1
        count += f
print(count)

Submission status: Accepted

Solution code:
N = int(input())
memo = {}
for _ in range(N):
    si = sorted(list(input()))
    si = "".join(si)
    try:
        memo[si] += 1
    except:
        memo[si] = 1

# 組み合わせの数
import math
def combinations_count(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


ans = 0
for key in memo.keys():
    ans += combinations_count(memo[key],2)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = [''.join(sorted(list(input()[:-1].decode()))) for _ in range(n)]

    nc2 = [i * (i - 1) // 2 for i in range(10 ** 5 + 1)]

    summary = {}

    for si in s:
        if si in summary:
            summary[si] += 1
        else:
            summary[si] = 1

    ans = 0

    for si in summary:
        ans += nc2[summary[si]]

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
import collections
n ,res, cc = int(raw_input()),0,collections.Counter()
while(n):
	u =  tuple(sorted(raw_input()))
	res += cc[u]
	n-=1
	cc[u]+=1
print res

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = list(sorted([sorted(input()) for i in range(N)]))
mini_count = 0
count = 0
tmp = S[0]

for i in range(1, N):
    if tmp == S[i]:
        mini_count += 1
        count += mini_count
    else:
        tmp = S[i]
        mini_count = 0

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
dict={}
cnt=0
for a in range(N):
  s="".join(sorted(input()))
  if s in dict:
    dict[s]+=1
    cnt+=dict[s]
  else:
    dict[s]=0
print(cnt)

Submission status: Accepted

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

# main
ans = 0
cnt = 1
for i in range(n):
  if s[i] == s[i+1]:
    cnt += 1
  if cnt > 1 and s[i] != s[i+1]:
    ans += cnt * (cnt-1) // 2
    cnt = 1

# output
print(ans)

Submission status: Accepted

Solution code:
import itertools

n = int(input())

s = [0 for _ in range(n)]
in_a = [0 for _ in range(n)]
dic_tmp = {}
dic_ar = [0 for _ in range(n)]
for i in range(n):
    in_a[i] = input()

cnt = 0
for i in range(n):
    s = sorted(in_a[i])
    s = "".join(s)
    if(s in dic_tmp):
        cnt += dic_tmp[s]
        dic_tmp[s] += 1
    else:
        dic_tmp[s] = 1

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

S = [""]*N

for i in range(N):
    S[i] = input()
    
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
  
  
S_set = ["".join(sorted(s)) for s in S]


import collections
import itertools

same_list = collections.Counter(S_set)

ans = [0]*len(same_list)

for i,v in enumerate(same_list.values()):
    if i==0:
        if v==1:
            ans[i] = 0
        else:
            ans[i] = cmb(v, 2)
    else:
        if v==1:
            ans[i] = ans[i-1] + 0
        else:
            ans[i] = ans[i-1] + cmb(v, 2)

print(ans[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n=int(input())

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

B=collections.Counter(A)
C=list(B.values())

cnt=0
for i in C:
    cnt+=(i*(i-1))//2

print(cnt)

Submission status: Accepted

Solution code:

N = int(input())

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

c = collections.Counter(l)
#print(c)
issubclass(type(c), dict)
C = list(c.values())
#print(C)

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

Submission status: Accepted

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

c = {}
for i in s:
  x = ''.join(sorted(i))
  if x in c:
    c[x] += 1
  else:
    c[x] = 1

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

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=[""]*n
st=set()
d={}
for ii in range(n):
  ss=list(input())
  ss.sort()
  ss="".join(ss)
  if ss in st:
    d[ss]+=1
  else:
    d[ss]=1
    st.add(ss)

if len(st)==n:
  print(0)
  exit()
if len(st)==1:
  print((n*(n-1))//2)
  exit()

r=0
for sss in st:
  c=d[sss]
  if c>1:
    r+=(c*(c-1))//2
    
print(r)

Submission status: Accepted

Solution code:
n = int(input())
a = {}
ans = 0
for i in range(n):
    s = list(input())
    s.sort()
    S = ''.join(s)
    if S in a:
        a[S] += 1
    else:
        a[S] = 1

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

Submission status: Accepted

Solution code:
# ソートした状態で文字を読み込んで、アナグラムの文字と出てくる回数を取得する
# あるアナグラム文字がn回出てくるならば、ペアの選び方はnC2で、n*(n-1)/2
# 全てのアナグラム文字に関して足し合わせ
import collections

n = int(input())
# ソートして文字読み込み
s_sorted = [''.join(sorted(input())) for i in range(n)]

# アナグラム文字列と出現回数を取得
anagram_list = collections.Counter(s_sorted)

count = 0

# 出現回数のリストからペアの選び方を計算して、足し合わせ
for i in anagram_list.values():
    count += i*(i-1)//2

print(count)

Submission status: Accepted

Solution code:
n = int(input())
 
words = {}
anagrams = 0
 
for i in range(n):
    word = "".join(sorted(input()))
    if word not in words:
        words[word] = 1
    else:
        anagrams += words[word]
        words[word] += 1
 
print(anagrams)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

n = int(input())

d = {}

for i in range(n):
    t = input()
    t = ''.join(sorted(t))
    d[t] = d.get(t,0) + 1
        
ans = 0

for v in d.values():
    ans += v*(v-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)
ans = 0
for i in c.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

words = {}
anagrams = 0

for i in range(n):
    word = "".join(sorted(input()))
    if word not in words:
        words[word] = 1
    else:
        anagrams += words[word]
        words[word] += 1

print(anagrams)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ans=0
s=[[x for x in input()] for i in range(n)]

dic={}
for i in s:
  temp=tuple(sorted(i))
  if temp in dic:
    ans+=dic[temp]
    dic[temp]+=1
  else:
    dic[temp]=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
save = [-1]*(n+1)

def aC2(a):
    if(save[a]!=-1):
        return save[a]
    ans = 1
    for i in range(a-1,a+1):
        ans *= i
    ans //=2
    save[a] = ans
    return ans

dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if(dic.get(s) is None):
        dic[s] = 1
    else:
        dic[s] += 1

ans = 0
for x in dic.values():
    if(x==0 or x==1):
        continue
    ans += aC2(x)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
n = int(input())
s = [0]*n
a = [[0 for l in range(26)] for m in range(n)]
for i in range(n):
    s[i] = input()
for j in range(n):
    a[j][0] = s[j].count('a')
    a[j][1] = s[j].count('b')
    a[j][2] = s[j].count('c')
    a[j][3] = s[j].count('d')
    a[j][4] = s[j].count('e')
    a[j][5] = s[j].count('f')
    a[j][6] = s[j].count('g')
    a[j][7] = s[j].count('h')
    a[j][8] = s[j].count('i')
    a[j][9] = s[j].count('j')
    a[j][10] = s[j].count('k')
    a[j][11] = s[j].count('l')
    a[j][12] = s[j].count('m')
    a[j][13] = s[j].count('n')
    a[j][14] = s[j].count('o')
    a[j][15] = s[j].count('p')
    a[j][16] = s[j].count('q')
    a[j][17] = s[j].count('r')
    a[j][18] = s[j].count('s')
    a[j][19] = s[j].count('t')
    a[j][20] = s[j].count('u')
    a[j][21] = s[j].count('v')
    a[j][22] = s[j].count('w')
    a[j][23] = s[j].count('x')
    a[j][24] = s[j].count('y')
    a[j][25] = s[j].count('z')
counter = Counter(tuple(x) for x in a)
k = list(counter.values())
ans = 0
for y in k:
    ans += (y*(y-1))//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

num = {}

for i in range(n):
    seq = input()
    seq = ''.join(sorted(seq))
    if seq in num:
        num[seq] += 1
    else:
        num[seq] = 1

ans = 0
for s in num:
    k = num[s]
    if k > 1:
        ans += k * (k-1) // 2

print(ans)

Submission status: Accepted

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

hashtable = {}

for x in S:
    hashtable.setdefault(x, 0)
    hashtable[x] += 1

sumS = 0
for y in hashtable.values():
    sumS += y * (y - 1) // 2
print(sumS)

Submission status: Accepted

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

from math import factorial
def combi(n,r):
    return factorial(n)//(factorial(r)*factorial(n-r))
    
print(sum([combi(c,2) for c in counter.values() if c>1]))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#import numpy as np
#from decimal import *

N = INT()
s = ["".join(sorted(list(input()))) for _ in range(N)]

dic = defaultdict(int)
for x in s:
	dic[x] += 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#import numpy as np
#from decimal import *

N = INT()
s = [str(sorted(list(input()))) for _ in range(N)]

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

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = [''.join(sorted(list(input()))) for _ in range(N)]
C = collections.Counter(S)
count = 0
for key in C.keys():
    c = C[key]
    if c > 1:
        count += c*(c-1)//2
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = sorted([sorted(input()) for i in range(N)])

ans = 0
count = 1
pre = S[0]
for i in range(1, N):
    if S[i] != pre:
        ans += count*(count-1)//2
        count = 1
        pre = S[i]
    else:
        count += 1

ans += count*(count-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int (input())
s = []
cnt = [0]* n
for i in range(n):
    a = list(input())
    a.sort()
    s.append(''.join(a))
ans = 0
s.sort()
ren = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        ans+=1+ren
        ren +=1
    else:
        ren = 0
print(ans)

Submission status: Accepted

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

for i in range(N):
  S[i] = "".join(sorted(S[i]))

ans = {}
for i in range(N):
  if S[i] in ans:
    ans[S[i]] += 1
  else:
    ans[S[i]] = 1

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

Submission status: Accepted

Solution code:
n = int(input())
s = ["".join(sorted(list(input()))) for _ in range(n)]
ans = 0
d = {}
for x in s:
    if x not in d.keys():
        d[x] = 1
    else:
        ans += d[x]
        d[x] += 1
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])
C = Counter(S)
ans = 0
for i in C.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N = int(input())
S = [list(input().rstrip()) for _ in range(N)]

from collections import defaultdict
d = defaultdict(int)

for s in S:
    s.sort()
    d["".join(s)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

N = int(input().rstrip())
S = []
for s in range(N):
    S.append(sorted(input().rstrip()))
S = sorted(S)
ans = 0
ren = 1
for i in range(N-1):
    if S[i] == S[i+1]:
        ans += ren
        ren += 1
    else:
        ren = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


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

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

    print(int(s))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

n = int(input())
s_list = [input() for _ in range(n)]
count = 0
strdic = {}
for s in s_list:
    s = sorted(s)
    s = ''.join(s)
    if s in strdic:
        # 同じものがstrdic[s]回出てるってことだから
        # 答えにstrdic[s]をたす
        count += strdic[s]
        strdic[s] += 1
    else:
        strdic[s] = 1
print(count)

Submission status: Accepted

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

dic = {}
ans = 0

for i in range(N):
    if not s[i] in dic:
        dic[s[i]] = 0
    dic[s[i]] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append(''.join(sorted(list(input()))))
S.sort()
#print('S', S)

count=0
n=1
for i in range(1,N):
    if S[i-1] == S[i]:
        n+=1
    else:
        count+= n*(n-1)*0.5
        n=1
count+=n*(n-1)*0.5
print(int(count))

Submission status: Accepted

Solution code:
import sys
from scipy.misc import comb

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

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

check = ''
cnt = 1
ans = 0
for s in sorted(str_list):
    if s == check:
        cnt += 1
    else:
        if cnt >= 2:
            ans += comb(cnt, 2, exact=True)
        cnt = 1
    check = s
if cnt >= 2:
    ans += comb(cnt, 2, exact=True)
print(ans)

Submission status: Accepted

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    _ = int(readline())
    s_list = [''.join(sorted(s.rstrip().decode())) for s in readlines()]
    counter = collections.Counter(s_list)
    ans = 0
    for v in counter.values():
        if v != 1:
            ans += v * (v - 1) // 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
dict = defaultdict(int)
ans = 0
for i in range(N):
    s = ''.join(sorted(input()))
    ans += dict[s]
    dict[s] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for _ in range(n)]
new_s = []
cnt = {}
ans = 0

for i in s:
    new_s.append(str(sorted(i)))

for i in new_s:
    if i in cnt.keys():
        cnt[i] += 1
    else:
        cnt[i] = 1
for v in cnt.values():
    ans += v * (v-1) // 2

print(ans)

Submission status: Accepted

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

for _ in range(N):
    s = "".join(sorted(input()))
    try:
        ans += unique_string[s]
        unique_string[s] += 1
    except KeyError:
        unique_string[s] = 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
s=s2(n)
d=defaultdict(int)

for line in s:
    d["".join(sorted(line))]+=1

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

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
s=s2(n)
d=defaultdict(int)

for line in s:
    a=[0]*26
    c=set((Counter(line)).items())
    for t in c:
        a[ord(t[0])-97]=t[1]
    d[tuple(a)]+=1

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

Submission status: Accepted

Solution code:
N = int(input())
s = ["".join(sorted(input())) for i in range(N)]
D = dict()
for st in s:
    if st in D:
        D[st] += 1
    else:
        D[st] = 1
ans = 0
for i in D:
    k = D[i]
    ans += (k*(k-1))//2
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
s=[input() for i in range(N)]
S=[]
ans=0
for i in s:
  a=sorted(i)
  a="".join(a)
  S.append(a)
c=collections.Counter(S)
d=list(map(int,c.values()))
for i in d:
  ans+=i*(i-1)/2
print(int(ans))