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 sys
from collections import Counter

def main():
    N = int(sys.stdin.readline().strip())
    ans = 0
    present = {}
    for i in range(N):
        temp = "".join(sorted(sys.stdin.readline().strip()))
        if temp not in present:
            present[temp] = 1
            
        else:
            ans += present[temp]
            present[temp] +=1
        
        
    print("%d" % ans)

def solve():
    pass


if __name__ == "__main__":
    main()

Submission status: Accepted

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

text_ls.sort()

num = 0
tmp_add = 0
for i in range(N-1):
    if text_ls[i] == text_ls[i+1]:
        num+=(1+tmp_add)
        tmp_add +=1
    else:
        tmp_add =0

print(num)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
c = Counter()

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

l = c.most_common()
cnt = 0

for e in l:
  cnt += int((e[1] * (e[1] - 1))/2)

print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
b = defaultdict(int)
n = int(raw_input())
a = []
for i in xrange(n):
    x = list(raw_input())
    x.sort()
    a.append(''.join(x))
i = 0
ans = 0
for i in a:
    b[i] += 1
for i in b.values():
    ans += i*(i-1)/2
print ans

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    count = [[0]*26 for _ in range(n)]
    for i in range(n):
        s = input()
        for c in s:
            count[i][ord(c)-97] += 1
    d = dict()
    for lv in count:
        t = tuple(lv)
        if t not in d:
            d[t] = 1
        else:
             d[t] += 1
    # print(d)
    output = 0
    for t in d:
        output += d[t] * (d[t]-1) // 2
    return output



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

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
s = []
for i in range(N):
  a = input()
  b = []
  for i in range(len(a)):
    b.append(ord(a[i]))
  b.sort()
  s.append(b)
s.sort()
ans = 0
temp = 1
for i in range(1,N):
  if s[i]==s[i-1]:
    temp += 1
  else:
    ans += temp*(temp-1)//2
    temp = 1
ans += temp*(temp-1)//2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

dc = {}
ans = 0
for i in range(N):
    s = input()
    s = "".join(sorted(s))
    if dc.get(s):
        ans += dc.get(s)
        dc[s] += 1
    else:
        dc[s] = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce


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


def cmb(n, r):
    # combination
    if n < r:
        return 0
    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


ans = 0
N = int(input())
S = [list(input()) for i in range(N)]

d = {}
for s in S:
    tmp = list(s)
    tmp.sort()
    d[tuple(tmp)] = d.get(tuple(tmp), 0) + 1

# print(d)
for k, v in d.items():
    ans += cmb(v, 2)

print(ans)

Submission status: Accepted

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

res=0
for key in ans.keys():
    res += int((ans[key] * (ans[key]-1)) / 2)

print(res)

Submission status: Accepted

Solution code:
import collections
import math
n = int(input())
s_list = []
for _ in range(n):
    s = input()
    s = "".join(sorted(s))
    s_list.append(s)
ans = 0
s_dict = collections.Counter(s_list)
for i in s_dict.values():
    ans += i * (i - 1)
print(ans // 2)

Submission status: Accepted

Solution code:
N = int(input())
G = {}
for _ in range(N):
    s = "".join(sorted(list(input())))
    if s not in G:
        G[s] = 0
    G[s] += 1
cnt = 0
for s in G:
    if G[s]>1:
        cnt += (G[s]*(G[s]-1))//2
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

S = ["".join(sorted(s)) for s in S]

print(sum([m*(m-1) for m in Counter(S).values()])//2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
acc = [0] * (10**5 + 1)
for i in range(10**5):
    acc[i + 1] = acc[i] + (i + 1)
n = int(input())
s = [input() for _ in range(n)]
dic = {}
for i in range(n):
    srt = ''.join(sorted(s[i]))
    dic[srt] = dic[srt] + 1 if srt in dic else 1
ans = 0
for v in dic.values():
    ans += acc[v - 1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    slist = [list(input()) for _ in range(n)]

    slist2 = [''.join(sorted(s)) for s in slist]
    ans = 0
    counter = Counter(slist2)
    for _, v in counter.items():
        if v > 1:
            ans += v * (v - 1) // 2
    print(ans)


main()

Submission status: Accepted

Solution code:
n  = int(input())
cs = {}

for i in range(0, n):
	s = "".join(sorted(input()))
	
	if s in cs:
		cs[s] += 1
	else:
		cs[s] = 1
		
count = 0

for c in cs.values():
	count += c * (c - 1) // 2

print(count)

Submission status: Accepted

Solution code:
#! python

from collections import defaultdict

#K, X = [int(x) for x in input().split()]

N = int(input())
S = []
for _i in range(N):
    S.append(input())

sortedS = ["".join(sorted(s)) for s in S]

cnt = defaultdict(int)
for s in sortedS:
    cnt[s] += 1

ans = 0
for s,c in cnt.items():
    if c >= 2:
        ans += c * (c-1) // 2
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N = int_raw()
    Ss = [input().strip("\n") for i in range(N)]
    hist_ana = {}
    for s in Ss:
        hist = [0]*27
        for c in s:
            hist[ord(c)-ord("a")]+=1
        hist = tuple(hist)
        if hist in hist_ana:
            hist_ana[hist]+=1
        else:
            hist_ana[hist]=1
    return sum([l*(l-1)//2 for k,l in hist_ana.items()])
    
print(main())

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
N = int(input())
S = [input() for i in range(N)]
d = defaultdict(lambda : 0)
ans = 0
for s in S:
    c = sorted(Counter(s).items())
    ans += d[str(c)]
    d[str(c)]+=1
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()

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

print(answer)

Submission status: Accepted

Solution code:
def val(s):
    a = [0 for i in range(26)]
    for i in s:
        a[ord(i) - ord('a')] += 1
    sum = 0;
    for i in a:
        sum = sum * 10 + i
    return sum


n = int(input())
a = []
for i in range(n):
    a.append(input())
d = {}
for i in a:
    k = val(i)
    if k in d.keys():
        d[k].append(i)
    else:
        d[k] = list()
        d[k].append(i)
sum = 0
for i in d.keys():
    sum += len(d[i]) * (len(d[i]) - 1) // 2
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
s = Counter("".join(sorted(list(input()))) for _ in range(N))

def func(k):
    w = 1
    for x in range(2, k+1):
        w *= x
    return w
ans = 0
for k in s:
    ans += s[k] * (s[k] - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
word_list  = sorted([ sorted(input()) for v in range(n) ])
k = 1
mul_list = []


for i in range(n-1):
    if word_list[i] == word_list[i+1]:
        k += 1
    else:
        mul_list.append(k)
        k = 1
else:
    mul_list.append(k)


def com(a):
    return a*(a-1)//2

print(sum(map(com,mul_list)))

Submission status: Accepted

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

def main():
    N = int(input())
    Dic = {}
    for _ in range(N):
        S = input()    
        S_list = [S[i] for i in range(len(S))]
        S_list.sort()
        S_sorted = "".join(S_list)
        Dic[S_sorted] = Dic.get(S_sorted,0) + 1
        
    ans = 0
    for x in Dic:
        num = Dic[x]
        ans += num * (num-1) // 2
        
    print(ans)
        
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
tmp = []
for i in range(n):
    s = list(str(input()))
    s.sort()
    tmpstr = "".join(s)
    tmp.append(tmpstr)
ans = 0
c = collections.Counter(tmp)
for i in c.keys():
    if c[i] >= 2:
        ans += (c[i]  - 1 )*(c[i] )//2
print(ans)

Submission status: Accepted

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

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

print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())

ans = 0
ana = {}
for i in range(N):
    s = input().rstrip()
    s = ''.join(sorted(s))

    if ana.get(s):
        ans += ana[s]
        ana[s] += 1
    else:
        ana[s] = 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter
from functools import reduce

# s = input()
N = int(input())

res = Counter()

res2 = 0
for _ in range(N):
    s = input()
    counter = Counter(s)
    code = ""
    for k in sorted(counter.keys()):
        code += str(k) + str(counter[k])
    res[code] += 1

for v in res.values():
    res2 += (v*(v-1)//2)

# print(res)

print(res2)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
H = defaultdict(int)
for _ in range(N):
  S = input()
  C = [0] * 26
  for s in S:
    C[ord(s) - 97] += 1
  H[str(C)] += 1
cnt = 0
for v in H.values():
  cnt += v * (v - 1) // 2
print(cnt)

Submission status: Accepted

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

s = sorted(s)

s.append("=end=")
t = ""
c = 0
total = 0
for a in s:
    if a != t:
        t = a
        total += c * (c-1) // 2
        c = 0
    c += 1

print(total)

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
seen = {}
cnt = 0
for ind,s in enumerate(S):
    x = list(s)
    x.sort()
    y = ''.join(x)
    if(y in seen):
        seen[y] += 1
    else:
        seen[y] = 1

for i in seen:
    if(seen[i]==1):continue
    x = seen[i]
    cnt += x*(x-1)//2
print(cnt)

Submission status: Accepted

Solution code:
# A, B = [int(i) for i in input().split(" ")]
# A = int(input())
# As = [int(i) for i in input().split(" ")]

from math import factorial
def nCr(n, r):
  return factorial(n) // factorial(r) // factorial(n - r)

N = int(input())
Ss = ["".join(sorted(input())) for _ in range(N)]
m = {}
for s in Ss:
  m[s] = m.get(s, 0) + 1
ans = sum(nCr(n, 2) for n in m.values() if n >= 2)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


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


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):
    c = []
    d = Counter()
    for s in S:
        c = Counter(s)
        h = []
        for k in sorted(c.keys()):
            h.append(k + str(c[k]))
        d[''.join(h)] += 1

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


def main():
    N = read_int()
    S = [read_str() for _ in range(N)]
    print(slv(N, S))


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 = ''.join(sorted(input()))
    d[s] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect


from math import factorial
# 組み合わせ
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

def main():
    num = int(input())
    data = [input() for i in range(num)]
    for i in range(num):
        data[i] = list(data[i])
        data[i].sort()
        data[i] = ''.join(data[i])
    data.sort()

    kari = 1
    pre = ''
    ans = 0
    # print(data)
    for i in range(num):
        if pre == data[i]:
            kari += 1
        else:
            if kari > 1:
                ans += combinations_count(kari, 2)
            kari = 1
            pre = data[i]

    if kari > 1:
        ans += combinations_count(kari, 2)
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
strs = sorted([''.join(sorted(input().strip())) for _ in range(n)])

i, j = 0, 1
lcnt = 1
res = 0
while j < n:
    if strs[i] == strs[j]:
        lcnt += 1
        j += 1
    else:
        res += lcnt * (lcnt - 1) // 2
        i = j
        j = i + 1
        lcnt = 1
res += lcnt * (lcnt - 1) // 2

print(res)

Submission status: Accepted

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
A = Counter()
for i in range(N):
    A[''.join(sorted(input()))] += 1

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
from collections import defaultdict

n = getN()
dc = defaultdict(int)
for i in range(n):
    s = input().strip()
    dc["".join(sorted(list(s)))] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
S = [input() for _ in range(N)]
L = defaultdict(list)
for i, s in enumerate(S):
    s = "".join(sorted(s))
    L[s].append(i)
num = 0
for x in L.values():
    num += sum(range(len(x)))
print(num)

Submission status: Accepted

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

def main():
    N = int( input())
    S = [ list(input()) for _ in range(N)]
    for i in range(N):
        S[i] = "".join(sorted(S[i][:10]))
    S.sort()
    cnt = 1
    ans = 0
    now = S[0]
    for i in range(1, N):
        if now == S[i]:
            cnt += 1
            continue
        ans += cnt*(cnt-1)//2
        cnt = 1
        now = S[i]
    ans += cnt*(cnt-1)//2
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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


def solve(N: int, S: "List[str]"):

    dic = collections.defaultdict(int)
    for s in S:
        s = "".join(sorted(s))
        dic[s] += 1

    res = 0
    for k,v in dic.items():
        if v > 1:
            res += v*(v-1)/2

    print(int(res))



# 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:
N = int(input())

S = []
for i in range(N):
    string = input()
    string = list(string)
    string.sort()
    string = ''.join(string)
    S.append(string)

S.sort()

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

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

import numpy as np

n = int(input())
appeared = defaultdict(lambda: 0)
ans = 0
for s in sys.stdin:
    p = tuple(np.bincount([ord(c) - 97 for c in s.strip()], minlength=26))
    ans += appeared[p]
    appeared[p] += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

from itertools import accumulate
from bisect import bisect, bisect_left
from collections import Counter, deque
from functools import reduce
from itertools import accumulate
from operator import itemgetter, xor
from string import ascii_lowercase as abc

from sys import setrecursionlimit
setrecursionlimit(10**9)
 
import sys
#input = sys.stdin.readline
from collections import Counter
def inpl(): return list(map(int, input().split()))

N = int(input())
C = Counter()

for i in range(N):
    S = Counter(input())
    K = [0]*26
    for i in range(26):
        K[i] = S[abc[i]]
    C[tuple(K)] += 1

def cal(x):
    return x*(x-1)//2

ans = 0
for v in C.values():
    ans += cal(v)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
dic={}
for i in range(n):
    s=input()
    l=[]
    for a in s:
        l.append(a)
    l.sort()
    s="".join(l)
    if s in dic:
        dic[s]+=1
    else:
        dic[s]=1
total=0
for f in dic:
    total+=int((dic[f]*(dic[f]-1))/2)
print(total)

Submission status: Accepted

Solution code:
n = int(input())
can = []
for i in range(n):
  raw = input()
  now = []
  for j in range(len(raw)):
    now.append(raw[j])
  now.sort()
  can.append(now)
can.sort()
cnt = 0
ans = 0
for i in range(1,n):
  if can[i-1] == can[i]:
    cnt += 1
    ans += cnt
  else:
    cnt = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ans = 0
for n in words.values():
    if n == 1:
        continue
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections
from functools import reduce
from operator import mul

N = int(input())
Hs = []


def cmb(n, r):
    try:
        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
    except:
        return 0


for line in range(N):
    l = list(input())
    l.sort()
    Hs.append(','.join(l).replace(',', ''))

c = collections.Counter(Hs)

su = 0
for v in c.values():
    su += cmb(v, 2)

print(su)

Submission status: Accepted

Solution code:
def decoder(s):
    ret = [0] * 26
    for c in s:
        ret[ord(c) - ord('a')] += 1
    return str(ret)
n = int(input())
dic = {}
for _ in range(n):
    s = input()
    count = decoder(s)
    if count in dic:
        dic[count] += 1
    else:
        dic[count] = 1
ans = 0
for i, c in dic.items():
    ans += c * (c-1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import math

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


def main():
    N = int(input())
    # K, X = [int(a) for a in input().split()]
    s = sorted([
        "".join(sorted(input()))
        for _ in range(N)
    ])

    c = 0
    last = ""
    res = 0
    for si in s:
        if last == si:
            c += 1
            continue
        else:
            last = si
            res += (c + 1) * c // 2
            c = 0
            continue

    res += c * (c + 1) // 2
    print(res)

    # A = [int(a) for a in input().split()]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
s = []
def str_sort(x, s = False):
    y = sorted(x, reverse = s)
    return ''.join(y)

for i in range(N):
    si = str_sort(input())
    s.append(si)
s.sort()

ans = 0
gr = itertools.groupby(s)
for key, group in gr:
    n = len(list(group))

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

print(ans)

Submission status: Accepted

Solution code:
import math

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



N = int(input())

dic = {}

for i in range(N):

    s = input()

    slis = list(s)
    slis.sort()

    strs = "".join(slis)

    if strs not in dic:
        dic[strs] = 1
    else:
        dic[strs] += 1


ans = 0

for i in dic:

    if dic[i] >= 2:
        ans += cc(dic[i],2)

print (ans)

Submission status: Accepted

Solution code:
import sys, math
from collections import deque
from collections import Counter

readline = sys.stdin.readline

N = int(readline())
S = [readline().rstrip() for i in range(N)]

S = [''.join(sorted(s)) for s in S]

c = Counter(S)

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

print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re
import bisect

BIGNUM = 10 ** 9 + 7

def main():
    #K, X = map(int, input().split())
    N = int(input())
    #Hs = list(map(int, input().split()))

    Ss = []
    for i in range(N):
        Ss.append(input().strip())

    counter = defaultdict(int)
    for s in Ss:
        key = "".join(sorted(s))
        counter[key] += 1

    count = 0
    for key in counter:
        num = counter[key]
        if num > 1:
            count += int(num * (num - 1) / 2)

    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！

from math import factorial
def cmb(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)


def main():
    n=int(input())
    l=[cmb(i,2) for i in Counter(["".join(sorted(input()))for i in range(n)]).values() if i>1]
    print(sum(l))
                    
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#
# Yet I'm feeling like
# 	There is no better place than right by your side
# 		I had a little taste
# 			And I'll only spoil the party anyway
# 				'Cause all the girls are looking fine
# 					But you're the only one on my mind


import sys
inf = float("inf")
#sys.setrecursionlimit(1000000)


# abc='abcdefghijklmnopqrstuvwxyz'
# abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod,MOD=1000000007,998244353
# vow=['a','e','i','o','u']
# dx,dy=[-1,1,0,0],[0,0,1,-1]

#from cmath import sqrt
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
# from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10
#from bisect import bisect_left,bisect_right
#import numpy as np

def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

n=int(input())
mydict=dict()
for i in range(n):
    string=list(input())
    string.sort()
    string=''.join(string)
    if string not in mydict:
        mydict[string]=1
    else:
        mydict[string]+=1
total=0
for i in mydict:
    total+=((mydict[i])*(mydict[i]-1))//2
print(total)

Submission status: Accepted

Solution code:
from functools import reduce
N = int(input())
s_list = [input() for i in range(N)]

s_list = [ sorted(s) for s in s_list]
s_list = [ reduce(lambda x, y: x+y, s) for s in s_list]

s_map = {}
for s in s_list:
    if s in s_map:
        s_map[s] += 1
    else:
        s_map[s] = 1

ans = 0
for k, v in s_map.items():
    if v > 1:
        ans += v*(v-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
D = {}
N = int(input())
for n in range(N):
    S = list(input())
    S.sort()
    T = tuple(S)
    D[T] = D.setdefault(T, 0) + 1

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

print(res)

Submission status: Accepted

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

al_nu = lambda c: ord(c) - ord('A')
S2 = [[] for i in range(n)]
for i in range(n):
    for j in range(10):
        S2[i].append(al_nu(S[i][j]))
for i in range(n):
    S2[i] = sorted(S2[i])

S2.sort()
#print(S2)

ans = 0
co = 0
for i in range(n-1):
    if S2[i] == S2[i+1]:
        co += 1
        #print("a")
    else:
        ans += co*(co+1)/2
        co = 0
ans += co*(co+1)/2
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
D = dict([])
ans = 0
for k in range(N):
    s = sorted([str(x) for x in input()])
    a = ""
    for e in s:
        a += e
    if a in D:
        ans += D[a]
        D[a] += 1
    else:
        D[a] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

d = {}


class mod_comb_perm:
    def __init__(self,n,mod):
        self.mod = mod
        self.fact = [1 for i in range(n)]
        for i in range(2,n+1):
            self.fact[i-1] = (self.fact[i-2]*i)%mod

    def expo(self,x,n):
        if n == 2:
            return (x * x) % self.mod
        elif n % 2 == 1:
            return (self.expo(x, n - 1) * x) % self.mod
        else:
            temp = self.expo(x, n // 2) % self.mod
            return (temp * temp) % self.mod
    def comb(self,n,k):
        a = (self.fact[n]*self.expo(self.fact[k],self.mod-2))%self.mod
        return (a*self.expo(self.fact[n-k],self.mod-2))%self.mod
    def invm(self,x):
        return (self.expo(self.fact[x],self.mod-2))%self.mod
    def perm(self,n,k):
        return (self.fact[n]*self.expo(self.fact[n-k],self.mod-2))%self.mod


# com = mod_comb_perm(10**5,10**18+3)
d = {}
for i in range(n):
    temp = [0 for i in range(26)]
    s = input()
    for c in s:
        t = ord(c)-97
        temp[t]+=1
    for i in range(26):
        temp[i] = str(temp[i])
    txt = ",".join(temp)
    if txt not in d:
        d[txt] = 1
    else:
        d[txt] += 1
ans = 0
# print(d)
for k in d:
    if d[k] != 1:
        ans += d[k]*(d[k]-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(n):
    p=input()
    l=list(p)
    l.sort()
    p="".join(l)
    s.append(p)
s.sort()
q=""
ans=0
qq=1
for i in range(n):
    if i!=n-1:
        if q==s[i]:
            qq+=1
        else:
            q=s[i]
            ans+=qq*(qq-1)//2
            qq=1
    else:
        if q==s[i]:
            qq+=1
        ans+=qq*(qq-1)//2
print(ans)