Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

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

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

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

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

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

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

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



Sample input:
3
acornistnt
peanutbomb
constraint


Sample output:
1

Submission status: Accepted

Solution code:
def sum_a(x):
    t = 0
    for i in range(x):
        t += i
    return t

N = int(input())
dic = {}

for i in range(N):
    s = input()
    s = ''.join(sorted(s))
    # print(s)
    if s in dic:
        dic[s] += 1
    else:
        dic[s] = 1

total = 0
for i in dic.values():
    if i > 1:
        total += sum_a(i)

print(total)

Submission status: Accepted

Solution code:
import itertools
from math import factorial
from scipy.misc import comb

N = int(input())
s = {}
for _ in range(N):
    b = list(input())
    b.sort()
    c = ''.join(b)
    if not c in s:
        s[c] = 1
    else:
        s[c] += 1

a = [comb(v, 2, exact=True) for v in s.values()]
print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
dic = dict()
ans = 0

for i in range(n):
    buf = input()
    seq = [buf[i] for i in range(len(buf))]
    seq.sort()
    bf2 = "".join(seq)
    
    if bf2 not in dic:
        dic[bf2] = 0
    else:
        dic[bf2] += 1
        ans += dic[bf2]

print(ans)

Submission status: Accepted

Solution code:
from math import factorial

N = int(input())
d = {}
for _ in range(N):
    s = input()
    s_sorted = "".join(sorted(s))
    if not d.get(s_sorted):
        d[s_sorted] = 1
    else:
        d[s_sorted] += 1

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

print(round(ans))

Submission status: Accepted

Solution code:
import collections

n = int(input())
L = []
cnt = 0
for i in range(n):
    s = sorted(input())
    s = "".join(s)
    L.append(s)
c = collections.Counter(L).values()
for i in c:
    cnt += i*(i-1)//2
print(cnt)

Submission status: Accepted

Solution code:
# ABC137 C

def resolve():
    n = int(input())
    table = {}
    for _ in range(n):
        s = input()
        arr = [c for c in s]
        arr.sort()
        ss = "".join(arr)
        if ss in table:
            table[ss] += 1
        else:
            table[ss] = 1

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input()) #get 1st row
inlist = [input() for i in range(N)] #read row (add int using brackets if needed)
result = {}

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

#make dic from input parameters
for component in inlist :
    dic = []
    for n in component:
        dic.append(n)
    dic.sort()
    dic2 = "".join(dic)
    if dic2 in result.keys():
        result[dic2] += 1
    else :
        result[dic2] = 1

#count the number
counter = 0
for v in result.values():
    counter += combination(v)

print (int(counter))

Submission status: Accepted

Solution code:
#abc137c

n=int(raw_input())
s=[''.join(sorted(raw_input())) for i in xrange(n)]
h={}
res=0
for si in s:
 if si in h:
  res+=h[si]
  h[si]+=1
 else:
  h[si]=1
print res

Submission status: Accepted

Solution code:
# coding: utf-8

def fact(n, m):
    ans = 1
    for i in range(n-m+1, n+1):
        ans *= i
    return ans

if __name__ == "__main__":
    N = int(input())
    s = []
    c = {}
    for i in range(N):
        a = "".join(sorted(input()))
        s.append(a)
        try:
            c[a] += 1
        except KeyError:
            c[a] = 1
    
    ans = 0
    for k,v in c.items():
        ans += fact(v,2)/2
    
    print(int(ans))

Submission status: Accepted

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

string=list()
for i in range(N):
  c=list(input())
  c.sort()
 # print(''.join(c))
  string.append(''.join(c))
  
#print(string)

a=collections.Counter(string)
b=list(a.values())

count=0
for j in range(len(b)):
  count=count +int(b[j]*(b[j]-1)/2)
  
print(count)

Submission status: Accepted

Solution code:
N = int(input())
l = [input() for i in range(N)]
mydict = {}
times = [1]*N
count = 0
value = 1

for i in range(N):
    l[i] = ''.join(sorted(l[i]))
    if l[i] in mydict.keys():
        count += times[mydict[l[i]]]
        times[mydict[l[i]]] += 1
    else:
        mydict[l[i]] = value
        value += 1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
S = [input() for _ in range(n)]
ans = 0

ctr = Counter()

for s in S : 
    a = ''.join(sorted(list(s)))
    if ctr[a] != 0 : 
        ans += ctr[a]
    ctr[a] += 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(s)
ans = 0

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

print(int(ans))

Submission status: Accepted

Solution code:
n,*sl, = map(lambda x:''.join(sorted(x)),open(0).read().split())
count = 0
dict ={}

for s in sl:
  if s in dict:
    count += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(count)

Submission status: Accepted

Solution code:
#n,*sl, = map(lambda x:''.join(sorted(x)),open(0).read().split())
n = int(input())
sl = []
for i in range(n):
  S = ''.join(sorted(input()))
  sl.append(S)
count = 0
dict ={}

for s in sl:
  if s in dict:
    count += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Ssort=[sorted(s) for s in S]
sort2=sorted(Ssort)

total=0
counter=0
ref=sort2[0]
for i in range(1,N):
    if(sort2[i]==ref):
        counter=counter+1
        if(i==N-1):
            total=total+sum(list(range(counter+1)))
    else:
        total=total+sum(list(range(counter+1)))
        counter=0
        ref=sort2[i]

print(total)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
r=[]
c=0
for i in range(n):
    a=input()
    b=[]
    for k in a:
      b.append(k)
    b.sort()
    s.append(b)
s.sort()
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
        r.append(c)
    else:
        c=0
        r.append(c)
print(sum(r))

Submission status: Accepted

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

for i in L.values():
    if i>1:
        ans+=(i*(i-1))//2
    else:
        continue
print(ans)

Submission status: Accepted

Solution code:
import itertools

n = int(input())

in_str = []
for _ in range(n):
    i = input()
    in_str.append(''.join(sorted(i)))


def cmb(n, r):
    """
    組み合わせ数
    https://qiita.com/derodero24/items/91b6468e66923a87f39f
    :param n:
    :param r:
    :return:
    """
    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


if __name__ == '__main__':
    count = 0
    for k, g in itertools.groupby(sorted(in_str)):
        n = sum(1 for _ in g)
        if n == 1:
            continue
        count += cmb(n, 2)
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
di = {}
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
d = {}

for _ in range(n):
    s = "".join(sorted(list(input())))
    if s not in d:
        d[s] = 0
    d[s] += 1

res = 0
for k in d:
    n = d[k]
    res += n*(n-1)//2

print(res)

Submission status: Accepted

Solution code:
n = int(input())
data = []
for i in range(n):
    s = list(input())
    s.sort()
    data.append(s)
data.sort()
i=0
j=0
count=1
decount=0
for i in range(n-1):
  if data[i]==data[i+1]:
    count=count+1
  else:
    decount=decount+(count-1)*count/2
    count=1
decount=decount+(count-1)*count/2
print(int(decount))

Submission status: Accepted

Solution code:
from math import factorial
def comb(n, r):
  return factorial(n) // factorial(r) // factorial(n - r)

N = int(input())
S = []
for i in range(N):
  S.append(''.join(sorted(input())))
S = sorted(S)
S.append('ZZZZZZZZZZ')
ans = 0
count = 1
for i in range(1,len(S)):
  if S[i] == S[i-1]:
    count += 1
  else:
    if count > 1:
      ans += comb(count, 2)
    count = 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
str = {}  # 辞書
ans = 0
for i in range(N):
	word = []
	tmp = input()
	for j in range(10):
		word.append(tmp[j])
	word.sort()  # s_iがソートされる
	word_add = ""
	for j in range(10):
		word_add += word[j]
		# word_addにs_iがソートされたものが入る
	
	# "aaabc"があればstr["aaabc"]を1増やし、
	# なければ新しく作って初期値1を入れる 
	if word_add in str:
		str[word_add] += 1
	else:
		str[word_add] = 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
words = {}
sum_cnt = 0

while n > 0:
    word = sorted(input())
    word = "".join(word)
    if word in words:
        sum_cnt += words[word]
        words[word] += 1
    else:
        words[word] = 1
    n -= 1

print(int(sum_cnt))

Submission status: Accepted

Solution code:
n = int(input())
combiList = {}
sum_cnt = 0

for _ in range(n):
    act_string = sorted(input())
    act_string = "".join(act_string)
    cnt = combiList.get(act_string)
    if cnt is None:
        combiList[act_string] = 1
    else:
        combiList[act_string] += 1
        sum_cnt += cnt

print(int(sum_cnt))

Submission status: Accepted

Solution code:
n = int(input())
combiList = {}
sum_cnt = 0

for _ in range(n):
    act_string = list(input())
    act_string.sort()
    act_string = "".join(act_string)
    cnt = combiList.get(act_string)
    if cnt is None:
        combiList[act_string] = 1
    else:
        combiList[act_string] += 1
        sum_cnt += cnt

print(int(sum_cnt))

Submission status: Accepted

Solution code:
import sys 
from collections import Counter

def main():
  input = sys.stdin.readline
  N = int(input())
  texts = [''.join(sorted(list(input().rstrip()))) for _ in range(N)]
  c = Counter(texts)
  cnt = 0
  for t in c.values():
    if t > 1:
      cnt += (t * (t-1)) // 2
  print(cnt)
 
if __name__ == '__main__':
  main()

Submission status: Accepted

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

for _ in range(n):
    act_string = list(input())
    act_string.sort()
    act_string = "".join(act_string)
    
    if combiList.get(act_string) is None:
        combiList[act_string] = 1
    else:
        combiList[act_string] += 1
sum_cnt = 0
for cnt in combiList.values():
    sum_cnt += cnt * (cnt - 1) / 2

print(int(sum_cnt))

Submission status: Accepted

Solution code:
N = int(input())
s = {}
for i in range(N):
	word = []
	tmp = input()
	for j in range(10):
		word.append(tmp[j])
	word.sort()
	word_add = ""
	for j in range(10):
		word_add += word[j]

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

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

Submission status: Accepted

Solution code:
import sys 
from collections import Counter
from math import factorial

def combinations_count(n, r):
  return factorial(n) // (factorial(n - r) * factorial(r))
 
def main():
  input = sys.stdin.readline
  N = int(input())
  texts = [''.join(sorted(list(input().rstrip()))) for _ in range(N)]
  c = Counter(texts)
  cnt = 0
  for t in c.values():
    if t > 1:
      cnt += combinations_count(t, 2)
  print(cnt)
 
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
S = sorted([''.join(sorted(input())) for _ in range(N)])
S.append('')
keep = ''
result = cnt = 0
for s in S:
    if keep == s:
        cnt += 1
    else:
        result += ((cnt - 1) * cnt) // 2
        cnt = 1
        keep = s
print(result)

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce

N = int(input())
S = []
for _ in range(N):
    S.append(''.join(sorted(input())))
print(int(reduce(lambda x, y: x + y, map(lambda x: ((x - 1) * x) / 2, Counter(S).values()))))

Submission status: Accepted

Solution code:
N=int(input())
s=sorted([str(sorted(input())) for i in range(N)])+[""]
ans=c=0
j=""
for i in s:
    if i==j:
        c+=1
    else:
        j=i
        ans+=c*(c+1)//2
        c=0
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=sorted([str(sorted(input())) for i in range(N)])+[""]
ans=c=0
j=s[0]
for i in s[1:]:
    if i==j:
        c+=1
    else:
        j=i
        ans+=c*(c+1)//2
        c=0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lists = [input() for _ in range(n)]

for i in range(n):
    lists[i] = ''.join(sorted(lists[i]))

d = {}
for i in range(n):
    if lists[i] in d:
        d[lists[i]] += 1
    else:
        d[lists[i]] = 1

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

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
str_dict = dict()
total_comb_num = 0
for _ in range(N):
    tmp_string = input()
    sorted_tmp_string = ''.join(sorted(tmp_string)) 
    if sorted_tmp_string in str_dict:
        total_comb_num += str_dict[sorted_tmp_string]
        str_dict[sorted_tmp_string] += 1
    else:
        str_dict[sorted_tmp_string] = 1
        
print(total_comb_num)

Submission status: Accepted

Solution code:
N = int(input())


## アナグラム的に一致した回数を辞書に格納
dict_s = {}
for n in range(N):
  _ = ''.join(sorted(input()))
  if _ in dict_s:
    dict_s[_] += 1
  else:
    dict_s[_] = 1

## 組み合わせ数を計算
from operator import mul
from functools import reduce

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

cnt_anagram = 0
for v in dict_s.values():
  if v > 1:
  	cnt_anagram += cmb(v, 2)

## 出力
print(cnt_anagram)

Submission status: Accepted

Solution code:
n=int(input())
d={}
totl=0

for i in range(n):
  s=list(input())
  s.sort()
  t=''.join(s)
  
  if t in d:
    d[t]+=1
  else:
    d[t]=1

for t in d:
  totl+=d[t]*(d[t]-1)/2

print(int(totl))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
strings = []
for i in range(N):
    string = list(input())
    string.sort()
    strings.append(string)
strings.sort()
wordAnagramCou = 0
anagramCou = 0
for i in range(N-1):
    if strings[i+1] == strings[i]:
        wordAnagramCou += 1
    else:
        wordAnagramCou=0
    anagramCou += wordAnagramCou
print(anagramCou)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    cnt = dict()
    for i in range(n):
        t = input()
        s = "".join(sorted(t))
        try:
            cnt[s] += 1
        except:
            cnt[s] = 1
    ans = 0
    for v in cnt.values():
        ans += (v * (v - 1)) // 2
    print(ans)

main()

Submission status: Accepted

Solution code:
import math
n = int(input())
anagram = {}
while n > 0:
    wordbef = list(input())
    wordbef.sort()
    word = "".join(wordbef)
    if word in anagram:
        anagram[word] = anagram[word] + 1
    else:
        anagram[word] = 1
    n -= 1
total = 0
for x in anagram:
    total += (anagram[x] * (anagram[x] - 1))/2
print(int(total))

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())
lst_s = ["".join(sorted(input())) for _ in range(N)]
lst_s.sort()

cnt_s = Counter(lst_s)

ans = 0
for v in cnt_s.values():
    if v == 1:
        continue
    else:
        ans += cmb(v, 2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
sum = 0

for i in range(n):
  s = list(input())
  s.sort()
  t = "".join(s)
  s.clear()
  if t in d:
    d[t] += 1
  else:
    d[t] = 1
    
for i in d:
  if d[i] > 1:
    sum += (d[i]*(d[i]-1))/2

print(int(sum))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

for i in range(n):
    si = list(input())
    si.sort()
    str_si = ""
    for j in range(10):
        str_si += si[j]
    if str_si not in dic:
        dic[str_si] = 1
    else:
        dic[str_si] += 1

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

print(tot)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = {}
num = 0
for i in range(N):
  s = ''.join(sorted(list(input())))
  if s in S:
    num += S[s]
    S[s] += 1
  else:
    S[s] = 1
print(num)

Submission status: Accepted

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

ans = 0
w_dic = {}
for l in li:
  if not w_dic.get(l):
    w_dic[l] = 1
  else:
    ans += w_dic[l]
    w_dic[l] = w_dic[l] + 1

print(int(ans))

Submission status: Accepted

Solution code:
n,*sl, = map(lambda x:''.join(sorted(x)),open(0).read().split())
count = 0
dict = {}
for s in sl:
  if s in dict:
    count += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC137 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
s = [list(input()) for _ in range(n)]
for i in s:
    i.sort()
s = [''.join(i) for i in s]
t = list(Counter(s).items())
ans = 0
for i,j in t:
    if j == 1:
        continue
    else:
        ans += (j)*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC137 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def comb(n,r):
    return math.factorial(n)//math.factorial(n-r)//math.factorial(r)
n = I()
s = [input() for _ in range(n)]
x = [list(Counter(s[i]).items()) for i in range(n)]
for i in x:
    i.sort()

y = [tuple(i) for i in x]
f = defaultdict(lambda : 0)
for i in range(n):
    f[y[i]] += 1
ans = 0
for i in f.values():
    if i < 2:
        continue
    ans += comb(i,2)
print(ans)

Submission status: Accepted

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


print(sum(result.values()))

Submission status: Accepted

Solution code:
import collections
 
N = int(input())
#read strings and sort each one alpha!
s = ["".join(sorted(input())) for i in range(N)]

# count every word occurences
c = collections.Counter(s)

cnt = 0
for i in set(s):
    n = c[i]
    # erase repititions
    cnt += n*(n-1)//2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
check = {}
lst = []

for _ in range(n):
  s = list(input())
  s.sort()
  s = "".join(s)
  lst.append(s)
  try:
    check[s]+=1
  except:
    check.update({s:1})
  
cnt=0
check_set = set(lst)
for c in check_set:
  temp_cnt = check[c]
  cnt +=  temp_cnt*(temp_cnt-1)/2
print(int(cnt))

Submission status: Accepted

Solution code:
import collections

N = int(input())
s = ["".join(sorted(input())) for i in range(N)]
c = collections.Counter(s)
cnt = 0
for i in set(s):
    n = c[i]
    cnt += n*(n-1)//2
print(cnt)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = collections.Counter(["".join(sorted(list(input()))) for _ in range(n)]).most_common()
t = 0
for i in s:
    for j in range(1, i[1]):
        t += j
print(t)

Submission status: Accepted

Solution code:
def solve(n, s_list):
    # time O(N log(N))
    sorted_list = sorted(s_list)

    ans = 0
    key = 0
    # time O(N)
    while key < n - 1:
        group_cnt = 0
        target = sorted_list[key]
        while key < n - 1 and target == sorted_list[key + 1]:
            group_cnt += 1
            key += 1
        if group_cnt:
            ans += group_cnt * (group_cnt + 1) // 2
        key += 1

    return ans


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

    s_list = []
    # time O(N)
    for i in range(n):
        s = "".join(sorted(input()))
        s_list.append(s)

    ans = solve(n, s_list)
    print(ans)


main()

Submission status: Accepted

Solution code:
n=int(input())
s=[''.join(sorted(input())) for _ in range(n)]
dic={}
ans=0

for i in s:
    t=dic.get(i,0)
    ans+=t
    dic[i]=t+1

print(ans)

Submission status: Accepted

Solution code:
import collections

def solve(n, s):
    records = []
    D = {}
    for i in range(n):
        counter = collections.Counter(s[i])
        k = ",".join(["%s:%d" % (k,v) for k, v in sorted(counter.items())])
        if not k in D:
            D[k] = 0
        D[k] += 1
    res = 0
    for k, v in D.items():
        res += v * (v-1) // 2
    return res

n = int(input())
s = [input() for i in range(n)]
print(solve(n, s))

Submission status: Accepted

Solution code:
def main():
     AlfaToNum={'a':2,'b':3,'c':5,'d':7,'e':11,'f':13,'g':17,'h':19,'i':23,'j':29,'k':31,'l':37,'m':41,'n':43,'o':47,'p':53,'q':59,'r':61,'s':67,'t':71,'u':73,'v':79,'w':83,'x':89,'y':97,'z':101}
     n=int(input())
     s=[]
     ans=0
     for i in range(n):
          si=input()
          cnt=1
          for j in si:
               cnt*=AlfaToNum[j]
          s.append(cnt)
     #print(s)
     s.sort()
     cnt=1
     for i in range(n-1):
          if s[i]==s[i+1]:
               cnt+=1
          else:
               ans+=cnt*(cnt-1)//2
               cnt=1
     ans+=cnt*(cnt-1)//2
     print(ans)
if __name__ == '__main__':
     main()

Submission status: Accepted

Solution code:
d={}
for _ in range(int(input())):
  s=input()
  s=list(s)
  s.sort()
  t=''.join(s)
  if t in d:
    d[t]+=1
  else:
    d[t]=1
c=0
# print(d)
for i in d:
  c+=(d[i]*(d[i]-1))//2
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[None for i in range(n)]
for i in range(n):
  l[i]=sorted(input())
l.sort()
l+="l"
cnt=1
ans=0
for i in range(n):
  if l[i]!=l[i+1]:
    ans+=(cnt-1)*cnt//2
    cnt=1
    continue
  cnt+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
dd = defaultdict(int)
ans = 0
for _ in range(N):
    s = ''.join(sorted(list(input())))
    ans += dd[s]
    dd[s] += 1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline())
    s = [stdin.readline().rstrip().split() for _ in range(N)]

    s_flat = [flatten for inner in s for flatten in inner]

    s_dict = {}
    sum = 0

    for S in s_flat:
        new_S = ''.join(sorted(S))
        if not s_dict.get(new_S):
            s_dict[new_S] = 1
        else:
            sum += s_dict[new_S]
            s_dict[new_S] = s_dict[new_S] + 1

    print(sum)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
  s.append(''.join(sorted(list(input().strip()))))
ans = 0
tmp = 0
ss = sorted(s)
prev = ''
for iss in ss:
  if prev != iss:
    prev = iss
    ans += int((tmp*(tmp+1))/2)
    tmp = 0
  elif prev == iss:
    # print("prev :{}, iss :{}".format(prev, iss))
    tmp += 1
if tmp != 0:
  ans += int((tmp*(tmp+1))/2)
print(ans)

Submission status: Accepted

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

ctr = Counter(arr)

res = 0
for x in ctr:
    res += ctr[x]*(ctr[x]-1)//2

print(res)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]

ctr = Counter()
ans = 0
for s in S:
    a = ''.join(sorted(list(s)))
    if a in ctr:
        ans += ctr[a]
    ctr[a] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

#input
c = int(input())
str_input = []
sort_str_input = []

for i in range(c):
    str_input.append(input())
    char_input = []
    for j in range(len(str_input[i])):
        char_input.append(str_input[i][j])
    char_input.sort()
    sort_str_input.append(''.join(map(str,char_input)))

test = Counter(sort_str_input)
test = test.most_common()
count=0
for i in range(len(test)):
    for j in range(test[i][1]-1):
        count = count +(test[i][1]-1-j)
print(count)

Submission status: Accepted

Solution code:
from math import factorial

n = int(input())
s_dict = {}
for i in range(n):
  s = "".join(sorted(list(input())))
  if s in s_dict:
    s_dict[s] += 1
  else:
  	s_dict[s] = 1
  
count = 0
for v in s_dict.values():
  if v > 1:
    count += factorial(v) // (factorial(2) * factorial(v - 2))

print(count)

Submission status: Accepted

Solution code:
import collections

N = int(input())
lst = [''.join(sorted(input())) for _ in range(N)]
ret = 0
for x in collections.Counter(lst).values():
    ret += (x * (x - 1) // 2)
print(ret)

Submission status: Accepted

Solution code:
# 提出一覧から持ってきた解答
# わかりやすいように各所分解してます

from collections import Counter

N = int(input())

s = []
for _ in range(N):
    # 入力値をソートしてリストにする
    s_input = sorted(input())
    # ソート済みのリストを文字列に変換する
    s2_input = ''.join(s_input)
    # ソート済み文字列用の配列に突っ込む
    s.append(s2_input)

cnt = Counter(s)

full_cnt = 0
for c in cnt.values():
    if c >= 2:
        # 1からnまでの和 -> {(n+1)*n}//2
        # 同じ文字列がa個存在した場合、その組み合わせは重複を排除して{1からa-1までの和}通りある。
        full_cnt += c * (c - 1) // 2


print(full_cnt)

Submission status: Accepted

Solution code:
from collections import Counter as c
N = int(input())
a = []
for _ in range(N):
  a.append(" ".join(sorted(input())))
a.sort()
l = c(a).items()
res = 0
for e in l:
  k = e[1]
  res += k * (k - 1) // 2
print(res)

Submission status: Accepted

Solution code:
N = int(input())
li = list("".join(sorted(input())) for _ in range(N))
dic = {}
ans = 0
for i in range(N):
  if li[i] in dic:
    ans += dic[li[i]]
    dic[li[i]] += 1
  else:
    dic[li[i]] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
# print(n)
# print(s)
s = list(map(list, s))
# print(s)
for i in range(n):
    s[i].sort()
    s[i] = "".join(s[i])
# print(s)
dic = {}
for i in range(n):
    if s[i] in dic:
        dic[s[i]] += 1
    else:
        dic.setdefault(s[i],1)
a = list(dic.values())
# print(a)
import math
count = 0
for i in range(len(a)):
    if a[i] >= 2:
        count += (math.factorial(a[i])//(math.factorial(a[i]-2)*2))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
# print(n)
# print(s)
s = list(map(list, s))
# print(s)
for i in range(n):
    s[i].sort()
    s[i] = "".join(s[i])
# print(s)
dic = {}
for i in range(n):
    if s[i] in dic:
        dic[s[i]] += 1
    else:
        dic.setdefault(s[i],1)
a = list(dic.values())
# print(a)
import math
count = 0
for i in range(len(a)):
    if a[i] >= 2:
        count += int(math.factorial(a[i])/(math.factorial(a[i]-2)*2))
print(count)