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

ORD_A = ord("a")


def count_chars(s):
    counter = [0 for _ in range(26)]
    for c in s:
        counter[ord(c) - ORD_A] += 1

    return counter


def main():
    n = int(input())
    anagram_counter = collections.Counter(tuple(count_chars(input())) for _ in range(n))

    combinations = sum(v * (v - 1) // 2 for v in anagram_counter.values())
    print(combinations)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections

ORD_A = ord("a")


def count_chars(s):
    counter = [0 for _ in range(26)]
    for c in s:
        counter[ord(c) - ORD_A] += 1

    return counter


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

    anagram_counter = collections.Counter(tuple(count_chars(input())) for _ in range(n))

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

    print(s)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
stdin = sys.stdin

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

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

r=1
ans=0

for i in range(1,N):
    if(S[i-1]==S[i]):
        r+=1
    elif(r>1):
        ans+=math.factorial(r)//(math.factorial(r-2)*math.factorial(2))
        r=1

if(r>1):
    ans+=math.factorial(r)//(math.factorial(r-2)*math.factorial(2))

print(ans)

Submission status: Accepted

Solution code:
def gen(chars):
  a = [0] * 26
  for c in chars:
    a[ord(c) - 97] += 1
  return tuple(a)

n = int(input())
m = dict()
r = 0
for s in map(gen, [input() for _ in range(n)]):
  m[s] = m.get(s, 0) + 1
  r += m[s] - 1
print(r)

Submission status: Accepted

Solution code:
n = int(input())
lst = []
pair = 0
for i in range(n):
	st = ''.join(sorted(list(input())))
	lst.append(st)
lst = sorted(lst)
lst += ['end']
pre = ''
tmp_cnt = 0
cnt = 0
for i in lst:
	if i == pre:
		tmp_cnt += 1
	else:
		cnt += sum([j for j in range(tmp_cnt + 1)])
		tmp_cnt = 0
	pre = i
print (cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = [''.join(sorted(list(input()))) for _ in range(N)]
 
from collections import Counter
a=Counter(S)
 
ans = 0
 
for i in a.values():
    if i>1:
        ans += i*(i-1)/2
 
print(int(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
S = [sorted(input()) for _ in range(N)]
S = sorted(S)
cnt = 0
x = S[0]
cnt_all = 0
for s in S[1:]:
    if x == s:
        cnt += 1
    elif cnt > 0:
        cnt += 1
        cnt_all += int(cnt*(cnt - 1)/2)
        cnt = 0
        x = s
    else:
        x = s

cnt += 1
cnt_all += int(cnt*(cnt - 1)/2)

print(cnt_all)

Submission status: Accepted

Solution code:
import os, sys, re, math

N = int(input())
S = dict()
for i in range(N):
    w = ''.join(sorted(input()))
    if w in S:
        S[w] += 1
    else:
        S[w] = 1

count = 0
for (w, n) in S.items():
    if n > 1:
        count += (n * (n - 1)) // 2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
ans = {}
for i in range(n):
    key = ''.join(sorted(input()))
    ans[key] = ans.get(key, 0) + 1
print(sum([v*(v-1)//2 for k, v in ans.items()]))  # 1=0, 2=1, 3=3, 4=6, ... n(n-1)//2

Submission status: Accepted

Solution code:
N = int(input())
ret = dict()
count = 0
for n in range(N):
    s = input()
    s = ''.join(sorted(s))
    c = ret.get(s, 0)
    count += c
    ret[s] = c + 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
ss = [input() for _ in range(n)]
d = {}
for s in ss:
    key = ''.join(sorted(s))
    d[key] = d.get(key, 0) + 1
print(sum(d[key] * (d[key] - 1) // 2 for key in d))

Submission status: Accepted

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

from collections import Counter
a=Counter(S)

ans = 0

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

print(int(ans))

Submission status: Accepted

Solution code:
def q3():
    N = int(input())
    from collections import deque
    S = deque()
    for _ in range(N):
        S.append(''.join(sorted(list(input()))))

    ans = 0
    d = {}  # key:文字列 value:出現回数
    for s in S:
        if s in d:
            ans += d[s]
            d[s] += 1
        else:
            d[s] = 1

    print(ans)


if __name__ == '__main__':
    q3()

Submission status: Accepted

Solution code:
import collections
N=int(input())
s=["".join(sorted(input())) for _ in range(N)]
c=collections.Counter(s)
ans=0
for si in set(s):
    n=c[si]
    ans+=n*(n-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
  l.append(str(sorted(input())))

d={}
for i in l:
  d.setdefault(i,0)
  d[i]+=1

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

Submission status: Accepted

Solution code:
N = int(input())
detayatsu = dict()
ans = 0
for i in range(N):
    S_i = "".join(sorted(list(input())))
    if S_i in detayatsu.keys():
        ans += detayatsu[S_i]
        detayatsu[S_i] += 1
    else:
        detayatsu[S_i] = 1
print(ans)

Submission status: Accepted

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

ss.sort()

total = 0
c = ss[0]
s = 0
for i in range(1, n):
    if c != ss[i]:
        total += (i-s) * (i-s-1) // 2
        c = ss[i]
        s = i

total += (n-s) * (n-s-1) // 2
print(total)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))

def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N = iip(False)

    d = {}
    for _ in range(N):
        s = iip_ord()
        s.sort()
        s = "".join([chr(i) for i in s])

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

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

    print(ans)


#####################################################ライブラリ集ここから

def iip(listed=True, num_only = True):
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iipt(l, listed=False, num_only = True):
    ret = []
    for i in range(l):
        ret.append(iip_ord(listed=listed, num_only=num_only))
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")

def fprint(s):
    for i in s:
        print(i)

def bitall(N):
    ret = []
    for i in range(2**N):
        a = []
        for j in range(N):
            a.append(i%2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):
    print(" ".join([str(i) for i in s]))

def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side="left"):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = list()
for i in range(n): l.append(str(sorted(input())))
d = {}
for i in l:
    d.setdefault(i, 0)
    d[i] += 1
cnt = 0
for i in d.values():
    cnt += i * (i - 1) // 2
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial

N = int(input())

def combinations_count(n, r):
    ''' 組み合わせ '''
    return factorial(n) // (factorial(n - r) * factorial(r))

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

ans = 0
for i in d.values():
    if i >= 2:
          ans += combinations_count(i, 2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

s = []
s = [input() for i in range(N)]


sort = lambda str: ''.join(sorted(str))
wa = lambda n : int(0.5 * n * (n + 1))


sorted_s = map(sort, s)
counter = {}
for si in sorted_s:
    counter[si] = 1 if not si in counter else counter.get(si) + 1

nums = [wa(num - 1) for num in counter.values()]
answer = sum(nums)

print(answer)

Submission status: Accepted

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

n = int(input())
dic = defaultdict(int)
for i in range(n):
    s = ''.join(sorted(input()))
    dic[s] += 1
count = 0
for key, value in dic.items():
    if value >= 2:
        count += cmb(value, 2)
print(count)

Submission status: Accepted

Solution code:
import math
N = int(input())
S = []
ans = 0
count = 1
for _ in range(N):
    s = list(input())
    s.sort()
    S.append(''.join(s))
S.sort()
S.append('a')

for i in range(N):
    if S[i] == S[i+1]:
        count += 1
    else:
        if count > 2:
            ans += math.factorial(count)//(math.factorial(count-2)*math.factorial(2))
        elif count == 2:
            ans += 1
        count =1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[]
for _ in range(N):
    s=input()
    S.append("".join(sorted(s)))
    #S.append(sorted(s))
#print(S)
re=0
dt={}
for i in range(N):
    if S[i] in dt:
        dt[S[i]]+=1
    else:
        dt[S[i]]=1
for v in dt.values():
    if v>=2:
        v-=1
        re+=(v*(v+1)//2)

print(re)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 17:18:48 2020
"""
from collections import Counter

N        = int( input() )
list_all = []
for nn in range(N):
    text = ''.join( sorted( str( input() ) ) )    
    list_all.append( text )

c     = Counter(list_all)
count = 0
for key in c.keys():
    if( c[key] >= 2 ):
        count += c[key] * (c[key]-1)//2

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
ans = 0
li = [0]*N
for i in range(N):
    s = list(input())
    s.sort()
    li[i] = ''.join(s)
c = Counter(li)
key = c.keys()
for i in key:
    if c[i] >= 2:
        ans += c[i]*(c[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s_input = [input() for i in range(n)]
s = [[s_input[i][j] for j in range(10)] for i in range(n)]
sum = 0
count = 1
for i in range(n):
    s[i] = sorted(s[i])
s = sorted(s)
i = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        count += 1
    else:
        sum += (count * (count - 1)) // 2
        count = 1
sum += (count * (count - 1)) // 2
print(sum)

Submission status: Accepted

Solution code:
#template
from collections import Counter
def inputlist(): return [int(j) for j in input().split()]
#template
N = int(input())
ans = 0
li = ['0']*N
for i in range(N):
    s = list(input())
    s.sort()
    li[i] = ''.join(s)
c = Counter(li)
key = c.keys()
for i in key:
    if c[i] >= 2:
        ans += c[i]*(c[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
slist = [""]*n
for i in range(n):
	slist[i] =sorted(input())
sslist=sorted(slist)
count=0
i = 0
while i < n-1:
	j=1
	if sslist[i]==sslist[i+1]:
		j=2
		while i+j<=n-1 and sslist[i]==sslist[i+j]:
			j=j+1
		count=count + j*(j-1)//2
#		print(j)
	i=i+j
#	print(sslist[i])
print(count)

Submission status: Accepted

Solution code:
n = int(input())

s = [""] * n
t = [""] * n

cnt = 0
flg = 0

for i in range(n):
    s[i] = input()
    t[i] = sorted(s[i])

t = sorted(t)

for i in range(n - 1):
    if t[i] == t[i + 1]:
        flg += 1
        cnt += flg
    else:
        flg = 0

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
dic={}
for i in range(n):
  wk={}
  s=input()
  for i in range(len(s)):
    if s[i] in wk:
      wk[s[i]]+=1
    else:
      wk[s[i]]=1
  list=sorted(wk.items(),key=lambda x:x[0])
  key="".join(map(str,list))
  if key in dic:
    dic[key]+=1
  else:
    dic[key]=1
ans=0
for i in dic.values():
  if i==1:
    continue
  else:
    ans+=i*(i-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
dic={}
for i in range(n):
  wk={}
  s=input()
  for i in range(len(s)):
    if s[i] in wk:
      wk[s[i]]+=1
    else:
      wk[s[i]]=1
  list=sorted(wk.items(),key=lambda x:x[0])
  key="".join(map(str,dict(list).items()))
  if key in dic:
    dic[key]+=1
  else:
    dic[key]=1
ans=0
for i in dic.values():
  if i==1:
    continue
  else:
    ans+=i*(i-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial, log, log10, log2
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
from datetime import datetime as dt

def main():
  n = int(readline())
  S = []
  for _ in range(n):
    S.append(''.join(sorted(input())))
  counter = Counter(S)
  ans = 0
  for i in counter.values():
    ans +=  i*(i-1)/2
  print(int(ans))
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
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())
anas = [''.join(sorted(input())) for _ in range(n)]
c = Counter(anas)


cnt = 0
for v in c.values():
    if v < 2:
        continue
    else:
        cnt += cmb(v, 2)

print(cnt)

Submission status: Accepted

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  clist = []
  for i in range(n):
    s = ''.join(sorted(input()))
    clist.append(s)
  d = {}
  for v in clist:
    if v in d:
      d[v] += 1
    else:
      d[v] = 1
  ans = 0
  for v, count in d.items():
    if count >= 2:
      ans += combinations_count(count, 2)
  print(ans)
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
from collections import Counter

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  mydict = {}

  for i in range(n):
    temp = list(input())
    temp.sort()
    temp = ''.join(temp)
    
    if temp not in mydict:
      mydict[temp] = 1
    else:
      mydict[temp] =  mydict[temp] + 1
      
  ans = 0
  for i in mydict.values():
    if i >= 2:
      ans = ans + i*(i-1)//2
    
  print(ans)
  
if __name__ == '__main__':
    main()

Submission status: Accepted

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

import math
ans = 0
for v in s.values():
  if v < 2: ans += v
  if v > 1: ans += v*(v+1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())

S = []
sum_ana = 0
for _ in range(N):
  s = sorted(input())
  S.append("".join(s))

S.sort()
ana = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    ana+= 1
    continue
  sum_ana += (ana*(ana+1))//2
  ana = 0
sum_ana += (ana*(ana+1))//2
print(sum_ana)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = [str(sorted(input())) for i in range(n)]
d = dict()
for el in s:
    if d.get(el) == None:
        d[el] = 1
    else:
        d[el] += 1
count = 0
for el in d.values():
    count += el*(el-1)//2
print(count)

Submission status: Accepted

Solution code:
li = []
l_dic={}
counter = 0
n = int(input())
for i in range(n):
    l = "".join(sorted(list(input())))
    if l in l_dic.keys():
        l_dic[l] += 1
        
    else:
        l_dic[l] =1
    
for d in l_dic.values():
    counter+=(d-1)*d*0.5
print(int(counter))

Submission status: Accepted

Solution code:
n=int(input())
dic={}
ans=0
for i in range(n):
    s=list(input())
    s.sort()
    s="".join(s)
    dic[s]=dic.get(s,0)
    if dic[s]==0:
        pass
    else:
        ans+=dic[s]
    dic[s]=dic[s]+1

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = []
ans = 0
for i in range(n):
    a.append("".join(sorted(input())))

c = collections.Counter(a)
an = c.values()

for i in an:
    ans += i*(i-1)/2
print(int(ans))

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())
ans = 0
sum = 0
ansdic = {}
for i in range(n):
  s = input()
  slist = [i for i in s]
  slist.sort()
  smoji = ','.join(slist)
  if smoji in ansdic:
    ansdic[smoji] += 1
  else:
    ansdic[smoji] = 1
for i in ansdic:
  if ansdic[i] > 1:
    sum += cmb(ansdic[i],2)
print(sum)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    l = []
    for _ in range(n):
        s = input().rstrip()
        s = sorted(s)
        s = ''.join(s)
        l.append(s)
    c = Counter(l)
    res = 0
    for k, v in c.items():
        res += (v-1)*v//2
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(N):
	S[i] = "".join(S[i])

S.sort()
S.append(None)

pt,count,C = 1,0,[]
while pt <= N:
	if S[pt-1] == S[pt]:
		count += 1
	else:
		if count > 0:
			C.append(count+1)
			count = 0
	pt += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [str(input()) for _ in range(N)]
check = []
ans = 0

for s in S:
  s = sorted(s)
  check.append(''.join(s))

L = Counter(check)
for l,n in L.items():
  ans += n*(n-1)//2
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())
s = [""]*N
for i in range(N):
    s[i] = ','.join(sorted(input()))

c = collections.Counter(s)
ans = 0
for value in c.values():
    if(value == 1):
        continue
    ans += combinations_count(value, 2)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = [""]*N
for i in range(N):
    tmp = list(input())
    tmp.sort()
    s[i] = "".join(tmp)
s.sort()
cnt = 0
ans = 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 _ in range(n):
    s.append(''.join(sorted(input())))
res=0
d={}
for i in s:
    if i in d:
        res+=d[i]
    d[i]=d.get(i,0)+1
print(res)

Submission status: Accepted

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

ret = 0

for item in my_dict:
    ret += my_dict[item] * (my_dict[item] - 1) // 2

print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
sys.setrecursionlimit(10 ** 9)

N = int(input())
d = defaultdict(int)
for _ in range(N):
    s = list(input().rstrip("\r\n"))
    s.sort()
    d["".join(s)] += 1
ans = 0
def count(v):
    if v == 0:
        return 0
    return v + count(v-1)
for k in d:
    ans += count(d[k]-1)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np 
import math

def comb2(n):
    return math.factorial(n) // (math.factorial(n-2) * math.factorial(2))

N = int(input())
S = {}
for i in range(N):
    s = ''.join(sorted(input()))
    if s in S.keys():
        S[s] += 1
    else:
        S[s] = 1
      
res = [comb2(e) if e>1 else 0 for e in S.values()]
print(sum(res))

Submission status: Accepted

Solution code:
from collections import defaultdict
dd=defaultdict(int)
n=int(input())
for _ in range(n):
    s=input()
    x=[]
    for i in range(10):
        x.append(s[i])
    x.sort()
    s="".join(x)
    dd[s]+=1
ans=0
for s in dd:
    p=dd[s]
    ans+=p*(p-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = []
for n in range(N):
  s = ''.join(sorted(input()))
  S.append(s)
  
ans = 0
d = {}
for s in S:
  if s in d:
    ans += d[s]
  d[s] = d.get(s,0)+1
  
print(ans)

Submission status: Accepted

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

n=int(input())
T=[]
for i in range(n):
    S=sorted(list(input()))
    t=""
    for s in S:
        t+=s
    T.append(t)
T=sorted(T)
A=[]
cnt=0
c=T[0]
for t in T:
    if t==c:
        cnt+=1
    else:
        if cnt>1:
            A.append(cnt)
        c=t
        cnt=1
if cnt>1:
    A.append(cnt)
    
ans=0
for a in A:
    ans+=a*(a-1)//2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0

import collections
dict=collections.Counter(lst)

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
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())
s=[]
for _ in range(n):
    s.append(sorted(input()))
s=sorted(s)

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

if now!=1:
    sum+=cmb(now, 2)
print(sum)

Submission status: Accepted

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

s.sort()
ans = 0
for i in range(N):
    ans += bisect.bisect_right(s, s[i])-i-1
    # for j in range(i+1, N):
    #     if s[i] == s[j]:
    #         ans += 1
    #     elif s[i] < s[j]:
    #         break
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
ans=0
cc=Counter()
for _ in range(n):
    s=input()
    s1=''.join(sorted(s))
    ans+=cc[s1]
    cc[s1]+=1
print(ans)

Submission status: Accepted

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