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
import math

N = int(input())
s =[]
ans = 0
for i in range(N):
    s.append(input())
    letters = list(s[i])
    letters.sort()
    s[i] = "".join(map(str,letters))

H = collections.Counter(s)
I  = H.most_common()
for i in range(len(I)):
    if I[i][1] > 1:
        ans += int(math.factorial(I[i][1])/math.factorial(I[i][1]-2)/2)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
s = [input() for _ in range(N)]
dic = defaultdict(int)

for i in range(N):
    os = "".join(sorted(s[i]))
    dic[os] += 1
#print(os)
cnt = 0
for s, c in dic.items():
    #print(s, c)
    cnt += c * (c-1) // 2
    #print(c* (c - 1) // 2)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

result = 0
word_list = {}

for i in range(n):
    input_word = "".join(sorted(input()))
    flag = 1
    if input_word in word_list:
        word_list[input_word] += 1
        result += word_list[input_word]
        flag = 0
    else:
        word_list[input_word] = 0

print(result)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
l = ["".join(sorted([c for c in input()])) for i in range(N)]
l.sort()


def fac(n):
    """階乗"""
    res = 0
    for i in range(1, n+1):
        res += i
    return res


hash_l = []
for i in range(N-1):
    if l[i] == l[i+1]:
        if i == 0:
            hash_l.append(0)
        if l[i-1] == l[i]:
            hash_l[-1] += 1
        else:
            hash_l.append(1)

cnt = 0
for x in hash_l:
    cnt += fac(x)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
obj = {}
for i in range(N):
  s = input()

  w = ''.join(sorted(list(s)))
  if not obj.get(w):
    obj[w] = 1
  else:
    obj[w] += 1

cnt = 0
for i in obj:
  v = obj[i]
  cnt += (v*(v-1))/2

print(int(cnt))

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

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

[s[i].sort() for i in range(len(s))]

s = [' '.join(s[i]) for i in range(len(s))]
mycounter = Counter(s)
mycounter = list(mycounter.values())

mycounter = [mycounter[i] for i in range(len(mycounter)) if mycounter[i] > 1]
mycounter = [combinations_count(mycounter[i], 2) for i in range(len(mycounter))]

print(sum(mycounter))

Submission status: Accepted

Solution code:
import math
import sys
import itertools
import queue
from fractions import gcd

import sys
sys.setrecursionlimit(10000)

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

from math import factorial
import collections

if __name__ == "__main__":
    N = int(sys.stdin.readline())
    #mng = [][]
    sl = []
    for _ in range(N):
        tmp = sorted(sys.stdin.readline().rstrip())
        #print(''.join(tmp))
        sl.append(''.join(tmp))
    #print(sl)
    c = collections.Counter(sl)
    ans = 0
    for n in c.values():
        r = 2
        if n > 1:
            ans += factorial(n) // (factorial(n - r) * factorial(r))

    print(int(ans))

Submission status: Accepted

Solution code:
def encode(s):
#  r = [0]*26
#  for n in range(len(s)):
#    r[ord(s[n]) - ord('a')] += 1
#  return r
#  r = list(s)
#  r.sort()
#  return r
  r = 0
  for n in range(len(s)):
    so = ord(s[n]) - ord('a')
    r += 11**so
  return r

N = int(input())

S = [0]*N

for n in range(N):
  s = str(input())
  S[n] = encode(s)

S.sort()

ct = 0
M=1
ps=""
for cs in S:
  if cs==ps:
    M+=1
  else:
    ct += int((M**2-M)/2)
    M=1
    ps=cs
ct += int((M**2-M)/2)

print(ct)

Submission status: Accepted

Solution code:
N = int(input())
s = {}
ans = 0
for n in range(N):
  chars = str(sorted(list(input())))

  if chars in s:
    s[chars] += 1
  else:
    s[chars] = 0
    
for value in s.values():
  ans += value * (value+1) // 2
  
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
d = collections.defaultdict(int)
for _ in range(n):
    a = sorted(input())
    s = ''.join(a)
    d[s] += 1
count = 0
for v in d.values():
    count += v * (v-1) // 2
print(count)

Submission status: Accepted

Solution code:
import math

N = int(input())
s = [[""]*10 for i in range(N)]
a =[[0]*26 for i in range(N)]
b =[""]*N

cn =ord("a")

for i in range(N):
    s[i] = list(input())
    s[i].sort()
    b[i] ="".join(s[i])

b.sort()
c = 0
ans =0

for i in range(N-1):
    if b[i]==b[i+1]:
        c +=1
    else:
        if c !=0:
            ans=ans + math.factorial(c+1)/(math.factorial(2)*math.factorial(c+1-2))
            c = 0


if c !=0:
    ans = ans + math.factorial(c+1)/(math.factorial(2)*math.factorial(c+1-2))
    

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
string_list=["".join(sorted(input())) for i in range(N)]

ans = 0
c = Counter(string_list)

for v in c.values():
    ans += comb(v, 2, exact=True)

print(ans)

Submission status: Accepted

Solution code:
def nCr(n, r):
    r = min(r, n-r)

    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i

    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i

    return numerator // denominator


n = int(input())
s = sorted([sorted(input()) for _ in range(n)])


ans = 0
cnt = 0
for i in range(n-1):
    if i == n-2 and s[i] == s[i+1]:
        cnt += 1
        ans += nCr(cnt+1, 2)
        print(ans)
        exit()
    if s[i] == s[i+1]:
        cnt += 1
    else:
        if cnt > 0:
            ans += nCr(cnt+1, 2)
            cnt = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
string_list=["".join(sorted(input())) for i in range(n)]

ans = 0
cnt = Counter(string_list)
for i in cnt.most_common():
    if i[1] != 1:
        ans += (i[1] * (i[1]-1) ) // 2
print(ans)

Submission status: Accepted

Solution code:
import collections
import math

N = int(input())

al = "abcdefghijklmnopqrstuvwxyz"
al_dict = {}
for i, c in enumerate(al):
    al_dict[c] = i

ss = []
for i in range(N):
    counts = [0 for i in range(26)]
    for char in input():
        ind = al_dict[char]
        counts[ind] += 1
    counts = [str(count) for count in counts]
    key = ",".join(counts)
    #c = collections.Counter(s0)
    #print(tuple(c))
    ss.append(key)

#print(ss)
cc = collections.Counter(ss)

#print(cc)
out = 0
for count in cc.values():
    if count == 1:
        continue
    out += math.factorial(count) // (math.factorial(count-2) * 2)
print(out)

#for s in ss:
#    for  in s[i+1]:
#        if s[i] == s[j]:
#            count += 1

#print(count)

Submission status: Accepted

Solution code:
import math

n = int(input())

setmap = dict()
for index in range(n):
  s = input()
  sorted_s = ''.join(sorted(s))
  if setmap.get(sorted_s) is not None:
    setmap[sorted_s] = setmap[sorted_s] + 1
  else:
    setmap[sorted_s] = 1

same_count = 0
for val in setmap.values():
  if val != 1:
    same_count += int(math.factorial(val) / (2 *  math.factorial(val - 2)))
print(same_count)

Submission status: Accepted

Solution code:
N = int(input())

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

save = []

for i in range(N):
    s = ''.join(sorted(input()))
    save.append(s)

save.sort()

cnt = 0
serial = 1
for i in range(N-1):
    if save[i] == save[i+1]:
        cnt += 1
        serial += 1
    else:
        cnt += int(nume(serial))
        serial = 1

cnt += int(nume(serial))
print(cnt)

Submission status: Accepted

Solution code:
def main(text=""):
    n = int(input())
    a = [input() for i in range(n)]
    #a = text.split("\n")[1:]
    num = 0
    for i in range(len(a)):
        a[i] = "".join(sorted(a[i]))
    a.sort()
    i = 0
    while i < len(a):
        e = a[i]
        j = 1
        while i+j < len(a) and e == a[i+j]:
            j = j + 1
        if j != 1:
            num = num + combination(j)
            i = i+j
        else:
            i = i+1
    print(num)


def combination(x):
    return int(x*(x-1)/2)


main()

Submission status: Accepted

Solution code:
n = int(input())
sl = sorted([''.join(sorted(input())) for _ in [0]*n])
count = 0
while len(sl)>1:
  i = 1
  flag = True
  while flag:
    if sl[-1] == sl[-(i+1)]:
      i += 1
      if i == len(sl):
        flag=False
    else:
      flag = False
  if i>1:
    count += i*(i-1)//2
  del sl[-i:]
  flag=True
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import math

l_s = set()
c = 0
n = int(input())
dup = {}

for i in range(n):
    st = sorted(input())
    s  = "".join(st)
    if s in l_s:
        if s in dup.keys():
            dup[s] += 1
        else:
            dup[s] = 1
        c += dup[s]
    l_s.add(s)

print(c)

Submission status: Accepted

Solution code:
N = int(input())
X =[]
for i in range(N):
    Y =list(input())
    Y.sort()
    X.append(Y)
X.sort()
    
ans = 0
count = 1 
for i in range(N-1):
    if X[i] == X[i+1]:
        count += 1
    else:
        if count != 1:
            Z = 1
            Z = Z * count * (count-1)
            Z = Z // 2
            ans = ans + Z
            count = 1

if count != 1:
    Z = 1
    Z = Z * count * (count-1)
    Z = Z // 2
    ans = ans + Z
        
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())

word_list = []
for i in range(N):
    S = list(input())
    c = collections.Counter(sorted(S))
    count = c.values()
    words = c.keys()
    word_list.append(''.join(words)+str(count))

ans_dict = collections.Counter(word_list)
ans_count = list(ans_dict.values())
temp = [i for i in ans_count if i>1]
ans = int(sum([t*(t-1)/2 for t in temp]))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
lis = ["".join(sorted(list(input()))) for _ in range(N)]
keys, values = zip(*Counter(lis).most_common())
values = list(filter(lambda x: x >= 2, values))

sums = 0
for k in values:
  sums += ((k - 1) * (k)) / 2
  
print(int(sums))

Submission status: Accepted

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

ss.sort()
ans = 0
tmp = 1
for i in range(N-1):
    if ss[i] == ss[i+1]:
        tmp += 1
        if i == N-2:
            ans += (tmp * (tmp-1)) // 2
    else:
        ans += (tmp * (tmp-1)) // 2
        tmp = 1

print(ans)

Submission status: Accepted

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

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result



n = int(input())
s = []
for i in range(n):
  s.append(input())
h = []
b = 'abcdefghijklmnopqrstuvwyz'
for i in s:
  a = [0] * 27
  for c,d in enumerate(b):
    a[c] = i.count(d)
  ss = ''
  for j in a:
    ss += str(j)
  h.append(ss)
 
ans = 0
c = {}
for i in h:
  if c.get(i, -1) == -1:
    c[i] = 1
  else:
    c[i] += 1
sum = 0
for i in c:
  if c[i] < 2:
    continue
  ans += cmb(c[i],2)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[]
T=[]
for i in range(N):
	s=str(input())
	S.append(s)
for j in range(N):
	X=sorted(str(S[j])[k] for k in range(10))
	Y=X[0]+X[1]+X[2]+X[3]+X[4]+X[5]+X[6]+X[7]+X[8]+X[9]
	T.append(Y)
R=sorted(T)

l,r=0,0
ans=0
while r<N:
	if R[r]==R[l] and r==N-1:
		ans=ans+int((r-l)*(r-l+1)/2)
		r=r+1
	elif R[r]==R[l]:
		r=r+1
	else:
		ans=ans+int((r-l-1)*(r-l)/2)
		l=r
print(int(ans))

Submission status: Accepted

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

ss = sorted(ss)

total = 0
count = 0
for n in range(N):
    if n==0:
        continue
    # print("ss["+str(n-1)+"]="+str(ss[n-1]))
    # print("ss["+str(n)+"]="+str(ss[n]))
    if ss[n-1]==ss[n]:
        count += 1
    else:
        total += int(count*(count+1)/2)
        count = 0
    # print("===")
total += int(count*(count+1)/2)
count = 0

print(int(total))

Submission status: Accepted

Solution code:
import collections
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())

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

c = collections.Counter(a)
r = 2
total = 0
for k in c.keys():
  n = int(c[k])
  if n != 1:
    total += cmb(n, r)

print(total)

Submission status: Accepted

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

s = sorted(s)

j = 0
x = []
for i in range(1, n):
  if s[i] == s[i-1]:
    j += 1
  else:
    if j > 0:
      x.append(j)
      j=0
if j > 0:
  x.append(j)

def func(n):
  return (n+1)*n/2

ret = sum(map(func, x))

print(int(ret))

Submission status: Accepted

Solution code:
def ss(s):
    # 文字ソート
    res = s[0]
    for i in s[1:]:
        flg = False
        for j, e in enumerate(res):
            if e > i:
                res = res[:j] + i + res[j:]
                flg = True
                break

        if not flg:
            res += i
    return res


def poe(n):
    return sum([i - 1 for i in range(2, n + 1)])


def main():
    n = int(input())
    slist = {}
    for i in range(n):
        s = ss(input())
        if s in slist:
            slist[s] += 1
        else:
            slist[s] = 1

    ans = 0
    for k, v in slist.items():
        a = poe(v)
        ans += a
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

num = int(input())
tmp = {}
flg = 0
ans = 0
for i in range(num):
  inp = ''.join(sorted(input()))
  if inp not in tmp: tmp[inp]=1
  else: tmp[inp]+=1
for k, v in tmp.items():
  if v > 1:
    ans += v*(v-1)//2
    flg = 1
print(ans)

Submission status: Accepted

Solution code:
import collections

m = [0] * (10**6)
for i in range(1, len(m)):
    m[i] = m[i-1] + i

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

c = 0
for k, v in collections.Counter(s).items():
    if v <= 1:
        continue
    c += m[v-1]

print(c)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:

n = int(input())
s = []
dict = {}
for i in range(n):
    ss = sorted(input())
    st = "".join(map(str,ss))
    if st in dict:
        dict[st]+=1
    else:
        dict[st] = 1
    # print(st)
# print(s)
count = 0
for i in dict:
    if dict[i] > 1:
        count+=(dict[i] * (dict[i] - 1))//2

print(count)

Submission status: Accepted

Solution code:
N = int(input())
s = [None]*N
s_sort = [None]*N
for _ in range(N):
    s[_] = sorted(list(input()))
    s_sort[_] = ''.join(s[_])
s_sort.sort()
inst = s_sort[0]
s_sort.append("fuckatcorder")
count = 0
lis = []
for i in range(1,N+1):
    if s_sort[i] == inst:
        count += 1
    else:
        if count != 0:
            lis.append(count)
            count = 0
            inst = s_sort[i]
        else:
            inst = s_sort[i]
ans = 0
for k in range(len(lis)):
    ans += lis[k]*(lis[k]+1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

from collections import Counter
counter = Counter(S)

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

Submission status: Accepted

Solution code:
n = int(input())
li = [list(str(input())) for i in range(n)]
#print(li)
for i in range(n):
    li[i].sort()
#print(li)
li.sort()
# print(li)
now = 0
count = 0
res = 0
for i in range(1,n):
    if li[now] == li[i] and i == n-1:
        count += 1
        res += count*(count+1)/2
    elif li[now] == li[i]:
        count += 1
    else:
        now = i
        res += count*(count+1)/2
        count = 0

print(int(res))

Submission status: Accepted

Solution code:
from collections import Counter
c = Counter()
n = int(input())
for i in range(n):
  c[''.join(sorted(input()))] += 1
ans = 0
for v in c.values():
  ans += v * (v - 1) // 2
print(ans)

Submission status: Accepted

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

  d = {}
  for i, s in enumerate(S):
    x = "".join(sorted(s))
    if x in d:
      d[x] += 1
    else:
      d[x] = 1
  
  cnt = 0
  for v in d.values():
    if v == 1:
      continue
    if v == 2:
      cnt += 1
    elif v == 3:
      cnt += v
    else:
      cnt += sum(list(range(1,v)))

  print(cnt)
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import collections

n = int(input())

listAll = []

ans = 0

for i in range(n):
	s = input()

	slist = list(s)

	#print(slist)

	slist.sort()
	#print(slist)
	sString = "".join(slist)
	#print(sString)

	listAll.append(sString)

c = collections.Counter(listAll)
# print(type(c))
for mykey in c.keys():
	# print("きー")
	#print(mykey)
	n = c[mykey]
	ans += n * (n-1) /2


print(int(ans))

Submission status: Accepted

Solution code:
import collections
from math import factorial

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

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

c = collections.Counter(S)
values, counts = zip(*c.most_common())
counts = [i for i in counts if i > 1]

ans = 0
for c in counts:
    ans += cmb(c, 2)
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for _ in range(n)]
s2 = [0 for _ in s]
# l = [0 for _ in range(1<<(26*4))]
s2 = {}

for j, si in enumerate(s):
    counter = [0 for _ in range(26)]
    for c in si:
        i = ord(c) - 0x61
        counter[i] += 1
    rep = 0
    for i in range(26):
        rep += counter[i] * (1<<(i*4))
    # s2[j] = rep
    # l[rep] += 1
    if rep not in s2.keys():
        s2[rep] = 1
    else:
        s2[rep] += 1

pair = 0
"""
for i in range(n):
    for j in range(i+1, n):
        if s2[i] == s2[j]:
            pair += 1
"""
from math import factorial
for i in s2.values():
    if i > 1:
        pair += i * (i-1) // 2 

print(pair)

Submission status: Accepted

Solution code:
from collections import Counter as C
n=int(input());s=[input() for _ in range(n)]
print(int(sum(map(lambda x:x*(x-1)//2,C(tuple(sorted(C(i).most_common(),key=lambda x:x[0]))for i in s).values()))))

Submission status: Accepted

Solution code:
n=int(input())
s=list()
strlist=list()
for i in range(n):
  s.append(input())
  strlist.append(list(s[i]))
  strlist[i].sort()
  s[i]="".join(strlist[i])

s.sort()

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

print(int(ans))

Submission status: Accepted

Solution code:
def p2(num):
    if num == 0:
        return 0
    else:
        return (num * (num - 1)) // 2


a = [sorted(input()) for i in range(int(input()))]
sorted_arr = sorted(a)

tmp = [""]
tmp_num = 0
tmp_arr = []
for i in sorted_arr:
    if i != tmp:
        tmp = i
        tmp_arr.append(tmp_num)
        tmp_num = 1
    else:
        tmp_num += 1
tmp_arr.append(tmp_num)
res = 0
for i in tmp_arr:
    res += p2(i)
print(res)

Submission status: Accepted

Solution code:
from itertools import groupby

N = int(input())
sortedStrs = []
for i in range(N):
    sortedStrs.append("".join(sorted(list(input()))))
resultCount = 0
for (k, g) in groupby(sorted(sortedStrs), key=lambda m: m):
    count = len(list(g))
    if count > 1:
        resultCount += count * (count - 1) / 2
print(int(resultCount))

Submission status: Accepted

Solution code:
import collections
import math

inp = int(input())
A = []
for a in range(inp):
    A.append(input())

def charcnt(s):
    cset = {}
    for c in s:
        if c in cset:
            cset[c] += 1
        else:
            cset[c] = 1
    sortedset = sorted(cset.items(), key=lambda x:x[0])
    s = ""
    for k, v in sortedset:
        s += k + str(v)
    return s

sorted = [charcnt(v) for v in A]    

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


counted = collections.Counter(sorted)
cnt = 0
for v in counted.values():
    if v > 1:
        cnt += cmb(v, 2)
print(int(cnt))

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(sorted(list(input())))
cnt_ans = 0
s.sort()
cnt = 0
left = 0
right = 1
while right <= n-1:
    if s[left] == s[right]:
        right += 1
        cnt += 1
    else:
        left, right = right, right+1
        cnt_ans += ((cnt+1)*cnt//2)
        cnt = 0
    if right == n:
        cnt_ans += ((cnt+1)*cnt//2)
print(cnt_ans)

Submission status: Accepted

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

res = 0
ns = S[0]
c = 0
k = 1
for i in range(1, N):
    if S[i] == ns:
        c += k
        k += 1
    else:
        if c != 0:
            res += c
        c = 0
        k = 1
        ns = S[i]

if c != 0:
    res += c

print(res)

Submission status: Accepted

Solution code:
import collections

n = int(input())
ss = [tuple(sorted(collections.Counter(input()).items())) for i in range(n)]

s_dict = dict()

for s in ss:
  if s not in s_dict:
    s_dict[s] = 1
  else:
    s_dict[s] += 1

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())

cnt = {}
for i in range(n):
    s = input()
    s = sorted(list(s))
    s = str(s)
    if s not in cnt:
        cnt[s] = 0
    else:
        cnt[s] += 1

ans = 0
for c in cnt.values():
    ans += c * (c+1) // 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
z=0
d=0

for i in range(0,n):
    m=input()
    y=list(m)
    y.sort()
    m=''.join(y)
    s.append(m)

s.sort()

for j in range(0,n-1):
    if s[j]==s[j+1]:
        d=d+1
    else:
        if d==0:
            continue
        else:
            f=d*(d+1)//2
            z=z+f
            d=0
if d!=0:
    f=d*(d+1)//2
    z=z+f

print(z)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def main():
    N = int(sys.stdin.readline())
    S = [tuple(sorted(x.rstrip())) for x in sys.stdin.readlines()]

    dic = {}
    for i, s in enumerate(S):
        if s not in dic:
            dic[s] = {i:1}
        else:
            dic[s][i] = len(dic[s])+1

    count = 0
    for i, s in enumerate(S):
        count += len(dic[s]) - dic[s][i]

    print(count)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import collections

N = int(input())
s = [0] * N


for i in range(N):
    s[i] = "".join(sorted(input()))


c = collections.Counter(s)

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

print(int(count))

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N = int(input())

sentences = [None]*N

dp = {}

for i in range(N):
     k = list(map(str,input()))
     k.sort()
     sentences[i] = k

def addTree(tree, sentence,org):
    l = len(sentence)
    if l==0:
        return None
    st = sentence[0]
    if st not in tree:
        tree[st] = {}
    else:
        if l==1:
            org = ''.join(org)
            if org in dp:
                dp[org] += 1
            else:
                dp[org] = 2

    tree[sentence[0]] = addTree(tree[st], sentence[1:],org)

    return tree


def createTree(sentences):
    tree = {}
    for sentence in sentences:
        tree = addTree(tree, sentence, sentence)

    return tree

tree = createTree(sentences)

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

Submission status: Accepted

Solution code:
n = int(input())
s = []

for i in range(n):
    x = [x for x in input()]
    x.sort()
    s.append(''.join(x))

s.sort()
count = 0
i = 0
while True:
    ren = 0
    if i >= n:
        break
    for j in range(i + 1, n):
        if s[i] == s[j]:
            ren += 1
        else:
            break

    if ren != 0:
        for k in range(ren):
            count += k + 1
        i += ren - 1
    i += 1

print(count)

Submission status: Accepted

Solution code:
import collections

N = int(input())
s = [input() for i in range(N)]
count = 0
a = 0
moji = []
for i in s:
    char = list(i)
    char = sorted(char)
    char = ''.join(char)
    moji.append(char)
moji = sorted(moji)
c = collections.Counter(moji)
values, counts = zip(*c.most_common())
for count in counts:
    count -= 1
    while count > 0:
        a += count
        count -= 1
print(a)

Submission status: Accepted

Solution code:
n = int(input())
li = []

for i in range(n):
    s = input()
    t = list(s)
    t.sort()
    s = ''.join(t)
    li.append(s)

li.sort()

a = 1
ans = 0

for i in range(n):
    if i == n-1:
        ans += a*(a-1)/2
    elif li[i] == li[i+1]:
        a += 1
    else:
        ans += a*(a-1)/2
        a = 1

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
s_list = []
for _ in range(N):
    s = input()
    s_sorted = "".join(sorted(s))
    s_list.append(s_sorted)

C = Counter(s_list)
ans = 0
for x in C.values():
    ans += (x * (x - 1)) // 2

print(ans)

Submission status: Accepted

Solution code:
def f(d,s):  #s=[0,1,2,3,4]
    global ans
    #print(d,s)
    if d==26:
        a=len(s)
        ans+=a*(a-1)//2
        return
    s1=[[s[0]]]
    for i in range(1,len(s)):
        b=1
        for j in range(len(s1)):
            a=0
            if m[s1[j][0]][d]==m[s[i]][d]:
                s1[j]+=[s[i]]
                a=1
            if a:
                b=0
                break
        if b:
            s1+=[[s[i]]]
    #print(d,s1)
    for i in range(len(s1)):
        if len(s1[i])>1:
            f(d+1,s1[i])

            






n=int(input())
m=[[0 for i in range(26)]for j in range(n)]
a=ord("a")
for i in range(n):
    s=input()
    for j in range(10):
        m[i][ord(s[j])-a]+=1
  
#for i in range(n):
#  print(m[i])

s=[i for i in range(n)]
#print(s)
ans=0
f(0,s)

print(ans)

Submission status: Accepted

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

import math

N = int(input())

type_dict = {}
for _ in range(N):
    s = input()
    counts = (
        s.count('a'), s.count('b'), s.count('c'), s.count('d'),
        s.count('e'), s.count('f'), s.count('g'), s.count('h'),
        s.count('i'), s.count('j'), s.count('k'), s.count('l'),
        s.count('m'), s.count('n'), s.count('o'), s.count('p'),
        s.count('q'), s.count('r'), s.count('s'), s.count('t'),
        s.count('u'), s.count('v'), s.count('w'), s.count('x'),
        s.count('y'), s.count('z')
    )
    if counts in type_dict:
        type_dict[counts] = type_dict[counts] + 1
    else:
        type_dict[counts] = 1


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


print(sum([combinations_count(x, 2) for x in type_dict.values() if x > 1]))

Submission status: Accepted

Solution code:
n=int(input())
ls=sorted(["".join(sorted(list(input()))) for i in range(n)])

count=0
c=0
prev=ls[0]
for i,name in enumerate(ls):
    if name!=prev:
        count+=c*(c-1)/2
        c=0
        prev=name
    if i==n-1:
        if name==prev:
            c+=1
        count+=c*(c-1)/2
        c=0
        prev=name
    c+=1

print(int(count))