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:
N = int(input())
anagram = {}

ans = 0
for i in range(N):
    tmp = str(sorted(input()))
    if tmp in anagram:
        ans += anagram[tmp]
        anagram[tmp] += 1
    else:
        anagram[tmp] = 1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = []
ans = 0
count = 0

for i in range(N):
    line = sorted([x for x in stdin.readline().rstrip()])
    line = ("".join(line))
    S.append(line)

S.sort()
for i in range(N):
    if (i == 0) or (S[i] == S[i - 1]):
        count += 1
    else:
        ans += count*(count - 1)//2
        count = 1
ans += count*(count - 1)//2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter
a=int(input())
c=[0]*a
d=[0]*10
x=0
y=a
z=0
for i in range(a):
	e=input()
	for j in range(10):
		d[j]=e[j]
	d.sort()
	c[i]=d[0]+d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9]

mycounter = Counter(c)
lst=[]
for e in mycounter.items():
    lst.extend(e)
for t in range(1,len(lst),2):
	k=lst[t]
	z=int(z+(k*(k-1)/2))
print(z)

Submission status: Accepted

Solution code:
N = int(input())
S = []
ans = 0
for i in range(N):
    tmp = list(input())
    tmp.sort()
    tmp = "".join(tmp)
    S.append(tmp)

S.sort()

cnt = 0
ans = 0

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    tmp = str(sorted(input()))
    if tmp in anagram:
        ans += anagram[tmp]
        anagram[tmp] += 1
    else:
        anagram[tmp] = 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = {}
for i in range(N):
  s = input()
  s = ''.join(sorted(s))
  if s in l:
    l[s] += 1
  else:
    l[s] = 0
ans = 0
value_l = [1]
for value in l.values():
  if value == 0:
    continue
  if len(value_l) >= value:
    tmp = value_l[value - 1]
  else:
    l_val = len(value_l)
    for i in range(l_val, value):
      value_l.append(value_l[i-1] + i + 1)
    tmp = value_l[value - 1]
  ans += tmp

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
for i in range(n):
    s[i] = sorted(s[i])
    s[i] = "".join(s[i])
    
memo = {}

for i in range(n):
    if s[i] not in memo:
        memo[s[i]] = 1
    else:
        memo[s[i]] += 1
ans = 0
for num in memo:
    num = memo[num]
    ans += num * (num - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
counts = []
for _ in range(n):
    s = list(input())
    s.sort()
    counts.append("".join(s))
ans = 0
c = collections.Counter(counts)
for key in c.keys():
    if c[key] >= 2:
        ans += (c[key]-1)*c[key]//2
print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
S=[input() for i in range(N)]
ans=[]
count = 0
cnt=0
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
"""
def anagram(a,b):
    A=[]
    B=[]
    for i in range(10):
        A.append(a[i])
        B.append(b[i])
    A.sort()
    B.sort()
    if A==B:
        return "Yes" """
for i in range(N):
    kari=[]
    for j in range(10):
        kari.append(S[i][j])
    else:
        kari.sort()
        ans.append(kari)
ans.sort()
kari=[]
for i in range(N-1):
    if ans[i]==ans[i+1]:
        count+=1
    else:
        kari.append(count+1)
        count=0
else:
    kari.append(count+1)
#print(kari)
for j in range(len(kari)):
    if kari[j]>=2:
        cnt+=comb(kari[j],2)
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
d=dict()
ans=0
for _ in range(N):
  s=input()
  s=tuple(sorted(s))
  if s in d:
    d[s]+=1
  else:
    d[s]=0
for i in d.values():
  ans+=(int(i)**2+int(i))/2
print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
l=[list(input()) for _ in range(n)]
dic_l={}
cnt=0
ans_lst=[]
for i in range(n):
  l[i].sort()
  l[i]=str(l[i])
  if l[i] not in dic_l:
    dic_l[l[i]]=1
  else:
    dic_l[l[i]]+=1
for j in dic_l.values():
  if j>=2:
    ans_lst.append(j)
    cnt += int(j*(j-1)/2)
print(cnt)

Submission status: Accepted

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

n=int(input())
l=[list(input()) for _ in range(n)]
dic_l={}
cnt=0
ans_lst=[]
for i in range(n):
  l[i].sort()
  l[i]=str(l[i])
  if l[i] not in dic_l:
    dic_l[l[i]]=1
  else:
    dic_l[l[i]]+=1
for j in dic_l.values():
  if j>=2:
    ans_lst.append(j)
    cnt += comb(j,2)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
words = dict()
count = 0
for i in range(N):
    s = ''.join(sorted(input()))
    if s in words:
        count += words[s]
        words[s] += 1
    else:
        words[s] = 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())    
S = [0]*N
for i in range(N):
    t = list(input())
    t.sort()
    t_sort = "".join(t)
    S[i] = t_sort

count = 0
result={}
for i in range (0,len(S)):
    item = S[i]
    if item in result:
        count += result[item]
        result[item] = result[item]+1
    else:
        result[item] = 1
    
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [sorted(input()) for _ in range(N)]
a = [''.join(i) for i in A]
c = Counter(a)
cnt = 0
for j in c.values():
    cnt += j*(j-1)//2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
words = dict()
count = 0
for i in range(n):
  s = ''.join(sorted(input()))
  if s in words:
    count += words[s]
    words[s] += 1
  else:
    words[s] = 1
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict as dd
N = int(input())
a = [input() for _ in range(N)]
dic=dd(int)
for i,elem in enumerate(a):
    a[i] = tuple(sorted(elem))
    dic[a[i]] += 1
res = sum([val*(val-1)//2 for val in dic.values()])
print(res)

Submission status: Accepted

Solution code:

n = int(input())
cnt = 0
moji = {}
for i in range(n):
    s = ''.join(sorted(list(input())))
    if not s in moji.keys():
        moji[s] = 0
    cnt += moji[s]
    moji[s] += 1
print(cnt)


#以下は、途中まで正解しているが、実行時間が長すぎる
"""
n = int(input())
s = []
ans = 0
for i in range(n):
    a = list(input())
    a.sort()
    s.append("".join(a))

x = set(s)
y = []
for i in range(len(x)):
    y.append(s.count(s[i]))

#print(y)
for j in range(len(y)):
    ans += int((y[j]*(y[j]-1))/2)

print(ans)
"""

Submission status: Accepted

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

for _ in range(n):
    s = input()
    s_count = 0
    for c in s:
        s_count += 10**(ord(c)-97)
    s_count_dir[s_count] = s_count_dir.get(s_count,0) + 1

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n, *S = sys.stdin.read().split()
n = int(n)

def main():
    t = [''.join(sorted(t)) for t in S]
    c = Counter(t)
    cnt = 0
    for v in c.values():
        cnt += v*(v-1)
    print(cnt//2)

main()

Submission status: Accepted

Solution code:
n=int(input())
s=[input() for _ in range(n)]
from collections import Counter
ans=0
d=Counter()
def combination(n, r):
    a = 1
    b = 1
    for i in range(r):
        a *= (n - i)
        b *= (i + 1)
    return a // b
for i in s:
    a="".join(sorted(list(i)))
    d[a]+=1
for j in d.values():
    ans+=combination(j, 2)

print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
list_voc = [str(input()) for _ in range(n)]
list_mo = []
for i, s in enumerate(list_voc):
    list_n = list(s)
    list_n.sort()
    list_n = ''.join(list_n)
    list_mo.append(list_n)

ans = {}
for s in list_mo:
    if s in ans:
        ans[s] += 1
    else:
        ans[s] = 1


ans_val = []

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


for s in ans:
    if ans[s] != 1:
        ans_val.append(cmb(ans[s], 2))


print(sum(ans_val))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
slis = []
for i in range(N) :
    s = list(input())
    s.sort()
    slis.append(s)

slis.sort()
temp = 1
ans = 0

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

for i in range(1, N) :
    if slis[i] == slis[i-1] :
        temp += 1
    else :
        if temp == 2 :
            ans += 1
        elif temp >= 3 :
            ans += combinations_count(temp, 2)
        temp = 1

if temp == 2 :
    ans += 1
elif temp >= 3 :
    ans += combinations_count(temp, 2)

print(ans)

Submission status: Accepted

Solution code:
from math import factorial
def comb(n,k):
  if n<0 or k<0 or n<k:return 0
  if n==0 and k==0:return 1
  a=factorial(n)
  b=factorial(k)
  c=factorial(n-k)
  return a//(b*c)
n=int(input())
s=[sorted(list(input())) for i in range(n)]
s.sort()
cnt=1
ans=0
for i in range(1,n):
  if s[i]==s[i-1]:
    cnt+=1
  else:
    ans+=comb(cnt,2)
    cnt=1
if cnt>=2:
  ans+=comb(cnt,2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for _ in range(N):
    IN = input()
    A = "".join(sorted(list(IN)))
    if A not in dic:
        dic[A] = 1
    else:
        dic[A] += 1
answer = 0
for k,v in dic.items():
    answer += v*(v-1)//2
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0
moji = {}
for i in range(n):
    s = ''.join(sorted(list(input())))
    if not s in moji.keys():
        moji[s] = 0 
    cnt += moji[s]
    moji[s] += 1
print(cnt)

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())
    lines = ["".join(sorted(line.rstrip())) for line in sys.stdin.readlines()]

    ret = 0
    sortedlines = {}
    for n in range(N):
        item = lines[n]
        if item in sortedlines:
            sortedlines[item] += 1
            ret += sortedlines[item]
        else:
            sortedlines[item] = 0

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())
    lines = ["".join(sorted(line.rstrip())) for line in sys.stdin.readlines()]

    ret = 0
    sortedlines = {}
    for n in range(N):
        item = lines[n]
        if item in sortedlines:
            sortedlines[item] += 1
            ret += sortedlines[item] - 1
        else:
            sortedlines[item] = 1

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)

ans = 0
for i in set(s):
   n = c[i]
   ans += n * (n - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for _ in range(N):
    d[''.join(sorted(input()))] += 1

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

ans = 0
for i in d.values():
    ans += tousa(i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


n = int(input())

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


answer = 0
count = 1
for i in range(n - 1):
    if S[i] == S[i + 1]:
        count += 1
    else:
        answer += count * (count - 1) // 2
        count = 1
answer += count * (count - 1) // 2


print(answer)

Submission status: Accepted

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

table = {}
ans = 0
for i in S:
    s = ''.join(sorted(i))
    if s not in table:
        table[s] = 0
    else:
        table[s] += 1
        ans += table[s]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
d={} ; a=[]
from collections import Counter
for _ in range(int(input())):
    s=input()
    a.append(tuple(sorted(s)))
d=Counter(a)
ans=0
for i in d:
    ans+=d[i]*(d[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
from collections import defaultdict


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

    for i in range(N):
        s = input()
        sorted_s = "".join(sorted(s))
        dct[sorted_s] += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
ss = {}
for _ in range(n):
  s = input()
  S = []
  for c in s:
    S.append(c)
  S.sort()
  s = ''.join(S)
  if s in ss:
    ss[s] += 1
  else:
    ss[s] = 1

def func(n):
  if n ==1:
    return 0
  return n*(n-1)//2
    
ans = 0
for key in ss:
  ans += func(ss[key])
    
    
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    S = list(input())
    S.sort()
    a.append("".join(map(str,S)))
a.sort()
a.append("z")

c = []
r = 1
for i in range(1,N+1):
    if a[i] == a[i-1]:
        r += 1
    else:
        if r > 1:
            c.append(r)
        r = 1

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

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
ans = 0
d = {}
for i in range(N):
    s = list(input())
    s.sort()
    s = "".join(s)
    if s not in d.keys():
        d[s] = 1
    else:
        d[s] += 1
# print(d)
for key in d.keys():
    ans += d[key] * (d[key] - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[]
cnt=dict()


for _ in range(N):
    tmp=list(input())
    sotmp=sorted(tmp)
    stmp=''.join(sotmp)
    s.append(stmp)

ss=sorted(s)

for word in ss:
    if((word in cnt.keys())==False):
        cnt[word]=0
    else:
        cnt[word]+=1


total=0
for value in cnt.values():
    total+=(1+value)*value//2

print(total)

Submission status: Accepted

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

def main():
    n = int(input())
    ss = {}
    for i in range(n):
        s = "".join(sorted(input()))
        if s not in ss.keys():
            ss[s] = 0
        ss[s] = ss[s] + 1

    print(sum(c2(v) for k, v in ss.items()))

def c2(n):
    # Choosing 2 from n
    return n * (n - 1) // 2

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = [0]*N
dict = {}
cnt = 0
for i in range(N):
    S[i] = sorted(input())
    if tuple(S[i]) in dict:
        dict[tuple(S[i])] += 1
        cnt += dict[tuple(S[i])]    
    dict.setdefault(tuple(S[i]),0)
print(cnt)

Submission status: Accepted

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

t = 1
n=1
while t<N:
    if S[t-1]==S[t]:
        n+=1
    else:
        output += int(n*(n-1)*0.5)
        n=1
    t+=1
output += int(n*(n-1)*0.5)
print(output)

Submission status: Accepted

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

from collections import Counter
def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()

    dic = Counter()
    ans = 0
    for i in range(n):
        s = input()
        ss = "".join(sorted(s))
        dic[ss] += 1
        ans += dic[ss]-1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict


n, *s = sys.stdin.read().split()
d = defaultdict(int)
ans = 0
for si in s:
    d[''.join(sorted(si))] += 1
for x in d.values():
    ans += x * (x - 1) // 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial

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

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

t = [''.join(sorted(str)) for str in s]
c = Counter(t)
ll = [i for _, i in c.items() if i > 1]
cnt = 0
for i in ll:
    cnt += count_combinations(i, 2)

print(cnt)

Submission status: Accepted

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

S=[""]*N
T={}
count=0

for i in range(N):
    S[i]=sorted(s[i])
    S[i]="".join(S[i])
    if S[i] not in T:
        T[S[i]]=1
    else:
        count+=T[S[i]]
        T[S[i]]+=1

print(count)

Submission status: Accepted

Solution code:
n = int(input())

d = {}
ans = 0
for i in range(n):
    s = str(input())
    s = "".join(sorted(list(s)))
    if s in d:
        d[s] += 1
        ans += d[s]
    else:
        d[s] = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
d = {}
for _ in range(N):
    s = list(input())
    s.sort()
    s = "".join(s)
    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)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)

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

Submission status: Accepted

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = ["".join(sorted(str(readline().rstrip().decode('utf-8')))) for _ in range(n)]
    s = collections.Counter(s).most_common()
    t = 0
    for k, v in s:
        if v > 1:
            t += (1 + v - 1) * (v - 1) // 2
    print(t)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())

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

print(sum(v*(v-1)//2 for v in d.values()))

Submission status: Accepted

Solution code:
n=int(input())
l=[sorted(list(input())) for i in range(n)]

d={}
for i in l:
  i=''.join(i)
  if i in d:
    d[i]+=1
  else:
    d[i]=1

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

Submission status: Accepted

Solution code:
n = int(input())
d = dict()
res = 0
for _ in range(n):
    s = input()
    s = list(s)
    s.sort()
    s = "".join(s)
    if s in d:
        res += d[s]
        d[s] += 1
    else:
        d[s] = 1
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
words = []
for i in range(N):
  s_in = str(input())
  # 単語内の文字をソートする(標準化)
  s_sort = ''.join(sorted([ch for ch in s_in]))
  words.append(s_sort)
#print(words)

# Counterクラス: key=要素 -> val=出現回数
from collections import Counter
cntr = Counter(words)
#print(cntr)

# 組み合わせ数を足す
# N個から2個を選ぶなら N(N-1)/2 でよい、階乗いらない
# 1の場合は↑が0になるので cnt>1 の条件いらない
ans = 0
for cnt in cntr.values():
  ans += cnt * (cnt - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
words = []
for i in range(N):
  s_in = str(input())
  s_sort = ''.join(sorted([ch for ch in s_in]))
  words.append(s_sort)
#print(words)

# Counterクラス: key=要素, val=出現回数
from collections import Counter
cntr = Counter(words)
#print(cntr)

# 出現回数2個以上は組み合わせ数を足す
ans = 0
for cnt in cntr.values():
  if cnt > 1:
    ans += cnt * (cnt - 1) // 2

print(ans)

Submission status: Accepted

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

N=int(read_())
ans_d={}
ans=0
for i in range(N):
    s=''.join(sorted(input()))
    if s in ans_d:
        ans+=ans_d[s]
        ans_d[s]+=1
    else:
        ans_d[s]=1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from scipy.misc import comb

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

dd = defaultdict(int)
for i in range(N):
  s = "".join(sorted(S[i]))
  dd[s] += 1

ans = 0
for key, val in dd.items():
  ans += comb(val, 2, exact=True)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
from scipy.misc import comb

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

dd = defaultdict(int)
for i in range(N):
  s = "".join(sorted(S[i]))
  dd[s] += 1

ans = 0
for key, val in dd.items():
  ans += comb(val, 2, exact=True)

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
dic = defaultdict(int)
ans = 0
for i in range(n):
    x = sorted(list(input()))
    s = "".join(x)
    dic[s] += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=[]
ans=0
count=1
def comb(n,k):
  for i in range(k-1):
    n*=n-i-1
    k*=k-i-1
  return n//k

for i in range(n):
  a=list(input())
  a.sort()
  " ".join(a)
  s.append(" ".join(a))
s.sort()
for i in range(n-1):
  if s[i]==s[i+1]:
    count+=1
  else:
    ans+=(comb(count,2))
    count=1
if count>0:
  ans+=(comb(count,2))
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
l = ["".join(sorted(input())) for i in range(n)]
l.sort()
c = 0
b = l[0]
ans = 0
for s in l[1:]:
    if s == b:
        c += 1
    else:
        ans += c * (c + 1) * 0.5
        c = 0
        b = s
ans += c * (c + 1) * 0.5
print(int(ans))

Submission status: Accepted

Solution code:
import math
n = int(input())
l = ["".join(sorted(input())) for i in range(n)]
l.sort()
c = 0
b = l[0]
ans = 0
for s in l[1:]:
    if s == b:
        c += 1
    else:
        ans += c * (c + 1) * 0.5
        c = 0
    b = s
ans += c * (c + 1) * 0.5
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
d = [0 for i in range(n)]
for i in range(n):
    a = 0
    for j in input():
        a += 10**(ord(j)-97)
    d[i] = a
d = sorted(d)
ans = 0
co = 1
for i in range(1, n):
    if d[i-1] == d[i]:
        co += 1
    else:
        ans += co*(co-1) // 2
        co = 1
ans += co*(co-1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for _ in range(N):
    s = str(sorted(list(input())))
    d[s] += 1
ans = 0
for v in d.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

from collections import Counter

c = Counter(s)
keys, values = zip(*c.most_common())
#print(keys)
#print(values)
ans = 0
n = 0
for i in range(len(values)):
    if values[i] > 1:
        n = values[i]
        ans += n*(n-1)//2
        
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial

N = int(input())
S = defaultdict(int)

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

ans = 0
for v in S.values():
    if v > 1:
        pairs = factorial(v) // (2 * factorial(v-2))
        ans += pairs

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ab = []
for i in range(n):
    s=input()
    t = list(s)
    t.sort()
    t = ''.join(t)
    ab.append(t)

ab.sort()
count = 1
memo = []
for i in range(n-1):
    #print(i)
    if ab[i] == ab[i+1]:
        count += 1
    else:
        memo.append(count)
        count = 1
    if i == n-2:
        memo.append(count)
ans = 0
for i in memo:
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = 0
r = collections.Counter(s)
for v in r.values():
    c += (v * (v - 1)) // 2

print(c)

Submission status: Accepted

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

count=[1]*len(t)
j=0
for i in range(1,n):
  if t[j]==s[i]:
    count[j]+=1
  else:
    j+=1

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

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import Counter
n = int(input())
lst = [input() for i in range(n)]
cnt = Counter()
ans = 0
for i in lst:
    a = ''.join(sorted(list(i)))
    if a in cnt:
        ans += cnt[a]
    cnt[a] += 1

print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

ctr = collections.Counter(s)

count = 0

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

print(count)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
import numpy as np

n, *s = sys.stdin.read().split()
n = int(n)

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

def main():
    t = [''.join(sorted(t)) for t in s]
    v = np.array(list(Counter(t).values()), dtype=np.int64)
    return np.sum(comb_2(v))

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n, *s = sys.stdin.read().split()
n = int(n)

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

def main():
    t = [''.join(sorted(t)) for t in s]

    c = Counter(t)
    pairs_cnt = 0
    for v in c.values():
        pairs_cnt += comb_2(v)
    
    return pairs_cnt
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

d=dict()

for i in range(n):
    if s[i] not in d.keys():
        d[s[i]]=1
    else:
        d[s[i]]+=1
ans=0
for v in d.values():
    ans+=v*(v-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
s=[]
for i in range(n):
    x=input()
    y=[]
    for i in x:
        y.append(i)
    y.sort()
    s.append(''.join(y))

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

Submission status: Accepted

Solution code:
import numpy as np

def Speed_Anag_Count(String):
    count = {}
    for i in String:
        i = sorted(i)
        i = str(''.join(i))
        if count.get(i) is None:
            count[i] = 0
        count[i] += 1
    
    return count

N = int(input())

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

count = Speed_Anag_Count(S)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# 最初はごちゃごちゃ考えていたが、文字列を１文字ずつリストにしてからソートして
# それをさらに文字列に戻してソートしたらおわり
n = int(input())
sort = [""]*n
for i in range(n):
    s = list(input())
    s.sort()
    sort[i] = s
sort.sort()
#print(sort)
ans = 0
cnt = 0
for i in range(n-1):
    if sort[i+1] == sort[i]: cnt += 1
    else: 
        ans += cnt*(cnt+1)//2
        cnt = 0
print(ans+cnt*(cnt+1)//2)