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())
q=[]  
for i in range(N):
    q+=[list(input())]
    q[i].sort()
q.sort() 
i=0
s=0
z=0 #総得点
while i<N-1:
    if q[i]!=q[i+1]:
        z+=((s*(s+1))//2)
        s=0
    else:
        s+=1
    i+=1
z+=((s*(s+1))//2)
print(z)

Submission status: Accepted

Solution code:
N=int(input())
n=0
a=0
L=sorted([sorted(list(input())) for _ in range(N)])
L.append("p")
for i in range(N):
  if L[i]!=L[i+1]:
    n+=(i-a)*(i-a+1)/2
    a=i+1
print(int(n))

Submission status: Accepted

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

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())
L = [str(input()) for i in range(N)]
M = []

for l in L:
    m = ''.join(sorted(l))
    M.append(m)

import collections
c = collections.Counter(M)
d = list(c.values())
e = []
for i in range(len(d)):
    e.append(sum(range(1,d[i])))

print(sum(e))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

n=int(input())
L = []
for i in range(n):
  a = input()
  a = sorted(a)
  L.append(a)
L = sorted(L)


k=1
sum=0
for i in range(n-1):
  if L[i]==L[i+1]:
    k += 1
  else:
    if k!=1:
      sum += cmb(k,2)
    k=1
if k!=1:
  sum += cmb(k,2)
print(sum)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
def input() -> str:
    return sys.stdin.readline()
n = int(input())
s = [input() for _ in range(n)]
cnt = defaultdict(int)
ans = 0
for si in s:
    ana = "".join(sorted(si))
    ans += cnt[ana]
    cnt[ana] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
def input() -> str:
    return sys.stdin.readline()
n = int(input())
s = [input() for _ in range(n)]
cnt = defaultdict(int)
for si in s:
    cnt["".join(sorted(si))] += 1
ans = 0
for v in cnt.values():
    ans += sum(range(1, v))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
s = [input() for _ in range(n)]
cnt = defaultdict(int)
for si in s:
    cnt["".join(sorted(si))] += 1
ans = 0
for v in cnt.values():
    ans += sum(range(1, v))
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = []
for _ in range(N):
  s = list(input())
  s.sort()
  s = ''.join(s)
  S.append(s)
c = collections.Counter(S)
v = list(c.values())
count = 0
for j in v:
  count += j*(j-3)/2+j
print(int(count))

Submission status: Accepted

Solution code:
import collections as cl

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

cs = cl.Counter(s)
num = list(cs.values())
count = 0

for j in range(0, len(num)):
    if num[j] != 1:
        count = count + (num[j] * (num[j] - 1)) / 2

print(int(count))

Submission status: Accepted

Solution code:
import unittest
from io import StringIO
import sys
from math import factorial
from scipy.misc import comb
import math


def resolve():
    N = int(input())
    d = {}
    count = 0
    for i in range(N):
        s = list(input())
        s.sort()
        s = ''.join(s)
        if s not in d.keys():
            d[s] = 1
        else:
            count += d[s]
            d[s] += 1

    print(count)


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

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

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

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


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

Submission status: Accepted

Solution code:
n = int(input())

dic = {}
ans = 0

for i in range(n):
    s = input()
    sss = "".join(sorted(s))
    #print(dic)
    #print(sss)
    if sss in dic:
        val = dic[sss]
        dic[sss] = val + 1
        ans = ans + val
        #print(dic[sss])
    else:
        dic.setdefault(sss, 1)

#print(dic)
print(ans)

Submission status: Accepted

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

memo = {}
ans = 0

for i in range(n):
    si = ('').join(S[i])
    if si not in memo:
        memo[si] = 1
    else:
        memo[si] += 1
        ans += memo[si] - 1

print(ans)

Submission status: Accepted

Solution code:

def inp():
  return list(input())

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

for i in range(N):
  s = str(sorted(inp()))
  
  if s in res:
    res[s] += 1
  else:
    res[s] = 1
    

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

Submission status: Accepted

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

Submission status: Accepted

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


memo={}
ans=0
for i in range(n):
  if S[i] not in memo:
    memo[S[i]]=1
  else:
    memo[S[i]]+=1
    ans+=memo[S[i]]-1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
q=[]  
for i in range(N):
    q+=[list(input())]
    q[i].sort()
q.sort() 
i=0
s=0
z=0 #総得点
while i<N-1:
    if q[i]!=q[i+1]:
        z+=((s*(s+1))//2)
        s=0
    else:
        s+=1
    i+=1
z+=((s*(s+1))//2)
print(z)

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

N = int(input())

s = []
ps = []
for i in range(N):
    ps = sorted(list(input()))
    s.append(''.join(ps))

d = collections.Counter(s)

cnt = 0
for v in d.values():
    if v != 1:
        cnt += int(v * (v-1) / 2)

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
b = list()
for _ in range(n):
    
    s = input()
    a = list()
    for i in range(len(s)):
        a.append(s[i])
    a.sort()
    s = ""
    s = s.join(a)
    
    b.append(s)
    
b.sort()
b.append("zzzzzzzzzzzzzzz")

ans = 0
cn  = 1
for i in range(0,len(b)-1):
    if(b[i]==b[i+1]):
        cn += 1
    else:
        ans += (cn*(cn-1))//2
        cn  = 1

print(ans)

Submission status: Accepted

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

letter_dict = {}
count = 0

for s in s_list:
    sort_list = [l for l in s]
    sort_list.sort()
    sort_str = ''.join(sort_list)
    if hash(sort_str) in letter_dict:
        letter_dict[hash(sort_str)] += 1
    else:
        letter_dict[hash(sort_str)] = 1

for k,v in letter_dict.items():
    count += v*(v-1)//2

print(count)

Submission status: Accepted

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

def solve(N: int, s: "List[str]"):
    s = [sorted(s[i]) for i in range(N)]
    s.sort()
    group = groupby(s)
    answer = 0
    for _, group in group:
        a = len(list(group))
        answer += int(a*(a-1)/2) if a > 1 else 0
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve(N: int, s: "List[str]"):
    s = [sorted(s[i]) for i in range(N)]
    s.sort()
    group = groupby(s)
    answer = 0
    for key, group in group:
        a = len(list(group))
        answer += int(a*(a-1)/2) if a > 1 else 0
    print(answer)
   


    # for i in range(N):
    #     a = 1
    #     pop_index = []
    #     for j in range(i+1,len(ss)):
    #         if ss[i] == ss[j]:
    #             a += 1
    #             pop_index.append(j)
        
    #     ss = [ss[i] for i in range(len(ss)) if i not in pop_index]
    #     answer += int(a*(a-1)/2) if a > 1 else 0
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(N):
    s_temp=list(s[i])
    s_temp.sort()
    s[i] = str(''.join(s_temp))
    
s_ctr = collections.Counter(s)

cnt=0
for j in s_ctr.values():
    if j>1:
        cnt += math.factorial(j)//(math.factorial(2)*math.factorial(j-2))
    
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
memo = {}
ans = 0
for i in range(N):
    si = list(input())
    si.sort()
    si = ('').join(si)
    if si not in memo:
        memo[si] = 1
    else:
        memo[si] += 1
        ans += memo[si] - 1
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
c=[]
count=0
ans=0
for i in range(n):
    x=list(input())
    a.append(x)
for j in range(n):
    a[j].sort()
a.sort()
for k in range(n-1):
    if a[k]==a[k+1]:
        if k==0 or a[k-1]!=a[k]:
            count+=2
        else:
            count+=1
        if k==n-2:
            c.append(count)
    else:
        c.append(count)
        count=0
for l in range(len(c)):
    ans+=int(c[l]*(c[l]-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 si in set(S):
  n=C[si]
  ans+=n*(n-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
input = sys.stdin.readline

def acm(x):
    ary = [ i for i in range(x) ]
    return sum(ary)

n = int(input())
strings = {}
for _ in range(n):
    obj = ('').join(sorted(input()))
    if obj in strings:
        strings[obj] += 1
    else:
        strings[obj] = 1

res = 0
for obj in strings:
    res += acm(strings[obj])
print(res)

Submission status: Accepted

Solution code:
N = int(input())
li = []
for i in range(N):
  a = list(input())
  a.sort()
  li.append("".join(a))
  
dic = {}
li2 = []
for i in range(len(li)):
  if not li[i] in dic:
    dic[li[i]] = 1
    li2.append(li[i])
  else:
    dic[li[i]] += 1

b = 0
for i in range(len(li2)):
  c = dic[li2[i]]
  if c == 1:
    b += 0
  else:
    b += c*(c-1)/2
print(int(b))

Submission status: Accepted

Solution code:
# ABC137C - Green Bin
from collections import Counter


def main():
    _, *S = open(0).read().split()
    cnt = Counter("".join(sorted(s)) for s in S).values()
    ans = sum(i * (i - 1) // 2 for i in cnt)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(N):
    S[i] = ''.join(sorted(S[i])) # sort characters in each word

list.sort(S) # sort S in alphabetical order

ans = 0
m = 1  # 同一の（同じ文字の組み合わせの）アナグラムの数
for i in range(N - 1): # i = 0, 1, ..., N - 2
    if S[i + 1] == S[i]:
        m += 1
    else:
        ans += m * (m - 1) // 2
        m = 1

ans += m * (m - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	s = {}
	pair = 0
	for _ in range(N):
		b = "".join(sorted([a for a in input()]))
		if b in s.keys():
			pair += s[b]
			s[b] += 1
		else:
			s[b] = 1
	print(pair)
main()

Submission status: Accepted

Solution code:
N=int(input())

s_dic={}
for i in range(N):
  s=list(input())
  s.sort()
  s_str="".join(s)
  
  if s_str in s_dic:
    s_dic[s_str]+=1
  else:
    s_dic[s_str]=1

#print(s_dic)
answer=0
for key in s_dic:
  answer+=(s_dic[key]*(s_dic[key]-1))//2

print(answer)

Submission status: Accepted

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000)
n = int(input())
list1 = [''.join(sorted(input())) for i in range(n)]
dict1 = dict()
cout = 0
@lru_cache(maxsize = None)
def func(s):
	if s == 2:
		return 1 #2個の場合は1パターン
	return s-1 + func(s-1)

for i in range(n):
	if list1[i] in dict1:
		dict1[list1[i]] += 1
	else:
		dict1[list1[i]] = 1

for i in dict1:
	if dict1[i] != 1:
		cout += func(dict1[i])

print(cout)

Submission status: Accepted

Solution code:
def t(s):
    a = [''.join(sorted(i)) for i in s]
    b = {}
    r = 0
    for i in a:
        if i in b.keys():
            r += b[i]
            b[i] += 1
        else:
            b[i] = 1
    return int(r)


if __name__ == '__main__':
    n = int(input())
    print(t([input() for i in range(n)]))

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from math import factorial as f
dic = dd(int)
N = int(input())
for _ in range(N):
	dic["".join(sorted(input()))] += 1
print(sum(map(lambda x: f(x)//f(x-2)//2 if x > 1 else 0, dic.values())))

Submission status: Accepted

Solution code:
n = int(input())
d_ans = {}
for _ in range(n):
  line = input()
  char_lst = 26*[0]
  for char in line:
    char_lst[ord(char) - 97] += 1
  try:
    char_tup = tuple(char_lst)
    d_ans[char_tup] += 1
  except KeyError:
    d_ans[char_tup] = 1

ans = 0    
for val in d_ans.values():
  ans += (val*(val-1)) >> 1
print(ans)

Submission status: Accepted

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

s.sort()

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

ans += cnt * (cnt + 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
l=defaultdict(int)
for i in range(n):
  l1=sorted(list(input()))
  w1="".join(l1)
  l[w1]+=1
count=0
for i in l:
  if l[i]!=1:
    count+=l[i]*(l[i]-1)//2
print(count)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

N = int(input())
dict = {}
for _ in range(N):
    str = input()
    st = ''.join(sorted(str))
    if st not in dict:
        dict[st] = 1
    else:
        dict[st] += 1
answer = 0
for key in dict:
    l = dict[key]
    answer += l*(l-1)//2
# print(dict)
print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
S = []
count = 0
for _ in range(n):
    S.append(''.join(sorted(list(input()))))

for i, v in collections.Counter(S).items():
    for i in range(1, v):
        count += i

print(count)

Submission status: Accepted

Solution code:
import math

n = int(input())
di = dict()
answer = 0

for i in range(n):
    tmp = str(sorted(input()))
    if di.get(tmp, 0) == 0:
        di[tmp] = 1
    else:
        answer += di[tmp]
        di[tmp] += 1

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter

num = int(input())
input = [input() for i in range(num)]
rv = 0
counters = [sorted(Counter(s).elements()) for s in input]
strings = ["".join(s) for s in counters]
counts = list(Counter(strings).values())
for c in counts:
  rv += sum(x for x in range(1,c))
print(rv)

Submission status: Accepted

Solution code:
from collections import Counter

num = int(input())
words = ["".join(sorted(input())) for i in range(num)]
x = Counter(words)
res = 0
for c in x.values():
    res += (c**2 - c)//2
print(res)

Submission status: Accepted

Solution code:
import collections

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

S = collections.Counter(S)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

L = [list(input()) for _ in range(N)]

dic = []
flag = {}
res = 0
for i in range(N):
    L[i].sort()

    if str(L[i]) in flag:
        flag[str(L[i])] += 1
        res += flag[str(L[i])]
    else:
        dic.append(L[i])
        flag[str(L[i])] = 0

print(res)

Submission status: Accepted

Solution code:
n = int(input())
dicte = {}
count = 0
for i in range(n):
    s = list(input())
    z = ''.join(sorted(s))
    if z in dicte:
        dicte[z] += 1
    else:
        dicte[z] = 1
for i in dicte.keys():
    count += ((dicte[i] * (dicte[i]-1))//2)
print(count)

Submission status: Accepted

Solution code:
from sys import stdin, exit
from math import factorial
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()

def choose(x):
  return factorial(x)/(2*factorial(x-2))

words = {}

for i in range(int(input())):
    s = "".join(sorted(si()))
    if s not in words:
        words[s] = 1
    else:
        words[s] += 1
ans = 0
for i in words:
    if words[i] == 1: continue
    ans += choose(words[i])
print(int(ans))

Submission status: Accepted

Solution code:
dct, count = {}, 0
N = int(input())
for i in range(N):
    tmp = "".join(sorted(input()))
    if dct.get(tmp) is None:
        dct[tmp] = 1
    else:
        count += dct[tmp]
        dct[tmp] += 1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
#from scipy.special import comb
def com(n , r):
    if n == 1:
        return 0
    else:
        return factorial(n) // (factorial(n - r) * factorial(r))
N = int(input())
S = []
for _ in range(N):
    s = input()
    S.append("".join(sorted(s)))
CS = Counter(S)
ans = 0
for val in CS.values():
    ans += com(val , 2 )
print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    d = dict()
    for i in range(N):
        s = list(input())
        s.sort()
        s = tuple(s)
        if s in d:
            d[s] += 1
            continue
        d[s] = 0

    ans = 0
    for key, val in d.items():
        if val == 0:
            continue
        ans += (val+1)*val//2

    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

s.sort()

a = 0
answer = 0

for i in range(N-1):
    for j in range(10):
        if s[i][j] != s[i+1][j]:
            a = 0
            break
        elif j == 9:
            a += 1
            answer += a

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
s = 0
for n in range(N):
  v = ''.join(sorted(input()))
  if dic.get(v) is None:
    dic[v] = 1
  else:
    s += dic[v]
    dic[v] += 1
print(s)

Submission status: Accepted

Solution code:
from math import factorial
N = int(input())
s = {}
for i in range(N):
    w = sorted(list(input()))
    w = ''.join(w)
    if w in s :
      s[w]+=1
    else:
      s[w] = 1

count = 0
for k in s:
  if s[k] >= 2:
    count += factorial(s[k]) // (factorial(s[k] - 2) * factorial(2))
    
print(count)

Submission status: Accepted

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    s = collections.Counter(["".join(sorted(list(str(input().rstrip('\n'))))) for _ in range(n)])
    cnt = 0
    for k, v in s.items():
        for i in range(1, v):
            cnt += i
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = [0]*n
count = 0
for i in range(n):
  a[i] = list(input())
  a[i].sort()
  d = ""
  for j in a[i]:
    d += j
  a[i] = d
c = collections.Counter(a)
for i in c.values():
  count += (i-1) * i // 2
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
si = [''.join(sorted(list(input()))) for i in range(N)]

count = Counter(si)

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

print(ans)

Submission status: Accepted

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

L = ["0"]*N
for i in range(N):
    temp = ["0"]*10
    for j in range(10):
        temp[j] = S[i][j]
    temp.sort()
    L[i] = temp
        
L.sort()
ans = 0
cnt = 1
temp = L[0]
for i in range(1, N):
    if temp == L[i]:
        cnt += 1
    else:
        ans += cnt*(cnt-1)//2
        cnt = 1
        temp = L[i]
        
ans += cnt*(cnt-1)//2
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 collections import Counter

N = map(int,readline())
S = [''.join(sorted(x.decode('utf-8'))) for x in read().split()]

counter = Counter(S)

answer = sum(x*(x-1)//2 for x in counter.values())
print(answer)

Submission status: Accepted

Solution code:
n = int(input())

l = []
cnt = 0
t = 0

for i in range(n):
    s = str(input())
    sl = ''.join(sorted(s))
    l.append(sl)

l.sort()

tmp = l[0]

for i in range(1, n):
  if tmp == l[i]:
    cnt += 1
  else:
    tmp = l[i]
    t += int(cnt * (cnt + 1) / 2)
    cnt = 0
else:
    t += int(cnt * (cnt + 1) / 2)

print(t)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(lambda: 0)
ans = 0

for i in s:
    d[ str(sorted(i)) ] += 1

for i in d.keys():
    count = int(d[i])
    if count > 1:
        ans += (count * (count - 1)//2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for _ in range(n):
	s = input()
	l.append(''.join(sorted(s)))
l.sort()
res=0
ile = 1
for i in range(1, n):
	if l[i] == l[i-1]:
		ile+=1
	else:
		res+=ile * (ile-1) // 2
		ile=1
res+=ile * (ile-1) // 2
print (res)

Submission status: Accepted

Solution code:
N=int(input())
S=["".join(sorted(input())) for n in range(N)]
d={}
for s in S:
    if s not in d:
        d[s]=1
    else:
        d[s]+=1
c=0
for k in d:
    n=d[k]
    c+=(n*(n-1))//2
print(c)

Submission status: Accepted

Solution code:
N = int(input())
S =[]
for i in range(N):
  s = list(str(input()))
  s.sort()
  s = ''.join(s)
  S.append(s)
from collections import Counter
l = Counter(S)
count = []
for i in l.values():
  count.append(i*(i-1)/2)
print(int(sum(count)))

Submission status: Accepted

Solution code:
N=int(input())
S_list=[]
for i in range(N):
    s=input()
    ss=list(s)
    ss.sort()
    S_list.append(ss)
S_list.sort()
ans=0
cnt=0
for i in range(1,N):
    if S_list[i]==S_list[i-1]:
        cnt+=1
        ans+=cnt
    else:
        cnt=0
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    string = {}
    ret = 0
    for n in range(N):
        cur_char = ''.join(sorted(input()))
        if cur_char in string:
            ret += string[cur_char]
        string[cur_char] = string.get(cur_char, 0) + 1
    print(ret)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
  n = ni()

  d = {}
  for _ in range(n):
    s = "".join(sorted(ns()))
    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)

main()

Submission status: Accepted

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

for idx in range(N):
	s = input()
	allist = [c for c in s]
	allist.sort()
	sortedstr = ''.join(allist)
	count[sortedstr] = count.get(sortedstr, 0) + 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
myDict = defaultdict(int)

n = int(input().strip())
ans = 0
for _ in range(n):
    s = input().strip()
    s = ''.join(sorted(s)) 
    ans += myDict[s]
    myDict[s] += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    def c(n):
        if n == 1:
            return 0
        else:
            return n * (n - 1) // 2


    N = int(lines.pop(0))
    NLIST = {}
    for i in range(N):
        l = ''.join(sorted(lines.pop(0)))
        if l in NLIST:
            NLIST[l] += 1
        else:
            NLIST[l] = 1

    re = 0
    for n in NLIST.values():
        re += c(n)
    print(re)
lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

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

to = 0
for i in range(N):
	st = input()
	st = str(sorted(st))
	if i == 0 or not st in s:
		s[st] = 1
	elif st in s:
		cnt = s[st]
		to += cnt
		s[st] = cnt + 1

print(to)

Submission status: Accepted

Solution code:
from collections import Counter as co
n=int(input())
a=[str(sorted(input())) for _ in range(n)]
d=co(a)
l=d.values()
c=0
for i in l:
  c+=(i*(i-1))//2
print(c)

Submission status: Accepted

Solution code:
def aa(num):
    b=1
    c=0
    for i in range(num-1):
        c+=b
        b+=1
    return c

n=int(input())
s=[]
for i in range(n):
    word=list(input())
    word2=sorted(word)
    word3="".join(word2)
    s.append(word3)
s2=sorted(s)
ans=0
now=s2[0]
num=0
for i in range(n):
    if now==s2[i]:
        num+=1
    else:
        ans+=aa(num)
        now=s2[i]
        num=1
print(ans+aa(num))

Submission status: Accepted

Solution code:
import math
n = int(input())
dic = {}
def combinations_count(n,r):
    return math.factorial(n) // (math.factorial(n - r)*math.factorial(r))

for index in range(n):
    temp = ''.join(sorted(list(input())))
    #入力された文字列をa-z順にソートし、変数tempに代入
    if temp in dic:
        dic[temp] = dic[temp] + 1
        #すでに同じ文字列の入力があれば、key:tempのvalueに+1
    else:
        dic[temp] = 1
        #新しい文字列の場合は文字列とvalue＝1を辞書に追加
ans = 0

for k in dic.values():
    if k >= 2:
        ans += combinations_count(k,2)
    else:
        ans = ans
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

dic = {}

for index in range(N):
    temp = "".join(sorted(list(input())))
    if temp in dic:
        dic[temp] = dic[temp] + 1
    else:
        dic[temp] = 1
ans = 0
for k in dic.values():
    cnt = 0
    for i in range(k):
        cnt += i
    ans += cnt

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
strings = {}
answer = 0
for i in range(n):
  string = input()
  string = ''.join(sorted(string))
  if string not in strings:
    strings[string]=1
  else:
    answer += strings[string]
    strings[string] += 1
print(answer)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+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 inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

def conb(n,r): 
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = inp()
s = []
for i in range(n):
    tmp = list(input())
    s.append(sorted(tmp))
s.sort()
# print(s)
cnt = []
tmp = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    elif tmp != 0:
        cnt.append(tmp)
        tmp = 0
if tmp != 0:
    cnt.append(tmp)
# print(cnt)
res = 0
for i in cnt:
    res += conb(i+1, 2)
print(res)

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
ans = 0
for i in range(n):
    s = str(sorted(input()))
    if dic.get(s) != None:
        ans += dic[s]
        dic[s] = dic[s] + 1
    else:
        dic[s] = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    lst = []
    count = 0
    words = defaultdict(lambda: 0)
    for i in range(n):
        words[''.join(sorted(input()))] += 1
    sum = 0
    for word in words:
        sum += int(words[word]*(words[word] - 1)/2)
    print(sum)