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:
N = int(input())
ans = 0
S = {}
for i in range(N):
    s = ''.join(sorted(input()))
    if s not in S.keys():
        S[s] = 0
        continue
    S[s] += 1
    ans += S[s]
print(ans)

Submission status: Accepted

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

for i in range(n):
    s = "".join(sorted(input()))
    if s in d: d[s] += 1
    else: d[s] = 1
ans = 0
for i in d.values():
    ans += (i*i-i)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
strdict = {}
count = 0
for i in range(n):
    sortedstr = "".join(sorted(str(input())))
    strdict.setdefault(sortedstr, 0)
    count += strdict[sortedstr]
    strdict[sortedstr] += 1
print(count)

Submission status: Accepted

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

dict = {}
for i in range( n ):
	sorted_input = "".join( sorted( list( input() ) ) )
	if dict.get( sorted_input ) == None:
		dict[ sorted_input ] = 1
	else :
		dict[ sorted_input ] += 1

count = 0
for eachValue in dict.values():
	if eachValue == 1 : continue
	count += cmb( eachValue, 2 )
print( count )

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
 
for i in range(N):
    s = str(sorted(input()))
    d[s] += 1
ans = 0
 
for key in d.keys():
    ans += d[key]*(d[key]-1)//2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
dict={}
for _ in range(N):
  s=''.join(sorted(input()))
  dict[s]=dict.setdefault(s,0)+1
  
cnt=0
for c in dict.values():
  cnt+=(1+c-1)*(c-1)//2
  
print(cnt)

Submission status: Accepted

Solution code:
from collections import*;print(sum(c*~-c//2for c in Counter(map(tuple,map(sorted,open(0)))).values()))

Submission status: Accepted

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

def cmb(n,r):
	if n < r:return 0
	if r == 0 or n-r == 0:return 1
	r = min(n-r,r)
	up = reduce(mul, range(n, n-r, -1))
	down = reduce(mul, range(1,r+1))
	return up//down
    
l = []
for i in range(int(input())):
	l.append("".join(sorted(input())))
c = Counter(l)
ans = 0
for i in c.values():
	ans += cmb(i,2)
print(ans)

Submission status: Accepted

Solution code:
def C_GreenBin():
    from collections import Counter
    N = int(input())
    s = []
    for x in range(N):
        list_tmp = sorted(input())
        s.append("".join(list_tmp))
    result = 0
    s_tmp = Counter(s)
    for x in s_tmp.values():
        if x > 1:
            result += ((x - 1) * x) // 2
    print(result)
    
if __name__ == "__main__":
    C_GreenBin()

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 = [''.join(sorted(list(input()))) for i in range(N)]

dic={}
for i in s:
	if i in dic:
		dic[i] +=1
	else:
		dic[i] = 1

result = 0
for j in dic.values():
	if j > 1:
		result += nCr(j,2)

print(result)

Submission status: Accepted

Solution code:
N = int(input())
strDict = {} 
for i in range(N):
    inStr = input()
    inList = []
    for j in range(len(inStr)):
        inList.append(inStr[j])
        inList.sort()
    sortStr = ''.join(inList)
    if sortStr in strDict:
        strDict[sortStr] = strDict[sortStr] + 1
    else:
        strDict[sortStr] = 1

anaglam = 0

for i in strDict.keys():
    anaglam = anaglam + strDict[i]*(strDict[i]-1)//2
    
print(anaglam)

Submission status: Accepted

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

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)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    s = list(sys.stdin.readlines())
    new_s=sorted(s)
    char_list=[]
    dic ={}
    for i in s:
        i = i.replace('\n','')
        new_s=sorted(i)
        new_s = ''.join(new_s)
        char_list.append(new_s)
        if new_s not in dic:
            dic[new_s] = 1
        else:
            dic[new_s] += 1
    # c = 0
    # for j in range(n):
    #     if char_list[j] in char_list[j+1:]:
    #         c+=char_list[j+1:].count(char_list[j])
    # print(c)

    def combinations_count(n, r):
        if n - r < 0:
            return 0
        else:
            return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    # print(char_list)
    # print(dic)
    c=0
    # print(dic.values())
    for i in dic.values():
        c+=combinations_count(i, 2)
    print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

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

line = defaultdict(int)

for anagram in s:
  a = sorted(anagram)
  line[''.join(a)] += 1

_sum = 0

for i in line:
  _sum += int((line[i] * (line[i] - 1)) / 2)

print(_sum)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
ans = 0
for i in range(N):
    s = [v for v in input()]
    s.sort()
    word = ''.join(s)    

    if word in dic:
        dic[word] += 1
    else:
        dic[word] = 1

for word in dic:
    num = dic[word]
    ans += (num * (num - 1)) // 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for cc in liss:
  ans = ans + (liss[cc] * (liss[cc] - 1) / 2)
print(int(ans))

Submission status: Accepted

Solution code:
import collections as c
import math as m
print(sum([ m.factorial(n) // (m.factorial(n - 2) * 2) for n in c.Counter(map(lambda x:"".join(x),map(sorted,[input()for i in range(int(input()))]))).values() if n>1]))

Submission status: Accepted

Solution code:
N = int(input())
S = []
count = 0

dic = {}
for i in range(N):
    S.append(input())
    S[i] = sorted(S[i])
    S[i] = ''.join(S[i])
    if dic.setdefault(S[i], 0) > 0:
        count += dic[S[i]]
        dic[S[i]] += 1
    else:
        dic[S[i]] = 1
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial


def perm(n, r):
    return factorial(n) // factorial(n - r)


def comb(n, r):
    return perm(n, r) // factorial(r)


n = int(input())
D = defaultdict(lambda: 0)
for _ in range(n):
    D["".join(sorted(input()))] += 1
print(sum([comb(v, 2) for v in D.values() if v > 1] + [0]))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict as dd

#sys.stdin = open("third.txt","r")

def main():

	d = dd(int)
	for x in xrange(int(raw_input())):
		s = raw_input().rstrip()
		v = {i:0 for i in xrange(26)}
		for c in s:
			v[ord(c)-97]+= 1
		tu = ()
		for i in xrange(26):
			tu = tu + (v[i],)

		d[tu]+=	 1	

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

	print "%d"%ans

raw_input = sys.stdin.readline

if __name__ == '__main__':
	#for j in xrange(int(raw_input())):
	main()

Submission status: Accepted

Solution code:
N = int(input())

S, ans = {}, 0
for i in range(N):
    s = ''.join(sorted(list(input())))
    S[s] = S[s] + 1 if s in S else 1

for c in S.values():
    ans += c * (c - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = []
for i in range(0,N):
    a = input()
    s.append(sorted(a))
s = sorted(s)
t = 0
i = 0
while i<N:
    for j in range(i+1,N):
        if s[i]==s[j]:
            t = t+j-i
        else:
            break
    i = j
    if i==N-1:
        break
print(t)

Submission status: Accepted

Solution code:
from collections import Counter as C
n,*s=open(0)
print(sum(x*(x-1)//2 for x in C(tuple(sorted(i.strip()))for i in s).values()))

Submission status: Accepted

Solution code:
import collections
N = int(input())
s = [list(input()) for _ in range(N)]
mydict = {}
ans = 0
for i in s:
  a = str(sorted(i))
  if a in mydict:
    ans += mydict[a]
    mydict[a] += 1
  else:
    mydict.setdefault(a,1)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
Sdic = defaultdict(int)

N = int(input())
S = ['']* N
ans = 0
for i in range(N):
    Sdic[str(sorted(input()))] += 1

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    count = 0
    count_dict = {}

    for i in range(n):
        input_data = ''.join(sorted(input()))
        if input_data in count_dict:
            count += count_dict[input_data]
            count_dict[input_data] += 1
        else:
            count_dict[input_data] = 1

    print(count)


if __name__ == '__main__' :
    main()

Submission status: Accepted

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

#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())
    sl = []
    for _ in range(N):
        tmp = sorted(sys.stdin.readline().rstrip())
        sl.append(''.join(tmp))
    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:
N = int(input())
ss = {}
ht = []
ans = 0

for i in range(N):
    s = ''.join(sorted(input()))
    if not s in ss.keys():
        ss[s] = 1
    else:
        ans += ss[s]
        ss[s] += 1

print(ans)

Submission status: Accepted

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

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())
    sl = []
    for _ in range(N):
        tmp = sorted(sys.stdin.readline().rstrip())
        sl.append(''.join(tmp))
    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:
n = int(input())

slist = []
for h in range(n):
  slist.append(input())

newlist = []
word=[]
wordy = ""
for i in range(n):
  word = list(slist[i])
  word.sort()
  for jk in range(len(word)):
	  wordy = wordy+word.pop()
  newlist.append(wordy)
  wordy=""

resdic = {}
count = 0
for item in newlist:
  if not item in resdic:
    resdic[item] = 1
  else:
    count += resdic[item]
    resdic[item] += 1
    
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A=[]
S={}
for i in range(N):
    a = list(input())
    a.sort()
    b = ''.join(a)
    if b not in S:
        S[b] = 1
    else:
        S[b] += 1


count=0
for i in S.values():
    if i==1:
        continue
    count += i*(i-1)//2
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

line={}
c=0

for i in range(n):
    string = ''.join(sorted(list(input())))
    # print(string)
    if string in line:
        c+=line[string]
    if string in line:
        line[string]+=1
    else:
        line[string]=1
    # print(line,c)

print(c)

Submission status: Accepted

Solution code:
N = int(input())
d = dict()

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

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

Submission status: Accepted

Solution code:
import collections

cnt = 0
s = []
tmp = []
n = int(input())
for i in range(n):
    string = input()
    string = ''.join(sorted(string))
    s.append(string)

# for i, e in enumerate(s):
#     count = s.count(e)
#     if count > 1 and not e in tmp:
#         cnt += count*(count-1)//2
#         tmp.append(e)
cc = collections.Counter(s)

for cv in cc.values():
    if cv > 1:
        cnt += cv*(cv-1)//2

print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from operator import mul
from functools import reduce

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

n = int(input())
s = {}
for i in range(n):
    str = ''.join(sorted(input()))
    if str in s.keys():
        s[str] += 1
    else:
        s[str] = 1
ans = 0
for i in s:
    ans += combination(s[i], 2)
print(ans)

Submission status: Accepted

Solution code:
n =int(input())
A=[]
for I in range(n):
  moji=list(input())
  moji.sort()
  moji=''.join(moji)
  A.append(moji)
A.sort()
N=0
SUM=0
i=0
while True:
  if i==n-1:
    SUM=SUM+int(0.5*N*(N+1))
    break
  a=A[i]
  for s in range(i+1,n,1):
    b=A[s]
    if a==b:
      N+=1
      i+=1
    else:
      SUM=SUM+int(0.5*N*(N+1))
      N=0
      i=s
      break
print(SUM)

Submission status: Accepted

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

print(k)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
count = 0
lists =[]
count_dict = Counter()

for i in range(n):
    input_data = ''.join(sorted(input()))
    if input_data in count_dict:
        count += count_dict[input_data]
        count_dict[input_data] += 1
    else:
        count_dict[input_data] = 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
 
string = {}
count = 0

for i in range(n):
    base = sorted(input())

    if string.get("".join(base)) != None:
        count += int(string["".join(base)])
        string["".join(base)] += 1
    else:
        string["".join(base)] = 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
s = ["".join(sorted(input())) for i in range(N)]
s.sort()
ss = s[0]
num = 0
ans=0
for i in range(N):
    if ss == s[i]:
        num+=1
    else:
        ans+=num*(num-1)//2
        num=1
        ss=s[i]
ans+=num*(num-1)//2
print(ans)

Submission status: Accepted

Solution code:
def sort_str(s):
  temp = sorted(list(s))
  return ''.join(list(temp))

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

s_dict = {}
count = 0

for s_i in s:
  k = sort_str(s_i)
  if k in s_dict:
    count += s_dict[k]
    s_dict[k] += 1
  else:
    s_dict[k] = 1
    
print(count)

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C
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())
s = []
for i in range(N):
    tmp = list(input())
    tmp = sorted(tmp)
    tmp = ','.join(tmp)
    s.append(tmp)
c = collections.Counter(s)
ans_list = c.values()
ans_list = [i for i in ans_list if i >= 2]
ans = 0
if len(ans_list) != 0:
    for i in ans_list:
        ans += cmb(i, 2)
    print(ans)
else:
    print(ans)

Submission status: Accepted

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

count=0
for i in range(n):
  s_elem=''.join(sorted(list(input())))
  if s_elem in dic:
    count+=dic[s_elem]
    dic[s_elem]+=1
  else:
    dic[s_elem]=1

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
l={}
ans=0
for i in range(n):
  s=input()
  ss=sorted(s)
  s="".join(ss)
  ans+=l.setdefault(s, 0) 
  l[s]+=1
print(ans)

Submission status: Accepted

Solution code:
from math import factorial
from collections import Counter
 
def conb(n):
    if n <= 1:
        return 0
    else:
        return factorial(n) // factorial(n-2) // factorial(2) 

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

print(int(sum([conb(i) for i in Counter(S).values()])))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
ans = 0

l = ["".join(sorted(input())) for _ in range(n)]

a = Counter(l).values()

for i in a:
    ans += sum(range(i))

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
def input(): return sys.stdin.readline().strip()
def gcd(x, y): return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y): return (x * y) // gcd(x, y)
MOD = 10 **9 + 7

N = int(input())
# S = input()
# N, M = map(int, input().split())
# 横
# L = [int(x) for x in input().split()]
# 縦
S = [''.join(sorted(input())) for _ in range(N)]
# 多次元
# L = [[int(x) for x in input().split()] for _ in range(N)]

ans = 0
dic = {}
for s in S :
    if not s in dic :
        dic[s] = 0
    
    ans += dic[s]
    dic[s] += 1

print(ans)

Submission status: Accepted

Solution code:
# 入力部
N = int(input())

s = list()
for i in range(0, N):
  val = str(input())
  s.append(val)
  
# 計算部
result_dic = {}

for i in range(0, len(s)):
  sorted_val = ''.join(sorted(s[i]))
  if sorted_val in result_dic:
    result_dic[sorted_val] += 1
  else:
    result_dic[sorted_val] = 1

result_count = 0
for val in result_dic.values():
  result_count += val * (val - 1) // 2

print(result_count)

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

Solution code:
N = int(input())
para = [[c for c in input()] for i in range(N)]
b = []
dict = {}
ans = 0
for i in range(N):
    para[i].sort()
    b.append("".join(para[i]))
    if b[i] not in dict:
        dict[b[i]] = 1
    else :
        ans += dict[b[i]]
        dict[b[i]] += 1 

print(ans)

Submission status: Accepted

Solution code:
d = {}
n = int(input())
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
for k in d:
    if d[k] != 1:
        ans += d[k]*(d[k]-1)/2
print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

def p_c():
    n = int(input())
    ans = 0

    l = ["".join(sorted(input())) for _ in range(n)]

    a = Counter(l).values()

    for i in a:
        ans += sum(range(i))

    print(ans)
    
    
if __name__ == '__main__':
    p_c()

Submission status: Accepted

Solution code:
N=int(input())
dic={}
for i in range(N):
    k="".join(sorted(list(input())))
    if k in dic:
        dic[k]+=1
    else:
        dic[k]=1
c=0
for i in dic.values():
    c += i*(i-1)

print(c//2)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(n):
  s.append("".join(sorted(input())))
#sss=set(s)
ans=0
import collections
c=collections.Counter(s)

for l in c.values():
  ans+=l*(l-1)*(1/2)
print(int(ans))

Submission status: Accepted

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

num = 0
num_same = 1
prev = sorted_s[0]
for i in range(1, len(sorted_s)):
    if prev != sorted_s[i]:
        num += int(num_same * (num_same - 1) / 2)
        num_same = 1
        prev = sorted_s[i]
    else:
        num_same += 1
num += int(num_same * (num_same - 1) / 2)
print(num)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = ["".join(sorted(input().split()[0])) for _ in range(n)]
cnt = {}
result = 0
for si in s:
    if si in cnt:
        result += cnt[si]
        cnt[si] += 1
    else:
        cnt[si] = 1
print(result)

Submission status: Accepted

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

dic = {}
ans = 0
for s in S:
    sortstr = str(sorted(s))
    if sortstr in dic:
        ans += dic[sortstr]
        dic[sortstr] += 1
    else:
        dic[sortstr] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
count = 0
ls = dict([])
for i in range(n):
    si = ''.join(sorted(input()))
    if si in ls.keys():
        count += ls[si]
        ls[si] += 1
    else:
        ls[si] = 1
print(count)

Submission status: Accepted

Solution code:

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

    A = []
    for _ in range(N):
        a = sorted(list(input()))
        A.append(''.join(a))
    td = {}
    for t in A:
        if t not in td.keys():
            td[t] = 1
        else:
            td[t] += 1
    count = 0
    for v in td.values():
        count += v * (v - 1) // 2
    print(count)


main()

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()
     k = ''.join(k)
     if k in dp:
         dp[k] += 1
     else:
         dp[k] = 1

ans = 0

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

print(int(ans))

Submission status: Accepted

Solution code:
# coding: utf-8
import math

anagramed = []

def main():
    # n = map(int, input().split(" "))
    n = int(input())
    m = []
    r = {}
    count = 0

    for _ in range(n):
        m.append(input())
        anagramed.append(False)

    for i in range(n):
        s = "".join(sorted(m[i]))
        if r.get(s) is None :
            r[s] = 1
        else:
            count += r[s]
            r[s] += 1
    print(count)


main()

Submission status: Accepted

Solution code:
n = int(input())

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

s.sort()

cnt = 0
t_cnt = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        t_cnt += 1
    elif s[i] != s[i+1]:
        if t_cnt > 1:
            t_cnt += 1

            t = 0
            for j in range(t_cnt):
                t += j

            cnt += t
            t_cnt = 0
        else:
            cnt += t_cnt
            t_cnt = 0

    if i == n-2:

        if t_cnt > 1:
            t_cnt += 1

            t = 0
            for j in range(t_cnt):
                t += j

            cnt += t
            t_cnt = 0
        else:
            cnt += t_cnt
            t_cnt = 0

print(cnt)

Submission status: Accepted

Solution code:
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())
ss = []
for i in range(N):
	ss.append("".join(sorted(list(input()))))
	
dicS = {}
for s in ss:
	if s not in dicS:
		dicS[s] = 1
	else:
		dicS[s] += 1
		
#print(dicS)
		
ans = 0
for v in dicS.values():
	if v != 1:
		ans += cmb(v, 2)
	
print(ans)

Submission status: Accepted

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

ans = 0
d = {}
for i in s:
    if i in d:
        ans += d[i]
        d[i] += 1
    else:
        d[i] = 1
print(ans)

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())
s = [" ".join(sorted(list(input()))) for _ in range(N)]
c = collections.Counter(s)
ans = 0
for i in c.values():
    if i > 1:
        ans += cmb(i, 2)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

s_abc = {}
multiple = []
count = 0
for i in range(n):
    abc_dict = dict.fromkeys(alphabet, 0)
    s = input()
    for c in s:
        abc_dict[c] += 1
    lst = []
    for x in abc_dict.values():
        lst.append(str(x))
    strlst = ''.join(lst)
    if strlst in s_abc:
        s_abc[strlst] += 1
    else:
        s_abc[strlst] = 1
for v in s_abc.values():
    if v != 1:
        count += math.factorial(v) // (math.factorial(v - 2) * 2)

print(count)

Submission status: Accepted

Solution code:
n = int(input())
# 文字列内の文字を辞書順ソートしたリストを辞書順ソート
strings = sorted([''.join(sorted(list(input()))) for i in range(n)])
strings.append('XXXXXXXXXX')

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
s=["".join(sorted(input())) for _ in range(N)]
print(int(sum(list(map(lambda x: x*(x-1)/2*1, collections.Counter(s).values())))))

Submission status: Accepted

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

for i in range(N):
    s = str(sorted(input()))
    d[s] += 1
ans = 0

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

Submission status: Accepted

Solution code:
import sys
 
Ans = 0
N = int(input())
S = []
T = []
U = []
for _ in range(N):
  S.append(str(input()))
T = ['a'] * N  
U = ['a'] * N
 
for i in range(N):
  U[i] = ''.join(sorted(S[i]))
 
T = sorted(U)
 
j = 0
while j < N:
  for k in range(1,N):
    if j + k == N - 1:
      if T[j] != T[j + k]:
        Ans = Ans + (k-1) * k / 2
      else:
        Ans = Ans + (k+1) * k / 2
        
      print(int(Ans))
      sys.exit()
      
    if T[j] != T[j + k]:
      Ans = Ans + (k-1) * k / 2
      a = k
      break
  
  j = j + a
  
print(int(Ans))

Submission status: Accepted

Solution code:
import collections

chars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
         "w", "x", "y", "z"]


def eratosthenes_sieve(n):
    primes = [2]
    for odd in range(3, n + 1, 2):
        for p in primes:
            if odd % p == 0:
                break
            if p ** 2 > odd:
                primes.append(odd)
                break
    return primes


era = eratosthenes_sieve(150)
n = int(input())
array = [0] * n
for i in range(n):
    new = input()
    var = 1

    for j in range(10):
        h = chars.index(new[j])
        var *= era[h]

    array[i] = var

c = collections.Counter(array)
cv = list(c.values())
ans = 0
for i in range(len(c)):
    ans += cv[i] * (cv[i] - 1) // 2

print(ans)

Submission status: Accepted

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

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


N = int(input())

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

s.sort()
s.append('0')
ans_tmp = 0
ans = 0
for i in range(N):
    if(s[i]== s[i+1]):
        ans_tmp += 1
    elif(ans_tmp>=1):
        ans+= cmb(ans_tmp+1, 2)
        ans_tmp=0


print(ans)