Problem:


Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
3
acornistnt
peanutbomb
constraint


Sample output:
1

Submission status: Accepted

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

def createStr(intput_str):
    ret = ""
    intput_str = sorted(intput_str)
    for s in intput_str:
        ret += s
    return ret

ret = {}
for i in range(N):
    s = createStr(input())
    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:
from collections import Counter
n=int(input())
c=[''.join(sorted(input())) for _ in range(n)]
d=Counter(c)
an=0

for i in d.values():
    for j in range(i):
        an+=j

print(an)

Submission status: Accepted

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

d = dict.fromkeys(strs, 0)
for s in strs:
    if s in d.keys():
        d[s] += 1
    else:
        d.setdefault(s, 1)

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

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
s = [''.join(sorted(input())) for i in range(N)]
d = defaultdict(lambda: 0)

for _s in s:
    d[_s] += 1

print(sum(map(lambda x: x * (x-1) // 2, d.values())))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
 
D = {}
for i in range(N):
    s = ''.join(sorted(input()))
    D.setdefault(s, 0)
    D[s] += 1
    
ans = 0
for v in D.values():
    if v > 1:
        for i in range(v):
            ans += i
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = sorted([sorted(input()) for i in range(n)])
a = []
last = s[0]
count = 1
for i in range(1, n):
    if s[i] == last:
        count += 1
    else:
        a.append(count)
        count = 1
    last = s[i]
a.append(count)
ans = 0
for i in a:
    if i > 1:
        ans += i * (i - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = 0
words = Counter(s)
for i in words.values():
    cnt += i*(i-1)//2

print(cnt)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  sdic = {}
  for _ in range(n):
    s = [_s for _s in sorted(input().strip())]
    s = ''.join(s)
    if s in sdic:
      sdic[s] += 1
    else:
      sdic[s] = 1
  ans = 0
  for k, v in sdic.items():
    ans += v*(v-1)//2
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
strs = []
for i in range(N):
  str = list(input())
  str.sort()
  strs.append("".join(str))

strs.sort()
# print(strs)

cnts = []
pre = strs[0]
cnt = 1
for str in strs[1:]:
  if str == pre:
    cnt += 1
  else:
    cnts += [cnt]
    pre = str
    cnt = 1
cnts += [cnt]

# print(cnts)

ans = 0
for cnt in cnts:
  if cnt == 1:
    continue
  ans += cnt * (cnt - 1) / 2
  
print(int(ans))

Submission status: Accepted

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

mP = dict()
for i in range(N):
    s = "".join(sorted(list(vS[i])))
    if s in mP.keys():
        mP[s] += 1
    else:
        mP[s] = 1

count = 0
vC = []
for key in mP:
    if (mP[key] < 2):
        continue
    vC.append(mP[key])
    count += 1

res = 0
for x in vC:
    res += x * (x-1) // 2

print(res)

Submission status: Accepted

Solution code:
import collections

N = int(input())
s = [0] * N
for i in range(N):
    s[i] = list(input())
    s[i].sort()
    s[i] = ''.join(s[i])

c = collections.Counter(s)
l = list(c.values())
#print(l)

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

print(ans)

Submission status: Accepted

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

count = 0
dic1 = {}
#print(s)
for i in range(n):
    #print(s[i])
    so = sorted(s[i])
    so = ''.join(so)
    if so in dic1:
        count += dic1[so]
        dic1[so] += 1
    else:
        dic1[so] = 1
    #print(count, dic1)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
s = sorted(("".join(sorted(list(input()))) for _ in range(N))) + [""]

ans = 0

w = None
cnt = 0
path = 0
for x in s:
    if x != w:
        ans += path
        if x is None:
            break
        path = 0
        cnt = 1        
        w = x
    else:
        path += cnt
        cnt += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
m = {}
for i in range(n):
	s = input()
	l = list(map(str, s))
	s = "".join(sorted(l))
	if s in m:
		m[s] += 1
	else:
		m[s] = 1
res = 0
for i in m.keys():
	if m[i] > 1:
		res += m[i] * (m[i] - 1) // 2
print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
ana_dict = defaultdict(int)
n_pair = 0
for _ in range(n):
    s = input()
    t = [0,] * 26
    for c in s:
        t[ord(c) - ord('a')] += 1
    t = tuple(t)
    if t in ana_dict:
        n_pair += ana_dict[t]
    ana_dict[t] += 1
print(n_pair)

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline
def main():
    _, *keys = map(lambda s: ''.join(sorted(s.rstrip())), open(0))
    # _, *keys = map(lambda s: tuple(sorted(s)), open(0))
    primes = {}
    res = 0
    for key in keys:
        if key in primes:
            res += primes[key]
            primes[key] += 1
        else:
            primes[key] = 1
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline
def main():
    # _, *keys = map(lambda s: ''.join(sorted(s.rstrip())), open(0))
    _, *keys = map(lambda s: tuple(sorted(s)), open(0))
    primes = {}
    res = 0
    for key in keys:
        if key in primes:
            res += primes[key]
            primes[key] += 1
        else:
            primes[key] = 1
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
ss = [sorted(input()) for i in range(n)]
ss.sort()

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
arr = []
d = defaultdict(int)
count = 0

for i in range(n):
  arr.append(input())
  arr[i] = "".join(sorted(arr[i]))
  count += d[arr[i]]
  d[arr[i]] += 1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC137C - Green Bin
import sys
input = sys.stdin.readline

from collections import Counter


def main():
    N = int(input())
    A = Counter("".join(sorted(input())) for _ in range(N))
    ans = sum(i * (i - 1) // 2 for i in A.values())
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

MAP = "0123456789A"
freqs = {}
for i in range(N):
  s = input()

  template = [0 for x in range(26)]
  for j in range(len(s)):
    template[ord(s[j]) - ord("a")] += 1
  freq_str = ""
  for j in range(26):
    freq_str += MAP[template[j]]
  freqs[freq_str] = freqs.get(freq_str, 0) + 1

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

Submission status: Accepted

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


def main():
    N = int(input())
    A = Counter("".join(sorted(input())) for _ in range(N))
    ans = sum(i * (i - 1) // 2 for i in A.values())
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = sorted([sorted(input()) for _ in range(N)])
ans = [1]
for i in range(N - 1):
    if S[i] == S[i + 1]:
        ans[-1] += 1
    elif ans[-1] > 1:
        ans += [1]
ret = 0
for a in ans:
    ret += a * (a - 1) // 2
print(ret)

Submission status: Accepted

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

dic = {}
for i in range(n):
    if s[i] in dic.keys():
        dic[s[i]] += 1
    else:
        dic[s[i]] = 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = {}
for _ in range(N):
    S = "".join(sorted(input()))
    if S in D:
        D[S] += 1
    else:
        D[S] = 1
 
out = 0
for i in D.values():
    out += i * (i-1) //2
print(out)

Submission status: Accepted

Solution code:
N = int(input())
l = [input() for _ in range(N)]
ans = 0
dic = {}
for n in range(N):
    new = sorted(l[n])
    if not str(new) in dic:
        count = 1
        dic[str(new)] = count
    else:
        ans += dic[str(new)]
        dic[str(new)] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
c = Counter(''.join(sorted(input())) for _ in range(int(input())))
print(sum(x * (x - 1) // 2 for x in c.values()))

Submission status: Accepted

Solution code:
c = {}
n = int(input())
r = 0
for _ in range(n):
    s = ''.join(sorted(input()))
    v = c.get(s, 0)
    r += v
    c[s] = v + 1
print(r)

Submission status: Accepted

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

d = dict()

for st in s:
    if st in d:
        d[st]+=1
    else:
        d[st]=1

ans=0

for i in d:
    k=d[i]
    ans+=(k*(k-1))//2

print(ans)

Submission status: Accepted

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

#duplicate=[x for x in set(stl) if stl.count(x) > 1]

t = {}
for i in range(num):
  if stl[i] in t:
    t[stl[i]] += 1
  else:
    t[stl[i]] = 1
print(sum(t[k] * (t[k] -1) // 2 for k in t))

Submission status: Accepted

Solution code:
N = int(input())
count = 0
DIC = {}
for i in range(N):
    j = ''.join(sorted(input()))
    if j in DIC:
        count += DIC[j]
        DIC[j] += 1
    else:
        DIC[j] = 1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
l=[['0']*10 for _ in range(n)]
for i in range(n):
        a=str(input())
        for j in range(10):
                l[i][j]=a[j]
        l[i].sort()
l.sort()

c=0
d=[]
for j in range(n-1):
        if l[j]==l[j+1] and j==n-2:
                c+=2
                d.append(c)
        elif l[j]==l[j+1]:
                c+=1
        else:
                c+=1
                d.append(c)
                c=0
aa=0
for k in range(len(d)):
        aa+=d[k]*(d[k]-1)//2
print(aa)

Submission status: Accepted

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

from collections import Counter

def ri():
    return int(input())

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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
dic={}
cnt=0
for i in range(N):
    temp=' '.join(sorted(input()))
    if temp in dic:
        dic[temp]+=1
    else:
        dic[temp]=1
for k,v in dic.items():
    cnt+=v*(v-1)//2
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

lst = []

for i in range(N):
    lst.append("".join(sorted(list(input()))))


d={}
count = 0

for i in lst:
  if not i in d:
    d[i] = 1
    continue
  else:
    d[i] = d[i] + 1
    count = count + d[i]-1


print(count)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
l = {}
ans = 0
for i in range(N):
    tmp = list(input())
    tmp.sort()
    tmp = "".join(tmp)
    if tmp in l.keys():
        l[tmp] += 1
        ans += l[tmp]
    else: 
        l[tmp] = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
  tmp = input()
  s.append(tmp)
  
for i in range(n):
  l = [c for c in s[i]]
  l.sort()
  s[i] = ''.join(l)
  
import collections
d = dict(collections.Counter(s))
#print(d)

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

ans = 0
for key in d.keys():
  value = d[key]
  if value != 1:
    ans += henkan(value)
    
print(ans)

Submission status: Accepted

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

memo = {}
for x in s:
    if x not in memo:
        memo[x] = 1
    else:
        memo[x] += 1

print(sum(x*(x-1)//2 for x in memo.values()))

Submission status: Accepted

Solution code:
n = int(input())

l = []
for i in range(n):
    s = list(input())
    s.sort()
    s = "".join(s)
    l.append(s)

m = {}
ans = 0

for s in l:
    if s in m:
        m[s] += 1
        ans += m[s]
    else:
        m[s] = 0

print(ans)

Submission status: Accepted

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

lst = ["".join(sorted(list(input()))) for i in range(N)]
c = collections.Counter(lst)

ans = 0
for k,v in c.items():
  if (v > 1):
    ans += combinations_count(v, 2)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

class hashabledict(dict):
    def __hash__(self):
        return hash(tuple(sorted(self.items())))

N = int(input())
words = [hashabledict(dict(Counter(input()))) for _ in range(N)]
ans = 0
counter_hash = {}
for counter in words:
    if counter not in counter_hash.keys():
        counter_hash[counter] = 1
    else:
        counter_hash[counter] += 1
 
for count in counter_hash.values():
    ans += int((count * (count-1)) / 2)
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
    
v = list(Counter(s).values())
ans = []
for i in range(len(v)):
    if v[i] >= 2:
        ans.append((v[i] * (v[i]-1))/2)

print(int(sum(ans)))

Submission status: Accepted

Solution code:
N = int(input())
s_list = [input() for _ in range(int(N))]
import math
ok = {}
count = 0
for s in s_list:
    s = ''.join(sorted(s))
    if s in ok:
        ok[s] += 1 
    else:
        ok[s] = 1

for val in ok:
    prev = (ok[val] - 1)
    if prev != 0:
        count += math.floor(ok[val] * prev / 2)
print(count)

Submission status: Accepted

Solution code:
import collections
import math


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


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

ss = []

for i in range(n):
    ss.append(''.join(s[i]))

c = collections.Counter(ss)
d = sorted(list(c.values()), reverse=True)

sum = 0

for i in d:
    if i == 1:
        print(sum)
        exit()
    else:
        a = combinations_count(i, 2)
        sum += a

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
d={}
c=0
for i in open(0):i=tuple(sorted(i));t=d.get(i,0);c+=t;d[i]=t+1
print(c)

Submission status: Accepted

Solution code:
n =int(input())

s=[]
ans=0
count=1

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

s.sort()
for i in range(n-1):
    if s[i] == s[i+1]:
        count=count+1
    else:
        ans=ans+count*(count-1)/2
        count=1

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

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

num = int(input())
range_arr = range(num)

strs = [[] for i in range_arr]
for i in range_arr:
    strs[i] = input()

def sort(str):
    str_arr = list(str)
    str_arr.sort()
    return ''.join(str_arr)

new_strs = list(map(lambda v: sort(v), strs))
strs_length = len(new_strs)

sum = 0
counter = Counter()
for tmp_str in new_strs:
    sum += counter[tmp_str]
    counter[tmp_str] += 1

print(sum)

Submission status: Accepted

Solution code:
from collections import Counter

num = int(input())
range_arr = range(num)

strs = [[] for i in range_arr]
for i in range_arr:
    strs[i] = input()

def conv(str):
    str_arr = list(str)
    str_arr.sort()
    return ''.join(str_arr)

new_strs = list(map(lambda v: conv(v), strs))

_sum = 0
counter = Counter()
for tmp_str in new_strs:
    _sum += counter[tmp_str]
    counter[tmp_str] += 1

print(_sum)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
existed = {}
c = 0
for i in range(n):
  sorted_s = ''.join(sorted(s[i]))

  if sorted_s in existed:
    c += existed[sorted_s]
    existed[sorted_s] += 1
  else:
    existed[sorted_s] = 1
print(c)

Submission status: Accepted

Solution code:
N = int(input())
dicts = {}
for i in range(N):
    s = "".join(sorted(input()))
    dicts[s] = dicts[s] + 1 if s in dicts.keys() else 1

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

Submission status: Accepted

Solution code:
while True:
    try:
        N = int(input())
        result = dict()
        for i in range(N):
            S = input()
            ss = ''.join(sorted(S))
            if ss not in result:
                result[ss] = 0
            result[ss] += 1
        print(sum(map(int, map(lambda x: x*(x-1)/2, result.values()))))
    except EOFError:
        break

Submission status: Accepted

Solution code:
from collections import Counter
#from itertools import combinations
n = int(input())
lst = ["".join(sorted(list(input()))) for i in range(n)]
#lst2 = 1#Counter(lst)
#for i in len(lst):
#  print(i.values)


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

#print(cmb(4,2),0,0,0)
cnt = 0
for i in Counter(lst).values():
  if i == 1:
    continue
  cnt += cmb(i, 2)
"""
for i in range(len(lst)-1):
  for j in range(i+1,len(lst)):
    #print(0)
    if lst[i]==lst[j]:
      cnt += 1
"""
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
arr=[input() for _ in range(n)]
dic={}
alphabet='abcdefghijklmnopqrstuvwxyz'
for s in arr:
  count=[0]*26
  for i in range(10):
    for j in range(26):
      if s[i]==alphabet[j]:
        count[j]+=1
        break
  count=tuple(count)
  if count not in dic:
    dic[count]=1
  else:
    dic[count]+=1
ans=0
for val in dic.values():
  ans+=val*(val-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections

n=int(input())
arr=[input() for _ in range(n)]
dic={}
for s in arr:
  count=[0]*26
  tmp=collections.Counter(s)
  for char in tmp.keys():
    count[ord(char)-ord('a')]+=tmp[char]
  count=tuple(count)
  if count not in dic:
    dic[count]=1
  else:
    dic[count]+=1
ans=0
for val in dic.values():
  ans+=val*(val-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for _ in range(n)]
existed = {}
cnt = 0
for i in range(n):
  sorted_s = ''.join(sorted(s[i]))
  if sorted_s in existed:
    cnt += existed[sorted_s]
    existed[sorted_s] += 1
  else:
    existed[sorted_s] = 1
print(cnt)

Submission status: Accepted

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


input = sys.stdin.readline

n = int(input())
d = defaultdict(lambda: 0)

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

ans = 0
for key in d.keys():
    if d[key] >= 2:
        ans += cmb(d[key], 2)
print(int(ans))

Submission status: Accepted

Solution code:
import sys

n=int(input())
l=["".join(sorted(sys.stdin.readline().strip())) for i in range(n)]


res={}
ans=0
 
for j in range(n):
    ll=list(l[j])
    ll.sort()
    aa="".join(ll)
    if aa in res:
        ans+=res[aa]
        res[aa]+=1
    else:
        res[aa]=1

print(int(ans))

Submission status: Accepted

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

for i in range(N):
    str = ''.join(sorted(input()))
    if str in S.keys():
        S[str] += 1
    else:
        S.setdefault(str, 1)

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

print(cnt)

Submission status: Accepted

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

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

for i in range(n):
    l.append("".join(sorted(str(input()))))

l.sort()

p = 0
s = 1
f = 0

for i in range(n-1):
    if l[i] == l[i+1] and i != n-2:
        s += 1
        f = 1

    elif l[i] != l[i+1] and f == 1:
        f = 0
        p += int(s * (s-1) / 2)
        s = 1

    elif l[i] == l[i+1] and i == n-2:
        s += 1
        f = 0
        p += int(s * (s-1) / 2)


print(p)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline().strip())
list = [''.join(sorted(sys.stdin.readline().strip())) for i in range(n)]
d = {}
ans = 0
for x in list:
  if x in d:
    ans += d[x]
    d[x] += 1
  else:
    d[x] = 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
alldict = {}
nsame = 0
for i in range(N):
  tmp = list(input())
  tmp.sort()
  if ''.join(tmp) in alldict:
    alldict[''.join(tmp)] = alldict[''.join(tmp)] + 1
  else:
    alldict[''.join(tmp)] = 1

from operator import mul
from functools import reduce
def cmb(n,r):
    if n == 1:
        return 0
    r = min(n-r,r)
    if r == 0:
        return 1
    ov = reduce(mul, range(n, n-r, -1))
    un = reduce(mul, range(1, r+1))
    return ov // un

print(sum([cmb(i, 2) for i in alldict.values()]))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    words = {}
    for i in range(n):
        s = list(input())
        s.sort()
        s = tuple(s)
        if s not in words.keys():
            words[s] = 1
        else:
            words[s] += 1

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

    print(ans)


main()

Submission status: Accepted

Solution code:
n = int(input())
sdict = {}
for i in range(n):
    w = input()
    _w = ''.join(sorted(w))
    if _w not in sdict.keys():
        sdict[_w] = 1
    else:
        sdict[_w] += 1

def nc2(n):
    return int(n * (n-1) / 2)

n_dup = sum([nc2(val) for val in sdict.values() if val > 1])
print(int(n_dup))

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
mydict = {}
ans = 0
for i in range(N):
    T = [str(x) for x in S[i]]
    T.sort()
    S[i] = ''.join(T)
for j in range(N):
    if S[j] in mydict.keys():
        ans += mydict[S[j]]
        mydict[S[j]] += 1
    else:
        mydict.setdefault(S[j],1)
print(ans)

Submission status: Accepted

Solution code:
def init_load(ipt):
    return "".join(sorted([x for x in ipt]))
 
# load
n = int(input())
s = [init_load(input()) for _ in range(n)]
 
 
dictS = {}
for new_st in s:
    if new_st in dictS:
        dictS[new_st] += 1
    else:
        dictS[new_st] = 1
 
ans = sum([int(x * (x - 1) / 2) for x in dictS.values()])
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
dic = {}
count = 0
for i in range(N):
  temp = "".join(sorted(input()))
  if temp in dic:
    count += dic[temp]
    dic[temp] += 1
  else:
    dic[temp] = 1

print(count)

Submission status: Accepted

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

cnt = 0
dic = {}
for i in s:
    dic.setdefault(i, 0)
    cnt += dic[i]
    dic[i] += 1

print(cnt)

Submission status: Accepted

Solution code:
n =int(input())

S=[]
ans = 0
count=1

for i in range(n):
    tmp=input()
    S.append("".join(sorted(tmp)))

S.sort()

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

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

print(int(ans))

Submission status: Accepted

Solution code:
# 3.4.3

import re
from collections import deque
from functools import reduce
from itertools import permutations
from math import pi
from operator import itemgetter
from operator import mul
from operator import xor
from os import linesep
from queue import PriorityQueue
from sys import stdin


def rline() -> str:
    return stdin.readline().strip()


def rlines(hint: int = 1):
    ret = ['' for i in range(hint)]
    for i in range(hint):
        ret[i] = rline()
    return ret


def htokens(hint: int = 1):
    lns = rlines(hint)
    ret = list(map(lambda ln: ln.split(), lns))
    # if return value has one and only one element(list),
    if hint == 1:
        # then add an empty list.
        ret.append([])
    return ret


def vtokens(hint: int = 1):
    m, _ = htokens()
    wint = len(m)
    ret = [[None] * hint for i in range(wint)]
    for y in range(hint):
        if y != 0:
            m, _ = htokens()
        # convert horizontal to vertical
        x = 0
        for v in m:
            ret[x][y] = v
            x += 1
    # if return value has one and only one element(list),
    if wint == 1:
        # then add an empty list.
        ret.append([])
    return ret


def rint(radix: int = 10) -> int:
    return int(rline(), radix)


def hints(hint: int = 1, radix: int = 10):
    ret = htokens(hint)
    for i in range(len(ret)):
        ret[i] = list(map(lambda v: int(v, radix), ret[i]))
    return ret


def vints(hint: int = 1, radix: int = 10):
    ret = vtokens(hint)
    for i in range(len(ret)):
        ret[i] = list(map(lambda v: int(v, radix), ret[i]))
    return ret


def mat(hint: int, wint: int):
    return [[None]*wint for i in range(hint)]


def filllist(lst, value: int) -> None:
    # destructive.
    for i in range(len(lst)):
        lst[i] = value


def isprime(n: int) -> bool:
    if n <= 1:
        return False
    if n in (2, 3, 5):
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False
    lst = int(n**0.5)
    f = 5
    while f <= lst:
        if n % f == 0:
            return False
        if n % (f + 2) == 0:
            return False
        f += 6
    return True


def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str:
    return lnsep.join(map(lambda row: fieldsep.join(map(str, row)), matrix))


def strbool(boolval: int) -> str:
    return ['No', 'Yes'][boolval]


def solve() -> None:
    # read here
    n = rint()
    ss = rlines(n)

    # solve here
    dict = {}
    for s in ss:
        k = ''.join(sorted(list(s)))
        if not k in dict.keys():
            dict[k] = 0
        dict[k] += 1

    # 2-combination of x.
    ans = sum(map(lambda x: 0 if x == 1 else x*(x-1)//2, dict.values()))

    # print here
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
d = dict()
for i in range(N):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if s in d:
        d[s] += 1
    else:
        d[s] = 1

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

print(ans)

Submission status: Accepted

Solution code:
def init_load(ipt):
    return "".join(sorted([x for x in ipt]))

# load
n = int(input())
s = [init_load(input()) for _ in range(n)]


dictS = {}
for new_st in s:
    if new_st in dictS:
        dictS[new_st] += 1
    else:
        dictS[new_st] = 1

ans = sum([int(x * (x - 1) / 2) for x in dictS.values()])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = []
for _ in range(N):
    s = list(input())
    s.sort()
    S.append(''.join(s))

c = Counter(S).most_common()

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

print(ans)