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

def main():
    #K, X = [int(f) for f in input().split()]
    N = int(input())
    ana = {}
    for _ in range(N):
        key = ''.join(sorted(input()))
        if key in ana:
            ana[key] += 1
        else:
            ana[key] = 1

    counter = 0
    for key in ana:
        if ana[key] == 1:
            continue
        counter += cmb(ana[key], 2)
    print(counter)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
d={}
for i in range(n):
    s=str(sorted(input()))
    try:
        d[s]+=1
    except:
        d[s]=1
ans=0

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

Submission status: Accepted

Solution code:
N = int(input())
A = {}
cnt = 0
for i in range(N):
    x = sorted(list(input()))
    if str(x) in A.keys():
        A[str(x)] += 1
    else:
        A[str(x)] = 1
for i in A.values():
    cnt += sum(list(range(i)))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
oldi = 0
for i in range(N):
    if S[oldi] == S[i]:
        pass
    else:
        c += (i-oldi)*(i-oldi-1)//2
        oldi = i
c += (N-oldi)*(N-oldi-1)//2
print(c)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = [str(sorted(input())) for _ in range(N)]
ans = 0
for v in Counter(S).values():
    ans += v*(v-1) // 2

print(ans)

Submission status: Accepted

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

def nPr(n, r):
   """
   Calculate the number of permutation (nPr = n!/(n-r)!).
   The parameters need to meet conditions of n >= r >= 0.
   It returns 1 if r == 0, which means there is one pattern
   to choice 0 items out of the number of n.
   """
   num = 1
   for i in range(n, n-r, -1):
       num *= i
   return num

def nCr(n, r):
    """
    Calculate the number of combination (nCr = nPr/r!).
    The parameters need to meet the condition of n >= r >= 0.
    It returns 1 if r == 0, which means there is one pattern
    to choice 0 items out of the number of n.
    """

    # 10C7 = 10C3
    r = min(r, n-r)

    # Calculate the numerator.
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i

    # Calculate the denominator. Should use math.factorial?
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i

    return numerator // denominator


def solve(N: int, s: "List[str]"):
    for i in range(N):
        l = list(s[i])
        l.sort()
        s[i] = ''.join(l)

    from collections import Counter

    c = Counter(s)
    ans = 0
    # print(c.most_common())
    for k, v in c.most_common():
        if( v >= 2):
            ans += nCr(v,2)

    print(ans)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
from operator import mul
from functools import reduce

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

n=int(input())
l=[input() for i in range(n)]
for i in range(n):
	l[i]="".join(sorted(l[i]))
l.sort()
cnt=ans=0
ref=[]
for i,j in zip(l,l[1:]):
	if i==j:
		cnt+=1
	else:
		ref.append(cnt)
		cnt=0
ref.append(cnt)
for i in ref:
	if i!=0:
		ans+=cmb(i+1,2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict

d = defaultdict(int)

N = int(input())
SS = [0]*N
for _ in range(N):
    SS[_] = input()

for S in SS:
    o = [0]*26
    for s in S:
        o[ord(s)-97] += 1
    d[''.join(map(str,o))] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
def solve():
    n = int(input())
    m = defaultdict(int)
    for _ in range(n):
        m["".join(sorted(input()))] += 1
    ans = 0
    for s, i in m.items():
        if i != 1:
            ans += i * (i - 1) // 2
    print(ans)
solve()

Submission status: Accepted

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

for _ in range(N):
  s_list = list(input())
  s_list.sort()
  S = ''.join(s_list)
  if S in s_dict:
    s_dict[S] += 1
  else:
    s_dict[S] = 1
ans = 0
for value in s_dict.values():
  if value > 1:
    ans += cmb(value, 2)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    ans = 0
    d = defaultdict(lambda: 0)
    N = int(input())
    for _ in range(N):
        s = [c for c in str(input().strip())]
        s.sort()
        s = tuple(s)
        if s in d:
            ans += d[s]
        d[s] += 1

    return ans


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

Submission status: Accepted

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

a+=int((1+tmp)/2*tmp)
print(a)

Submission status: Accepted

Solution code:
n = int(input())

strings = [input() for _ in range(n)]

st2 = [[]] * n
for i in range(n):
	st2[i] = sorted(strings[i])
st2.sort()

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

ans += int(same * (same-1)//2)
print(ans)

Submission status: Accepted

Solution code:
s=[]
a=[]
n=int(input())
for i in range(n):
	x=input()
	s.append(x)
	aa=[]
	for j in range(27):
		bb=x.count(chr(97+j))
		aa.append(bb)
	a.append(aa)
a.sort()
ans=0
cnt=0
for i in range(n-1):
	if a[i]==a[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
dic = defaultdict(int)

N = int(input())
for _ in range(N):
    S = input()
    A = []
    for s in S:
        A.append(s)
    A.sort()
    word = ""
    for a in A:
        word += a
    dic[word] += 1

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

def main():
  n=I()
  l=[]
  for i in range(n):
    a=list(S())
    a.sort()
    l.append(a)

  l.sort()

  a=l[0]
  c=1
  ans=0
  for x in l[1:]:
    if a==x:
      c+=1
    else:
      ans+=nCr(c,2)
      c=1
      a=x

  ans+=nCr(c,2)
  c=1
  a=x
  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
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


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

d = {}
for s in S:
    s = "".join(sorted(s))
    if s in d:
        d[s] += 1
    else:
        d[s] = 1
ans = 0
for v in d.values():
    if v > 1:
        ans += cmb(v, 2)
print(ans)

Submission status: Accepted

Solution code:


words = lambda t : list(map(t, input().split()))
n = int(input())
d = {}

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

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

print(ans)

Submission status: Accepted

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

def readInts():
  return [int(s) for s in input().split(" ")]

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

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

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for i in range(N):
    S = input()
    S = "".join((sorted(list(S))))
    d[S] += 1
ans = 0
for val in d.values():
    ans += val * (val - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ls = []
for i in range(n):
    ls.append("".join(sorted(input())))
valus = Counter(ls).values()
cnt=0
for v in valus:
    for vs in range(v):
        cnt += vs
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(list(input()))
for i in range(N):
	s[i].sort()
s.sort()
total = 0
num = 0
for i in range(N - 1):
	for x in range(10):
		if s[i][x] != s[i+1][x]:
			total += num*(num+1)//2
			num = 0
			break
		if x == 9:
			num += 1
total += num*(num+1)//2

print(total)

Submission status: Accepted

Solution code:
n = int(input())
d = dict()
res = 0
for i in range(n):
    ss = input()
    ss = list(ss)
    ss.sort()
    ss = ''.join(ss)
    if d.__contains__(ss):
        res += d[ss]
        d[ss]+=1
    else:
        d[ss] = 1
print(res)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

n = int(input())
s = [sys.stdin.readline().rstrip() for _ in range(n)][::-1]

d = defaultdict(int)

result = 0
for i in range(n):
    num = ''.join(sorted(s[i]))
    result += d[num]
    d[num] += 1

print(result)

Submission status: Accepted

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

dic = {}

cnt = 0
for i in range(n):
    s = input()
    ent = []
    lst = list(s)
    lst.sort()
    for c in lst:
        ent.append(c)

    t = tuple(ent)

    if t in dic:
        dic[t] += 1
    else:
        dic[t] = 1

for k in dic:
    v = dic[k]
    if v > 1:
        for i in range(v):
            cnt += i


print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
Ts = []
for i in range(N):
  S = input()
  T = "".join(sorted(S))
  Ts.append(T)
Ts.sort()
r = 0
c = 1
for i in range(N-1):
  if Ts[i] == Ts[i+1]:
    c += 1
  else:
    r += c*(c-1)//2
    c = 1
r += c*(c-1)//2
print(r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def cmp2(n):
    return n * (n-1) // 2

n = int(input())
s = {}

for i in range(n):
    si = input()
    anagram = "".join(sorted(si))
    if anagram in s:
        s[anagram] += 1
    else:
        s[anagram] = 1

cnt = 0
for c in s.values():
    cnt += cmp2(c)

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from collections import Counter
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input())

    c = Counter()
    for _ in range(n):
        s = input()
        a = [0] * 26

        for h in s:
            a[ord(h) - 97] += 1

        c[tuple(a)] += 1

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

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
import math
import sys

sys.setrecursionlimit(10000000)

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

def main():
    N = int(input())
    S = []
    for i in range(N):
        s = input()
        s = sorted(s)
        S.append(s)
        
    S = sorted(S)
    ans = 0
    num = 0
    for i in range(N):
        if i < N - 1 and S[i] == S[i + 1]:
            num += 1
        else:
            if num == 0:
                continue
            else:
                ans += combinations_count(num + 1, 2)
            num = 0
    print(ans)
                

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def combi(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

N = I()
data = []

for _ in range(N):
  s = [i for i in S()]
  s.sort()
  data.append("".join(map(str,s)))

data.sort()
#print(data)

tmp = ""
cnt = 0
num = 1
for i in range(N):
  #print(tmp)
  if tmp == data[i]:
    num += 1
    continue
  else:
    if num != 1:
      cnt += combi(num,2)
    tmp = data[i]
    num = 1
  #print(cnt)
else:
  if num != 1:
    cnt += combi(num,2)
  tmp = data[i]
  num = 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

dic = {}

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

result = 0
for key in dic:
    result += (dic[key])*(dic[key]-1)/2

print(int(result))

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    ls = ["".join((sorted(list(input())))) for _ in range(n)]
    ls = sorted(ls)


    i =0
    cnt = 1
    ans = 0
    while 1:
        if ls[i]==ls[i+1]:
            ans += cnt
            cnt +=1
            i +=1
        else:
            cnt = 1
            i += 1
        if i == n-1:
            break
    print(ans)



       
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
d = defaultdict(lambda: 0)

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

print(max(sum([i * (i - 1) // 2 for i in d.values()]), 0))

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = []
for i in range(n):
    x = input()
    y = collections.Counter(x)
    x = tuple(sorted(list(y.items())))
    s.append(x)
s_count = collections.Counter(s)
s_num = list(s_count.values())
ans = 0
for i in s_num:
    z = 0
    if i >= 2:
        z = int(i*(i-1)/2)
    ans += z
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lst = {}
for i in range(N):
    base = 0
    s = input()
    for j in range(10):
        base += 10**(ord(s[j])-ord('A'))
    if base not in lst:
        lst[base] = 0
    lst[base] += 1
ans = 0
for v in lst.values():
    ans += v*(v-1)/2
print(int(ans))

Submission status: Accepted

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


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

cases = 0
for key, val in words.items():
    if val == 1:
        continue
    cases += (val * (val - 1) // 2)

print(cases)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
Ss = {}
for i in range(N):
    s = ''.join(sorted(list(input().rstrip("\n"))))
    if s in Ss:
        Ss[s] += 1
    else:
        Ss[s] = 1

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

print(c)

Submission status: Accepted

Solution code:
N = int(input())
S = [(input()) for i in range(N)]
A = {}
B = {}
for i in range(N):
    tmp = sorted(S[i])
    mojiretu = ' '
    
    for x in tmp:
        mojiretu += x
    if mojiretu in A:
        A[mojiretu] += B[mojiretu]
        B[mojiretu] += 1
    else:
        B[mojiretu] = 1
        A[mojiretu] = 0
answer = 0
for i in A:
    answer += A[i]
print(answer)

Submission status: Accepted

Solution code:
from itertools import groupby
N = int(input())
S = [sorted(input()) for _ in range(N)]
S.sort()
ans = 0
for _, g in groupby(S):
    l = len(list(g))
    ans += l * (l-1) // 2
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
def cmb(n,r):  #nCr = n!/(n-r)!r!
    retVal = 1
    for i in range(n-r+1,n+1):
        retVal *= i
    for i in range(2,r+1):
        retVal //= i
    return retVal
def main():
    from collections import defaultdict
    N = int(input())
    d = defaultdict(lambda:0)
    base = ord('a')
    for _ in range(N):
        s = input().strip()
        num = 0
        for c in s:
            num += 26 ** (ord(c) - base)
        d[num] += 1
    ans = 0
    #print(d.values())
    for n in d.values():
        if n >= 2:
            ans += cmb(n,2)
    print(ans)

        

    





    pass
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
d = []
for i in range(n):
    s = list(input())
    s.sort()
    d.append(s)
d.sort()
d.append("")
cnt = []    
c = 1
for i in range(n):
    if d[i+1] == d[i]:
        c += 1    
    else:
        if c > 1:
            cnt.append(c)
        c = 1
ans = 0
for c in cnt:
    ans += c*(c-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = set()
cnt = 0

def createStr(counter):
    ret = ""
    keys = list(counter.keys())
    keys.sort()
    for key in keys:
        ret += key
        ret += str(counter[key])
    return ret

ret = {}
for i in range(N):
    s = Counter(input())
    s = createStr(s)
    if s in ret.keys():
        ret[s] += 1
    else:
        ret[s] = 1

for i in ret.values():
    cnt += i*(i-1)//2

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = ['']*n
for i in range(n):
    st = list(input())
    st.sort()
    s[i] = st
s.sort()


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

Submission status: Accepted

Solution code:
N = int(input())
S = []

for n in range(N):
    tmp = list(input())
    tmp.sort()
    s = "".join(tmp)
    S.append(s)

S.sort()

ans = 0
count = 0

for i in range(N - 1):
    if S[i] == S[i+1]:
        count += 1
        ans += count
    else:
        count = 0

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
t={}
for i in range(N):
    s = input()
    s = list(s)
    s.sort()
    s = ''.join(s)
    if t.get(s, None) == None:
        t[s] = 1
    else:
        t[s] += 1

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

Submission status: Accepted

Solution code:
n=int(input())
s=[list(input()) for _ in range(n)]
for i in range(n):
    s[i].sort()
s.sort()
c=0
x=1
for i in range(1,n):
    if s[i]==s[i-1]:
        x+=1
    else:
        c+=(x*(x-1)//2)
        x=1
c+=(x*(x-1)//2)
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
dic1 = defaultdict(int)
x = ord("a")
for i in range(N):
    s = input()
    ls = [0]*26
    for j in range(10):
        ind = ord(s[j])-x
        ls[ind] += 1
    ls = tuple(ls)
    dic1[ls] += 1
ans = 0
for k in dic1.keys():
    x = dic1[k]
    ans += x*(x-1)//2
print(ans)

Submission status: Accepted

Solution code:

box=[list(input()) for i in range(int(input()))]
count=0
ans_box={}
for i in range(len(box)):
    line=sorted(box[i])
    line="".join(line)
    if line in ans_box:
        ans_box[line] +=1
    elif line not in ans_box:
        ans_box[line]=0

for v,m in ans_box.items():
    if m>=1:
        for i in range(m+1):
            count +=i

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
cnt = defaultdict(int)
for _ in range(n):
    s = list(input())
    s.sort()
    s = tuple(s)
    cnt[s] += 1
print(sum(v * (v - 1) // 2 for v in cnt.values() if v > 1))

Submission status: Accepted

Solution code:
import collections

n = int(input())
ss = []

for i in range(n):
    s = input()
    s = sorted(s)
    po = ""
    for i in range(len(s)):
        po += s[i]
    ss.append(po)
    
ss = sorted(ss)


ans = 0
c = collections.Counter(ss)
#print( c.most_common() )
go = c.most_common()
for i in range(len(go)):
    k = go[i][1]
    ans += int((k-1)*k/2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
strings=[]
for _ in range(n):
    s=input()
    s=sorted(s)
    s="".join(s)
    strings.append(s)
c=Counter(strings)
ans=0
for i,v in c.items():
    ans+=int(v*(v-1)*0.5)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
    s1 = sorted(input())
    s2 = ''
    for j in s1:
        s2 += j
    s.append(s2)

s.sort()

ans = 0
count = 1
for i in range(n-1):
    if 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:
N = int(input())
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alphanum = dict()
for i in range(26): alphanum[alphabet[i]] = i

fact = [0] * (N + 1)
for i in range(1, N + 1): fact[i] = fact[i-1] + i

wordtype = dict()
for _ in range(N):
    s = input()
    s_num = [0] * 26
    for i in range(len(s)):
        s_num[alphanum[s[i]]] += 1
    s_str = ""
    for i in range(26):
        s_str += alphabet[i] + str(s_num[i])
    
    if s_str in wordtype: wordtype[s_str] += 1
    else: wordtype[s_str] = 1

ans = 0
for key in wordtype:
    ans += fact[wordtype[key] - 1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = {}
for _ in range(N):
    s = list(input())
    s.sort()
    s = tuple(s)
    if s in S:
        S[s] += 1
    else:
        S[s] = 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math
import sys

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

input = sys.stdin.readline

N = int(input().strip())
dic = defaultdict(int)
for _ in range(N):
    string = "".join(sorted(list(input().strip())))
    dic[string] += 1

count = 0
for key in dic.keys():
    n = dic[key]
    if dic[key] >= 2:
        count += comb(dic[key], 2)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
d = {}
for i in range(n):
    a = str(input())
    a = sorted(a)
    a = tuple(a)
    
    if a in d:
        d[a] +=1
    else:
        d[a] = 1

ans = 0
for k in d:
    v = d[k]
    if v>1:
        ans += (1+v-1)*(v-1)/2
        
print(int(ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
from collections import defaultdict

def main():
	n = int(input().strip())
	st = defaultdict(int)
	for i in range(n):
		s = input().strip()
		d = []
		for c in s:
			d.append(ord(c))
		d.sort()
		st[tuple(d)] += 1
	ans = 0
	for v in st.values():
		if v == 1:
			continue
		ans += v*(v-1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n = int(input())
s = sorted([''.join(sorted(input())) for _ in range(n)])
# print(s)

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
from collections import  defaultdict
d = defaultdict(int)
for i in range(n):
    d["".join(sorted((input())))] += 1
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
ret = 0
for k in d.values():
    if k == 1:
        continue
    ret += combinations_count(k,2)
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
S = sorted([''.join(sorted(input())) for _ in range(N)])
prev = ''
ans = 0
n = 0
for i in range(N):
    if S[i]!=prev:
        ans += n*(n-1)//2
        prev=S[i]
        n=1
    else:
        n+=1
ans += n*(n-1)//2
 
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

ans = []

for _ in range(N):
    s = list(input())
    s.sort()
    s = ''.join(s)
    ans.append(s)
ans.sort()

now = ans[0]
count = 1
ansans = 0
for a in ans[1:]:
    if a == now:
        count += 1
    else:
        now = a
        tmp = count * (count - 1) // 2
        count = 1
        ansans += tmp

tmp = count * (count - 1) // 2
count = 1
ansans += tmp

print(ansans)

Submission status: Accepted

Solution code:
from collections import defaultdict
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

cnt = defaultdict(int)

N = int(input())

buff = []
for i in range(N):
    _b = list(input())
    _b.sort()
    cnt[''.join(_b)] += 1

ans = 0
for el in cnt:
    if cnt[el] > 1:
        ans += cmb(cnt[el], 2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

print(ans)