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())
s = [sorted(input()) for i in range(N)]

ans = 0
aDict = {}
for i in s:
  if str(i) not in aDict:
    aDict[str(i)] = 1
  else:
  	aDict[str(i)] += 1
    
for n in aDict.values():
    ans += (n*(n-1)//2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    d={}
    for i in range(n):
        tmp = ''.join(sorted(input()))
        
        if tmp in d:
            d[tmp]+=1
        else:
            d[tmp]=1
    ans=0
    for i in d.values():
        ans+=i*(i-1)//2
    print(ans)

main()

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list()
d = dict()
for i in range(n):
    b = list(input())
    b.sort()
    moji = "".join(b)
    if (moji in d):
        d[moji] += 1
    else:
        d[moji] = 1
x = 0
for i in d:
    if d[i] >=1:
        x += d[i]*(d[i]-1)/2
print(int(x))

#非最適化ソート
# for i in range(n):
#     a.append(input())
# for i, youso in enumerate(a):
#     b = list()
#     for j in youso:
#         b.append(j)
#     b.sort()
#     x = str()
#     for j in b:
#         x += j
#         a[i] = x

#TLE ver.
# for i, youso in enumerate(a):
#     for j in range(i, n-1):
#         if youso == a[j+1]:
#             x += 1
# print(x)

#TLE改良版
# a.sort()
# for i in range(n-1):
#     if a[i] == a[i+1]:
#         for j in range(i+1, n):
#             if a[i] == a[j]:
#                 x += 1
#             else:
#                 break
#     else:
#         continue



#いらない処理
# y = 0
# for i in range(1, x+1):
#     y += i

Submission status: Accepted

Solution code:
N = int(input())

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

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


def main():
    N = int(readline())
    S = read().split()
    S = list(map(lambda x: sorted(x.decode('UTF-8')), S))

    A = defaultdict(int)
    for i in range(N):
        s = ''.join(S[i])
        A[s] += 1

    cnt = 0
    for i in A.values():
        cnt += i*(i-1)//2
    print(round(int(cnt)))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter

N = int(input())
s = []
for i in range(N):
    s1 = "".join(sorted(input()))
    s.append(s1)
    
c = list(Counter(s).values())
ans = 0
for i in c:
    if i >= 2:
        ans += (i * (i - 1)) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
L = list()
for i in range(N):
  L.append("".join(sorted(input())))
  
#print(L)
d = collections.Counter(L)

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[input() for i in range(n)]
dic={}
l=list()
cnt=0
for i in range(n):
  k=sorted(s[i])
  t=''.join(k)
  l.append(t)
  if l[i] in dic:
    dic[l[i]]+=1
  else:
    dic[l[i]]=0
c=list(dic.values())
for i in range(len(c)):
   cnt+=c[i]*(c[i]+1)//2
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(sorted(input()))
a.sort()
p=1
q=0
for i in range(n-1):
  if a[i]==a[i+1]:
    p+=1
  else:
    q+=p*(p-1)//2
    p=1
q+=p*(p-1)//2
print(q)

Submission status: Accepted

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

hsh = dd(int)

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

x = 0
for i in hsh.keys():
  x += (hsh[i]*(hsh[i]-1))//2

print(x)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = ["".join(sorted(input())) for _ in range(n)]
#values = []
combs = 0
c = collections.Counter(s)
# このfor文を削減するためにcollections.Counter()を使用
# for i in set(s):
#   if s.count(i) > 1:
#     values.append(s.count(i))
for i in c.values():
  combs+=i*(i-1)//2
print(combs)

Submission status: Accepted

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    S=[input() for _ in range(N)]
    Ss=["".join(sorted(x)) for x in S]
    Ss.sort()

    ans = 0
    cnt = 0
    s0 = Ss[0]
    for s in Ss[1:]:
        if s == s0:
            cnt += 1
        else:
            ans += (cnt+1)*cnt//2
            s0 = s
            cnt = 0
            continue
    ans += (cnt+1)*cnt//2

    print(ans)

# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc137","c",main)

Submission status: Accepted

Solution code:
import collections

n = int(input())

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

c = collections.Counter(s)

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

print(ans)

Submission status: Accepted

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

n = int(input())
s = ["".join(sorted(input())) for i in range(n)]
c = collections.Counter(s)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# 2019-11-16 14:51:41(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
from scipy.misc import comb # comb(n, r, exact=True)
# import numpy as np
# import heapq
# import array


def main():
    n = int(sys.stdin.readline().rstrip())

    sorted_letters = [''.join(sorted(sys.stdin.readline().rstrip())) for _ in range(n)]
  
    count = 0
    for c in collections.Counter(sorted_letters).values():
        if c >= 2:
            count += comb(c, 2, exact=True)

    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 2019-11-16 14:51:41(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
from functools import reduce
import operator as op
# from scipy.misc import comb # float
# import numpy as np
# import heapq
# import array


def nCr(n, r):
    r = min(r, n - r)
    upper = reduce(op.mul, range(n, n-r, -1), 1)
    lower = reduce(op.mul, range(r, 0, -1), 1)
    return upper // lower

def main():
    n = int(sys.stdin.readline().rstrip())

    sorted_letters = [''.join(sorted(sys.stdin.readline().rstrip())) for _ in range(n)]
  
    count = 0
    for c in collections.Counter(sorted_letters).values():
        if c >= 2:
            count += nCr(c, 2)
            
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

N = int(input())

dict = {}

for i in range(N):
    s = input()
#     print(s)
    temp = [s[j] for j in range(len(s))]
    temp.sort()
    s2 = temp[0]
    for j in range(len(s)):
        if j == 0: continue
        s2 += temp[j]
    if s2 in dict:
        dict[s2] += 1
    else:
        dict[s2] = 1

ret = 0
for key in dict.keys():
    n = dict[key]
    ret += (n - 1) * n / 2
print(int(ret))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
dic={}#辞書
for i in range(n):
    tmp="".join(sorted(input()))
    if tmp in dic.keys():
        dic[tmp]+=1
    else:
        dic[tmp]=1

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

Submission status: Accepted

Solution code:
n = int(input())
st = {}
sv = {}
for i in range(n):
	tmp = "".join(sorted(input()))
	if tmp in st.keys():
		sv[tmp] += st[tmp]
		st[tmp] += 1
	else:
		st[tmp] = 1
		sv[tmp] = 0


print(sum(sv.values()))

Submission status: Accepted

Solution code:
from functools import reduce
from operator import mul

def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    return reduce(mul, range(n, n - r, -1)) // reduce(mul, range(r, 0, -1))
ans = 0
n = int(input())
s = [input() for i in range(n)]
anagram = {}
for i in s:
  t = [0] * 26
  for j in i:
    t[ord(j)-ord("a")] += 1
  t = "".join(map(str, t))
  if t in anagram:
    anagram[t] += 1
  else:
    anagram[t] = 1
for i in anagram.values():
  if i >= 2:
    ans += cmb(i, 2)
print(ans)

Submission status: Accepted

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

ss = [''.join(sorted(i)) for i in s]
ans = 0
key_dict = {}

for i in ss:
    if i in key_dict:
        key_dict[i] += 1
    else:
        key_dict[i] = 1

a = key_dict.values()
al = [i * (i-1) / 2 if i > 1 else 0 for i in a]

ans = int(sum(al))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
str_list = [''] * n
ans = 0

for i in range(n):
  text = list(input())
  text.sort()
  str_list[i] = ''.join(text)

str_list.sort()

current_str = str_list[0]
count = 0

for i in range(1, n):
  if current_str == str_list[i]:
    count += 1
  else:
    if count > 0:
      for j in range(1, count+1):
        ans += j
    count = 0
    current_str = str_list[i]

if count > 0:
  for j in range(1, count+1):
    ans += j
  
# print(n, str_list)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

dic = {}
count = 0

for i in range(n) :
    tmp = str.join('', sorted(list(input())))
    if tmp not in dic :
        dic[tmp] = 0
    else :
        dic[tmp] += 1
        count += dic[tmp]

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

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

n = inp()
tempcons = 0
ans = 0

Slist = sorted([sorted(input()) for i in range(n)])
for i in range(n-1):
    if Slist[i] == Slist[i+1]:
        tempcons += 1
    else:
        ans += pair(tempcons+1)
        tempcons = 0
ans += pair(tempcons+1)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N = int(input())
    d = Counter()
    for _ in range(N):
        t = ''.join(sorted(input()))
        d[t] += 1
    r = 0
    for v in d.values():
        if v == 1:
            pass
        r += v * (v - 1) // 2
    return r

print(main())

Submission status: Accepted

Solution code:
import math
n = int(input())
li = list()
for _ in range(n):
    li.append(''.join(sorted(input())))
li.sort()

ans = 0
i = 0
while i < n:
    cnt = 1
    c = li[i]
    while i < n - 1 and li[i] == li[i + 1]:
        cnt += 1
        i += 1
    i += 1
    if cnt > 1:
        ans += math.factorial(cnt) // (math.factorial(cnt - 2) * math.factorial(2))
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
words = {}
ans = 0

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# 初項s, 交差dのn個の数列の和
def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
def get_counter(s):
    return Counter(s.upper().replace(' ', ''))

n=int(input())
#a=[get_counter(input().rstrip('\r\n')) for i in range(n)]
a=["".join(sorted(input().rstrip('\r\n'))) for i in range(n)]
def r(n):
    cnt=0
    for i in range(n):
        cnt+=i+1
    return cnt

my_dic={}
for i in a:
    if i in my_dic:
        my_dic[i]+=1
    else:
        my_dic[i]=1

res=0
for i in my_dic.values():
    res+=r(i-1)
        
print(res)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = [''.join(sorted(input().strip())) for _ in range(N)]

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

Submission status: Accepted

Solution code:
import collections
import math

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

ans = 0

for _ in range(N):
    s = input()
    s = "".join(sorted(s))
    if s in word_dict:
        ans += word_dict[s]
        word_dict[s] += 1
    else:
        word_dict[s] = 1
    
print(ans)

Submission status: Accepted

Solution code:
import collections as c

N = int(input())
S = sorted(["".join(sorted(input())) for _ in range(N)])

D = c.Counter(S)

D_value = []

for a in D.values():
	D_value.append(a)

ans = 0

for i in D_value:
	if i == 0:
		continue
	ans += (i * (i-1)) // 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[""]*n
for i in range(n):
  s[i]=input()

ans=0
lis={}
for word in s:
  arr=[]
  for let in word:
    arr.append(let)
  arr.sort()
  new=''
  for let in arr:
     new += let
  if new in lis:
    lis[new] += 1
    ans += lis[new]
  else: 
    lis[new] = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
ss = [input() for _ in range(n)]

sorted_ss = [''.join(sorted(s)) for s in ss]
sorted_counter = Counter(sorted_ss)

cnt = sum([n * (n - 1) // 2 for n in sorted_counter.values()])
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
def print_ans(N, sr):
    """Test Case
    >>> print_ans(3, ["acornistnt", "peanutbomb", "constraint"])
    1
    >>> print_ans(2, ["oneplustwo", "ninemodsix"])
    0
    >>> print_ans(5,["abaaaaaaaa","oneplustwo","aaaaaaaaba","twoplusone","aaaabaaaaa"]) 
    4
    """
    for i in range(N):
        sr[i] = "".join(sorted(sr[i]))
    C = Counter(sr)
    ans = 0
    for v in C.values():
        ans += v * (v - 1) // 2
    print(ans)

    # for i in range(N):
    #     tmp = sr[i]
    #     for k in range(i + 1, N):
    #         if tmp == sr[k]:
    #             count +=1
    # print(count)


if __name__ == '__main__':
    N = int(input().rstrip())
    sr = [ input().rstrip() for _ in range(N)]
    print_ans(N, sr)

Submission status: Accepted

Solution code:
from collections import Counter #ハッシュ法
N = int(input())
s_list = [input() for i in range(N)]

#出現するアルファベットをカウントしていく
al_list = [chr(ord('a') + i) for i in range(26)]

num_list = []
count_by_kw = Counter()
for _s in s_list:
    num = ''
    for _al in al_list:
        num += str(_s.count(_al))
    
    num_list.append(num)

count_list = list(Counter(num_list).values()) 

ans = 0
for i in range(len(count_list)):
    k = count_list[i]
    ans += k * (k - 1) / 2

print(int(ans))

Submission status: Accepted

Solution code:
import sys
import collections


def alg_combination(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    else:
        denominator = 1
        for i in range(n, n - r, -1):
            denominator = (denominator * i)
        molecule = 1
        for i in range(1, r + 1):
            molecule = (molecule * i)
        return denominator // molecule


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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
S = list(''.join(sorted(str(input()))) for _ in range(N))

dic = {}
ans = 0

for s in S:
    if not s in dic:
        dic[s] = 1
    else:
        ans += dic[s]
        dic[s] += 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

s_list_cnt = collections.Counter(s_list)
total = 0
for cnt in s_list_cnt.values():
  total += cnt * (cnt - 1) / 2
  
print(int(total))

Submission status: Accepted

Solution code:
N = int(input())
S = {}
ans = 0
for i in range(N):
    a = input()
    s = [ ]
    for j in range(10):
        s.append(a[j])
    s.sort()
    b =s[0]
    for k in range(1,10):
        b += s[k]
    if b in S:
        ans += S[b]
    else:
        S[b] = 0
    S[b] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
ans = 0

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

cnt = 1
for j in range(0, n-1):
  if s[j] == s[j+1]:
    cnt += 1
    if j == n-2:
      ans += cnt*(cnt-1)/2
  else:
    ans += cnt*(cnt-1)/2
    cnt = 1
    
print(int(ans))

Submission status: Accepted

Solution code:
N=int(input())
d=dict()
ans=0
for i in range(N):
    s="".join(sorted(input()))
    if s not in d.keys():
        d[s]=0
    ans+=d[s]
    d[s]+=1
print(ans)

Submission status: Accepted

Solution code:
alphabet = "abcdefghijklmnopqrstuvwxyz"


n = int(input())
res = dict()
ans = 0
for _ in range(n):
    s = str(input())
    s = "".join(sorted(s))
    if s not in res.keys():
      res[s] = 1
    else:
      ans += res[s]
      res[s] += 1
print(ans)

Submission status: Accepted

Solution code:
alphabet = "abcdefghijklmnopqrstuvwxyz"


n = int(input())
res = dict()
for _ in range(n):
    s = str(input())
    freqency = [0 for _ in alphabet]
    for __ in s:
        freqency[alphabet.index(__)] += 1
    freq = "".join([str(___) for ___ in freqency])
    if freq not in res:
        res[freq] = 1
    else:
        res[freq] += 1

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

Submission status: Accepted

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

for s in S:
  ls = str(sorted(list(s)))
  if ls in count:
    count[ls] +=1
  else:
    count[ls] = 1

for s in count:
  count[s] = count[s]*(count[s]-1)//2 
print(sum(count.values()))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

A={}
U=0
for i in range(N):
    if ''.join(sorted(s[i])) in A.keys():
        A[''.join(sorted(s[i]))]=A[''.join(sorted(s[i]))]+1
        U+=A[''.join(sorted(s[i]))]
    else:
        A[''.join(sorted(s[i]))]=0
print(U)

Submission status: Accepted

Solution code:
from operator import itemgetter

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

# 2次元配列の文字を、左から優先にソート
s_sort = sorted(s, key = itemgetter(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

count = 0
s_count = 0

l = s_sort[0]

for i in range(1, n):
    if l == s_sort[i]:
        #同じ文字があるごとにカウントの数が増える（同じ文字が2つなら1個の組み合わせ、3つなら3つの組み合わせがある）
        s_count += 1
        count += s_count
    else:
        l = s_sort[i]
        s_count = 0

print(count)

Submission status: Accepted

Solution code:
N = int(input())
s = {}
ans = 0
for i in range(N):
    a = "".join(sorted(input()))
    if a not in s:
        s.setdefault(a,0)
    else:
        s[a] += 1
        ans += s[a]
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)
def moji(s):
    tmp = []
    for i in range(10):
        tmp.append(s[i])
    tmp.sort()
    new_s = ''
    for i in range(10):
        new_s += tmp[i]
    return new_s

N = int(input())
dic = {}
ans = 0
for i in range(N):
    tmp = moji(input())
    if tmp in dic:
        dic[tmp] += 1
    else:
        dic[tmp] = 1
for i in dic.values():
    if i == 1:
        continue
    ans += C(i,2)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[0]*N
for i in range(N):
    s[i] = input()
    s[i] = sorted(s[i])
s=sorted(s)
counter=1
sum=0
for i in range(N-1):
    if s[i] == s[i+1]:
        counter+=1
    else:
        sum+=counter*(counter-1)//2
        counter=1
sum+=counter*(counter-1)//2
print(sum)

Submission status: Accepted

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

# check
dic = {}
for i in li:
    if (i in dic):
        dic[i] += 1
    else:
        dic[i] = 1

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

ans = 0

for i in dic.values():
    ans += calc(i)

# output
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
b = []
for i in range(n) :
  b.append(sorted(list(input())))
b.sort()
prev = [1,2,3,4]
tot = 1
ans = 0
for i in range(n) :
  if b[i]==prev :
    tot+=1
  else :
    ans+=(tot*(tot-1)/2)
    tot=1
    prev = b[i]
if tot>1 :
  ans+=(tot*(tot-1)/2)
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = []
d = {}
for _ in range(n):
    s.append("".join(sorted(str(input()))))
for si in s:
    if si not in d.keys():
        d[si] = 1
    else: d[si] += 1
print(sum([int(v*(v-1)/2) for v in d.values()]))

Submission status: Accepted

Solution code:
n = int(input())
s = []
d = {}
for _ in range(n):
    s.append("".join(sorted(str(input()))))
for si in s:
    if si not in d.keys():
        d[si] = 1
    else: d[si] += 1
ans = 0
val = d.values()
for v in val:
    ans += v*(v-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
import collections
N = int( input() )
char_list = []
ans = 0 
for i in range(N):
    c = sorted( input() ) 
    c_new = "".join(c)
    char_list.append(c_new)
    
t = collections.Counter(char_list)

for i in t.values():
    if i != 1:
        ans += int( ( i * (i - 1) ) / 2 )
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
S = collections.defaultdict(int)
for _ in range(N):
  s = ''.join(sorted(input().rstrip()))
  S[s] += 1

ans=0
for s in S.values():
  ans += s*(s-1)//2
  
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()))
  if s in S:
    S[s] += 1
  else:
    S[s] = 1

ans=0
for s in S.values():
  ans += s*(s-1)//2
  
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().rstrip()))
  if s in S:
    S[s] += 1
  else:
    S[s] = 1

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import collections
n = int(input())

sl = []
for _ in range(n):
    s = sorted(list(input()))
    sl.append(s)

before_s = ""
ans = 0
point = 1
for i in sorted(sl):
    if i == before_s:
        point += 1
    else:
        before_s = i
        ans += int(point * (point - 1) / 2)
        point = 1
ans += int(point * (point - 1) / 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = sorted([sorted(input()) for _ in range(N)])
k = 0
ans = 0
for i in range(1, N):
    if S[i] == S[i-1]:
        k += 1
        ans += k
    else:
        k = 0
print(ans)

Submission status: Accepted

Solution code:
# C Green_Bin
n = int(input())
sl = []
ans = 0
for i in range(n):
    s = "".join(sorted(input()))
    sl.append(s)
sl.sort()
bi = ""
tmp = 0
for i in sl:
    if (bi == i):
        tmp += 1
    else:
        ans += (tmp+1)*(tmp)//2
        tmp = 0
    bi = i
ans += (tmp+1)*(tmp)//2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = []
for _ in range(n):
    tmp = list(input())
    tmp.sort()
    s.append(str(tmp))
cnt = 0
dict = {}
for x in s:
    if x in dict:
        cnt += dict[x]
        dict[x] += 1
    else:
        dict[x] = 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
ans = 0
for i in range(n):
  s = "".join(sorted(input()))
  a = dic.get(s)
  if a is None:
    dic[s] = 1
  else:
    ans += a
    dic[s] += 1
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 si in set(s):
    n = c[si]
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
strings_list = [list(input()) for i in range(n)]
counter = 0
dict = {}
 
for strings in strings_list:
    strings = "".join(sorted(strings))
    if strings in dict.keys():
        counter += dict[strings]
        dict[strings] += 1
    else:
        dict[strings] = 1
print(counter)

Submission status: Accepted

Solution code:
N = int(input())
s_dict = {}
ans = 0
for i in range(N):
  string = list(input())
  string.sort(reverse=False)
  if tuple(string) in s_dict:
    s_dict[tuple(string)] += 1
  else:
    s_dict[tuple(string)] = 1

for i in s_dict.values():
  if i != 1:
    ans += sum([i for i in range(1,i)])

print(ans)

Submission status: Accepted

Solution code:
#137c
N = int(input())
s_dict = {}
ans = 0

#文字を読み込みつつ、出現回数を辞書化して、辞書に追加
for i in range(N): #2<N<10^5
  # https://www.lifewithpython.com/2015/05/python-count-characters-in-string.html
  string = list(input())
  string.sort(reverse=False)
  
  #print(tuple(string))
    
  if tuple(string) in s_dict:
    s_dict[tuple(string)] += 1
  else:
    s_dict[tuple(string)] = 1

for i in s_dict.values():
  if i != 1:
    ans += sum([i for i in range(1,i)])

print(ans)

Submission status: Accepted

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

num=1
for i in range(N-1):
    if(s[i]==s[i+1]):
        num += 1
    elif(num>1):
        count += num*(num-1)/2
        num = 1
    
if(num>1):
    count += num*(num-1)/2
print(int(count))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    array_s = []
    array = []
    for i in range(n):
        array_s.append(input())
 
    for s in array_s:
        array.append(''.join(sorted(s)))
    array = sorted(array)
    ans = 0
    count = 1
    for i in range(n-1):
        if array[i] == array[i+1]:
            count += 1
            if i != n-2:
                continue
        ans += int(count*(count-1)/2)
        count = 1
    print(ans)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
plis = [2]
for i in range(3,150):
    s =True
    for p in plis:
        if i%p==0:
            s = False
    if s:
        plis.append(i)
for i in range(N):
    foo = 1
    for j in input():
        j = ord(j) - ord("a")
        foo *= plis[j]
    try:
        dic[foo] += 1
    except:
        dic[foo] = 1
ans = 0
for n in dic.values():
    ans += n*(n-1)//2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=[list(input()) for i in range(n)]
count=0
ans=0
for i in range(n):
  d[i].sort()
d.sort()

for i in range(n-1):
  if d[i]==d[i+1]:
    count+=1
  elif d[i]!=d[i+1]:
    x=count+1
    count=0
    ans+=(x*(x-1))/2
  if i==(n-2):
    x=count+1
    count=0
    ans+=(x*(x-1))/2
  
print(int(ans))