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())
s=["".join(sorted(input())) for i in range(N)]
s.sort()
X=[1]
for i in range(1,N):
    if s[i-1]==s[i]:
        X[-1]+=1
    else:
        X.append(1)
#print(X)
ans=0
for k in X:
    ans+=(k*(k-1))//2
print(ans)

Submission status: Accepted

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

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

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

ans = 0

c = collections.Counter(s)
for v in c.values():
    ans += (1/2) * v * (v-1)

print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = [input()for _ in range (n)]
dic = {}

for ss in s:
    sss = list(ss)
    
    sss.sort()
    
    ssss = ''.join(sss)


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

  

sum = 0

for aaa in dic.values():
    sum += aaa*(aaa-1)//2
    
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
alphabet = "abcdefghijklmnopqrstuvwxyz"

from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    s = input()
    num = 0
    sorted(alphabet)
    for i,a in enumerate(alphabet):
        if s.count(a) == 10:
            continue #アナグラムはない
        num += s.count(a)*10**i
    d[num] += 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())
ABCS=[[0 for i in range(28)] for j in range(N)]

for i in range(N):
    S=input()
    for j in range(10):
       #print(S[j:j+1],"a")
        ABCS[i][ord(S[j:j+1])-97]=ABCS[i][ord(S[j:j+1])-97]+1
ABCS.sort()
#print(ABCS)
dabu=[None]
ct=1
test=ABCS[0]
for i in range(N):
    if i>0:
        if ABCS[i]==test:
            ct=ct+1
        else:
            dabu.append(ct)
            test=ABCS[i]
            ct=1
    if i==N-1:
        dabu.append(ct)
kumi=0
for x in dabu:
    if x != None:
        kumi=kumi+x*(x-1)/2
    
#print(dabu)
print(int(kumi))

Submission status: Accepted

Solution code:
N = int(input())
s = [''] * N
for _ in range(N):
    tmp = sorted(input())
    s[_] = ''.join(tmp)
tmp = sorted(s)
cnt = 1
index = 0
ansFlag = True
ans1 = 0
while ansFlag:
    cnt = 1
    for j in range(index + 1, N):
        if tmp[index] == tmp[j]:
            cnt += 1
            index += 1
        else:
            index += 1
            break

    tmp2 = 0
    for i in range(cnt, 0, -1):
        tmp1 = i - 1
        tmp2 += tmp1
        ans = tmp2
    ans1 += ans
    if index == (N - 1):
        ansFlag = False

print(ans1)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math

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

N = int(input())
L = defaultdict(int)
ans = 0
for _ in range(N):
    s = sorted(list(input()))
    L[str(s)] += 1

for c in L.values():
    if c > 1:
        ans += combination(c, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
import math

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

N = int(input())
L = defaultdict(int)
ans = 0
for _ in range(N):
    s = sorted(list(input()))
    L[str(s)] += 1

for c in L.values():
    if c > 1:
        ans += combination(c, 2)
print(ans)

Submission status: Accepted

Solution code:
import math

from collections import Counter


def func(i):
    if i >= 1:
        return math.factorial(i)
    else:
        return 1


N = int(input())

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

print(sum([func(i) // func(i-2) // 2 for i in Counter(S).values()]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = []
for i in range(n):
    t = input()
    q = [t[i] for i in range(10)]
    q.sort()
    r = ""
    for i in range(10):
        r += q[i]
    s.append(r)

x = Counter(s).most_common()
ans = 0
for a,b in x:
    ans += b*(b-1)//2

print(ans)

Submission status: Accepted

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

N = int(input())
s = [None]*N
for i in range(N):
    s[i] = ''.join(sorted(input().rstrip('\n')))
s.sort()

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

Submission status: Accepted

Solution code:
import math

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

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

s_list = []
for i in s:
    s_list.append(sorted(i))

s_list = sorted(s_list)

ans = 0
i = 0
while i < n-1:
    count = 0
    while s_list[i] == s_list[i+1]:
        count += 1
        i += 1
        if i == n-1:
            ans += c(count+1)
            break
        elif i+1 <= n-1:
            if s_list[i] != s_list[i+1]:
                ans += c(count+1)
                break
    i += 1

print(ans)

Submission status: Accepted

Solution code:
import math
import copy

n = int(input())
# k, x = [int(i) for i in input().split()]
# table = [[int(i) for i in input().split()] for j in range(n)]
# table = [list(input()) for n in range(h)]
# s = [int(i) for i in input().split()]
# num = [int(i) for i in range(n)]
# s = [input() for i in range(n)]
# s = input()
# list = list(s)
# dp = [[0 for _ in range(2)] for _ in range(13)]
s = [input() for i in range(n)]

for i in range(n):
    moji = list(map(lambda x: x, s[i]))
    moji.sort()
    s[i] = moji

s.sort()

count = 0
i = 0
j = 1
temp = 0
while (j < n):
    if (s[i] == s[j] and j < n - 1):
        j += 1
    elif (s[i] != s[j] and j < n - 1):
        count += (j - i) * (j - i - 1) / 2
        i = j
        j = i + 1
    elif (s[i] == s[j] and j == n - 1):
        count += (j - i) * (j - i + 1) / 2
        i = j
        j = i + 1
    else:
        count += (j - i) * (j - i - 1) / 2
        i = j
        j = i + 1

print(int(count))

Submission status: Accepted

Solution code:
n=int(input())
string=[]
ang=0
charcou=[[0]*26 for i in [1]*n]
for c in range(n):
    string.append(input())
    for d in range(10):
        charcou[c][ord(string[c][d])-97]+=1
charcou.sort()
c=0
while c<n-1:
    comb=1
    while charcou[c]==charcou[c+1]:
        comb+=1
        c+=1
        if c>=n-1:
            break
    if comb!=1:
        ang+=int(comb*(comb-1)/2)
    c+=1
print(ang)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict

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

dd = defaultdict(int)

for i in S:
    p = 0
    for j in i:
        p += 26**(ord(j)-ord('a'))*(i[j])
    dd[p] += 1

ans = 0
a = dd.keys()
for i in a:
    ans += (dd[i]-1)*dd[i]//2
print(ans)

Submission status: Accepted

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

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

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

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

  return result

N = int(input())
s = {}
h = {'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}
for i in range(N):
    total = 1
    chs = list(input())
    for j in range(10):
        total *= h[chs[j]]
    s[total] = s.get(total, 0) + 1

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

print(int(cnt))

Submission status: Accepted

Solution code:
import sys,math,itertools
def r(x):
    sum = 0
    for i in range(1,x+1):
        sum += i
    return sum

N = int(input())
s = []
ans = [0] * N
for i in range(N):
    tmp = input()
    li_tmp = list(tmp)
    s_tmp = sorted(li_tmp)
    st = "".join(s_tmp)
    s.append(st)

data = {}
a = set()
a.add(s[N-1])
for i in range(N-2,-1,-1):
    if s[i] in a:
        if s[i] in data:
            data[s[i]] += 1
        else:
            data[s[i]] = 1 
    else:
        a.add(s[i])
for i in data:
    data[i] = r(data[i])
print(sum(data.values()))

Submission status: Accepted

Solution code:
from operator import itemgetter

n=int(input())
s=[]
li=[]
ans=0
cnt=1

for i in range(n):
  s.append(sorted(input()))
s.sort(key=itemgetter(9,8,7,6,5,4,3,2,1,0))
s=s+['']

for i in range(n):
  if s[i]==s[i+1]:
    cnt+=1
  elif cnt>1:
    li.append(int(cnt*(cnt-1)/2))
    cnt=1
  else:
    cnt=1
print(int(sum(li)))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = [''.join(sorted(input())) for i in range(N)]
count = Counter(a).values()
b = 0
for i in count:
    b = b + i * (i-1)
b=int(b/2)
print(b)

Submission status: Accepted

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

import collections
counters = []
count = 0
for text in texts:
    text_list = list(text)
    text_list.sort()
    text = "".join(text_list) 
    c = collections.Counter(text)
    counters.append(c)



ccccc = []
anss = []
for ccc in counters:
    ccc = str(ccc)
    ccccc.append(ccc)
    

dd = collections.Counter(ccccc)
for v in dd.values():
    ans = v * (v-1) / 2
    anss.append(ans)
    
print(int(sum(anss)))

Submission status: Accepted

Solution code:
import sys
import math

dic = {}

N = int(sys.stdin.readline())

for _ in range(N):
  line = sys.stdin.readline()[0:10]
  key = ''.join(sorted(line))
  dic[key] = dic[key] + 1 if (key in dic) else 1
print(sum(map(lambda x: 0 if x==1 else math.factorial(x) // (math.factorial(x - 2) * 2), dic.values())))

Submission status: Accepted

Solution code:
import sys

N = int(input())
L = {}
count = 0
for i in range(N):
    A = ''.join(sorted(sys.stdin.readline().strip()))
    if A not in L:
        L[A] = 1
    else:
        count += L[A]
        L[A] += 1

print(count)

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())
matched = [False] * N
s = [None] * N
for i in range(N):
    moji = list(input())
    moji.sort()
    tmp = "".join(moji)
    s[i]= tmp

s.sort()
#print(s)
ans = 0
i = 0
j = 1
while i < N-1:
    cnt = 0
    #print("ss",i,j)
    if j == N:
        break
    while j <= N-1:
        #print("aa",i,j)
        if s[i] == s[j]:
            cnt += 1
            j += 1
            continue
        else:

            i = j
            j = i+1
            break
    if cnt >= 1:
        ans += cmb(cnt+1,2)
    

print(ans)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
from collections import defaultdict

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

n = int(input())
d = defaultdict(int)
for i in range(n):
    w = input()
    ws = sorted(w)
    ww = ''.join(ws)
    d[ww] += 1

ans = 0
for i in d.keys():
    if d[i] > 1:
        ans += x(d[i])

print(int(ans))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

    else:
        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


sList = {}

N = int(input())


for i in range(N):
    s = list(input())
    s.sort()

    s_data = ''.join(s)

    if s_data in sList.keys():
        sList[s_data] = sList[s_data] + 1

    else:    
        sList[s_data] = 1

result = 0
for i in sList:
    result += cmb(sList[i],2)

print(str(result))

Submission status: Accepted

Solution code:
import math
n = int(input())
lst = list()
num = 0
flag = 1
for i in range(n):

    s = input()
    s = list(s)
    s.sort()
    lst.append(s)

lst.sort()

for i in range(n-1):
    if (lst[i] == lst[i+1]):
        flag = flag + 1
    else:
        if (flag > 1):
            num = num + math.factorial(flag) // (math.factorial(flag - 2) * 2)
        flag = 1
if (flag > 1):
    num = num + math.factorial(flag) // (math.factorial(flag - 2) * 2)
print(num)

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())
str_dict = {}
for n in range(N):
  l = ''.join(sorted(input()))
  if l in str_dict:
    str_dict[l] += 1
  else:
    str_dict[l] = 1
print(sum([cmb(v,2) if v>= 2 else 0 for v in str_dict.values()]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
def is_anagram(string1, string2):
  for a in string1:
    if a not in string2:
      return False
    string2 = string2.replace(a, "", 1)
  if len(string2) == 0:
    return True
  else:
    return False

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())
anagrams = {}
for i in range(N):
  string = ''.join(sorted(input()))
  if string in anagrams:
    anagrams[string] += 1
  else:
    anagrams[string] = 0

ans = 0
for anagram_num in anagrams.values():
  if anagram_num> 0:
    ans += int(cmb(anagram_num+1,2)) 
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
lists = [str(input()) for i in range(n)]

lists2 = ["".join(sorted(i)) for i in lists]
l = collections.Counter(lists2)
c = l.values()
 
print(int(sum([i * (i - 1)/ 2 for i in c])))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
strings = [str(sorted(Counter(input()).items())) for _ in range(N)]

counts = Counter(strings)
ans = 0
for i in counts.values():
    ans += sum(range(i))

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input());
s = [0]*n;
for i in range(n) :
	s_pre = list(input());
	s_pre.sort();
	s[i] = s_pre;


s.sort(key=lambda x:(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9]));

ans = 0;
ren = 0;
for i in range(1,n) :
	ok = 1;
	if s[i-1]==s[i]:
		ans += 1;
		ans += ren;
		ren += 1;
	else :
		ren = 0;

print(ans);

Submission status: Accepted

Solution code:
#c
n = int(input())
count=0
dic={}
for i in range(n):
    #文字列を追加するか
    b=list(input())
    b.sort()
    mojiretu = ''.join(b)
    if mojiretu not in dic:
        dic[mojiretu] =1
    else:
        dic[mojiretu] +=1
for v in dic.values():
    count += v*(v-1)//2
print(count)

Submission status: Accepted

Solution code:
import collections


N=int(input())
c=0
ans=[]
for n in range(N):
    a=input()
    a=sorted(a)
    a=''.join(a)
    ans.append(a)
c = collections.Counter(ans)
values, counts = zip(*c.most_common())
uu=0
for n in range(len(counts)):
    uu+=(counts[n]*(counts[n]-1))/2
print(round(uu))

Submission status: Accepted

Solution code:
N=int(input())

def listdict(s1):
    ret={}
    rets=""
    for i in s1:
        tmp=ret.get(i,0)
        if tmp==0:
            ret[i]=1
        else:
            ret[i]+=1
    ret_sorted = sorted(ret.items(), key=lambda x:x[0])
    
    for k in range(len(ret_sorted)):
        rets+=str(ret_sorted[k][0])
        rets+=str(ret_sorted[k][1])
    return rets

sN=[listdict(input()) for _ in range(N)]

d1={}
for i in sN:
    tmp=d1.get(i,0)
    if tmp==0:
        d1[i]=1
    else:
        d1[i]+=1
ret=0
for i in d1.values():
    if i ==1:
        continue
    for j in range(1,i):
        ret+=j
print(ret)

Submission status: Accepted

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

res = 0
for x in S.values():
  for y in range(1,x+1):
    res+=y
print(res)

Submission status: Accepted

Solution code:
import collections
N = int(input())
ch = []
for i in range(N):
    ch.append(''.join(sorted(list(input()))))
cnt = 0
ch = sorted(ch)
# print(set(ch))
# print(len(ch)-len(set(ch)))
cnt = collections.Counter(ch)
# se = list(set(ch))
# print("se:"+str(se))
h={}
sum=0
for i in range(N):
    h[i]=sum+i
    sum+=i
sum = 0
for i in cnt.values():
    sum += h[i-1]
print(sum)

Submission status: Accepted

Solution code:
def conv(st):
    res = [0]*26
    for s in st:
        res[ord(s)-ord('a')] += 1
    return ''.join(map(str,res))

N = int(input())
S = {}
res = 0
for i in range(N):
    s = conv(input())
    if s not in S.keys():
        S[s] = 1
    else:
        S[s] += 1
    if S[s] > 1:
        res += S[s]-1
print(res)

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

max = int(input())
i = 0
strings = []
while True:
  if i == max:
    break
  strings.append(input())
  i+=1

#print(strings) # ['acornistnt', 'peanutbomb', 'constraint']

strings2 = [list(str) for str in strings]
[str.sort() for str in strings2] # 配列の中身を副作用でソートする
# print(strings2)
strs3 = [''.join(str) for str in strings2] # ソートした文字のリストを文字列に戻す

#print(strs3)

c = collections.Counter(strs3)
# print(c)
counts = list(c.values()) # 組み合わせの数のリスト
counts2 = [ c for c in counts if c > 1] # 2個以上の重複がないものは除外
#print(counts2)

# nC2 の組み合わせを計算して、合計する
print(sum([int(cmb(n, 2)) for n in counts2]))

Submission status: Accepted

Solution code:
import string
import collections

N = int(input())

CountList = []

atoz = string.ascii_lowercase
for i in range(N):
    temp = ''
    s=input()
    CountList.append(''.join(sorted(s)))

str = collections.Counter(CountList)

count = 0
for s in str:
    count += sum(range(str[s]))

print(count)

Submission status: Accepted

Solution code:
N = int(input())
dicts = {}
for _ in range(N):
    s = "".join(sorted((input())))
    if s not in dicts:
        dicts[s] = 1
    else:
        dicts[s] += 1
print(sum([n*(n-1)//2 for n in list(dicts.values()) if 0<n]))
print("")

Submission status: Accepted

Solution code:
N = int(input())

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

result = 0
for key in V.keys():
	result += ((V[key]-1)*(1+V[key]-1))//2

print(result)

Submission status: Accepted

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

input = sys.stdin.readline


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


def main():
    n = int(input())
    lines = ["".join(sorted(input().strip())) for _ in range(n)]
    count = 0
    for _, v in Counter(lines).most_common():
        if v == 1:
            break
        count += cmb(v, 2)
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

tmp = []
counter = 0
for i in range(N):
    new_str = ''.join(sorted(str(input())))
    
    #if i != 0:
    #    counter += tmp.count(new_str)
    tmp.append(new_str)

import collections
c = collections.Counter(tmp)

#new_str_list = [i for i in new_str = ''.join(list(sorted(str(input()))))]
#print(new_str_list)

    #for j in range(i):
    #    if str(new_str) == str(tmp[j]):
    #        counter += 1
value = list(c.values())
#print(value)

for j in value:
    for k in range(j):
        counter += k

print(counter)

Submission status: Accepted

Solution code:
#coding: utf-8
amount = int(input())
strings = list(map(str, [input() for i in range(amount)]))
sum = 0
sum2 = 0

for i in range(len(strings)):
	# print(strings[i])
	abc = list(strings[i])
	abc.sort()
	strings[i] = ''.join(abc)
	# print(strings[i])

strings.sort()

for j in range(len(strings)-1):
	# print(j+1)
	# print(strings[j]+strings[j+1])
	if strings[j] == strings[j+1]:
		sum2 = sum2+1
	else:
		sum = sum + sum2 * (sum2+1) / 2
		sum2 = 0

sum = sum + sum2 * (sum2+1) / 2
print(int(sum))

Submission status: Accepted

Solution code:
import collections
import math

N = int(input())
b =[]
for i in range(N):
    a = input()
    b.append([j for j in a])
    b[i].sort()
a = [''.join(j) for j in b]
b = [k for k in collections.Counter(a).values()]
c = 0
for x in range(len(b)):
    if b[x] > 1:
        c += math.factorial(b[x]) // (math.factorial(b[x] - 2) * math.factorial(2))
    else:
        None
print(c)

Submission status: Accepted

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

sdict = [{c:s.count(c) for c in s} for s in S]
c = [ "".join( [c+str(sd[c]) for c in 'abcdefghijklmnopqrstuvwxyz' if c in sd] ) for sd in sdict ]

sdictc = {}
maxSdictc = 0
for cc in c:
    #print(cc)
    if cc in sdictc:
        maxSdictc += sdictc[cc]
        sdictc[cc] += 1
    else:
        sdictc[cc] = 1

print(maxSdictc)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input() for _ in range(n))

t=['']*n

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

u=sorted(t)

cnt=0

#for i in range(n-1):    
#	for j in range(i+1,n):
#		if u[i]!=u[j]:
#			break
#		else:
#			cnt+=1

cnt_a=0
cnt_b=0

u.append(['0'])

#print(u)


for i in range(n):
	if u[i+1]==u[i]:
		cnt_a+=1
		#print(cnt_a)
	else:
		if cnt_a>0:
			cnt_b+=(cnt_a+1)*cnt_a//2
			#print(cnt_b)
			cnt_a=0

print(cnt_b)

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
 
from collections import Counter
 
nS = []
for s in S:
    s  = ''.join(sorted(s))
    nS.append(s)

c = Counter(nS)
 
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

acc = 0
for cnt in list(c.values()):
    if cnt!=1:
        acc+=cmb(cnt, 2)
    
print(acc)

Submission status: Accepted

Solution code:
import collections as c
import math

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

def C(n, r):
  return P(n, r)//math.factorial(r)

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

for i in s:
  count.append(''.join(sorted(list(i))))

# print(count)

comb = [i[1] for i in c.Counter(count).most_common()]
print(sum([C(i, 2) for i in comb if i >= 2]))

Submission status: Accepted

Solution code:
n = int(input())
s = []
count = 0
for i in range(n):
    s1 = list(input())
    s1.sort()
    s1 = "".join(s1)
    s.append(s1)
s.sort()
s2 = list(set(s))
s2.sort()
if len(s) == len(s2):
    print("0")
else:
    j1 = 0
    j2 = 0
    count1 = 0
    while j1 < n and j2 < len(s2):
        if s[j1] == s2[j2]:
            count1 = count1 + 1
            j1 = j1 + 1
        else:
            count = count + int(count1*(count1-1)/2)
            j2 = j2 + 1
            count1 = 0
    count = count + int(count1*(count1-1)/2)
    print(count)

Submission status: Accepted

Solution code:
import collections
import math

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

N = int(input())
s = [""]*N
S = []
for i in range(N):
    s[i] = input()
    test = list(s[i])
    test.sort()
    S.append("".join(test))
ana = sorted(collections.Counter(S).values())
count = 0
for i in ana:
    if i >= 2:
        count += combinations_count(i,2)
print(count)

Submission status: Accepted

Solution code:
import collections as cl
import itertools as it
N = int(input())
s = []
for i in range(N):
    ss = input()
    s.append(ss)
#print(s)
t = sorted(s)
t= []
for i in range(N):
    tt= sorted(s[i])
    ts = ",".join(tt)
    t.append(ts)
#    print(ts)
#print(t)
l = cl.Counter(t)
#print(l)
count = 0
for v in l.values():
    if v > 1:
        count = count + v*(v-1)/2
#    print(v)
print(int(count))

Submission status: Accepted

Solution code:
def norm_str(s):
  ls = list(s)
  return "".join(sorted(ls))
  
ans = 0
pool = {}
n = int(input())
for i in range(n):
  s = input()
  norm = norm_str(s)
  
  if norm in pool:
    pool[norm] += 1
    ans += pool[norm]
    
  else:
    pool[norm] = 0
  
print(ans)

Submission status: Accepted

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

s.sort()

ans = 0
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)

Submission status: Accepted

Solution code:
import collections
import math


n = int(input())

inputlist = []

for i in range(n):
    inputlist.append("".join(sorted(list(input()))))

counter = collections.Counter(inputlist)

counter = dict(counter)
valuelist = list(map(int, counter.values()))

ans = 0

for i in valuelist:
    if i == 1:
        continue
    if i == 2:
        ans += 1
        continue
    temp = math.factorial(i)
    temp /= math.factorial(i-2)
    temp = int(temp / 2)
    ans += temp

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
from itertools import combinations

N = int(input())
c = ["".join(sorted(str(input()))) for x in range(N)]
cs = Counter(c)

ans=0
for i in cs.values():
    if i!=1:
        ans += (i * (i-1))/2

print(int(ans))

Submission status: Accepted

Solution code:
# K,X = list(map(int, input().split()))

N = int(input())

alphabet = "abcdefghijklmnopqrstuvwxyz"

ans_dic = {}

for i in range(N):
    word = input()
    tmp = 0
    for s in word:
        tmp += 10**alphabet.index(s)

    if tmp in ans_dic:
        ans_dic[tmp] += 1
    else:
        ans_dic[tmp] = 1
ans = 0
for k, v in ans_dic.items():
    ans += v * (v-1) // 2
print(ans)

Submission status: Accepted

Solution code:
def f(x):
    return 10 ** (ord(x) - ord('a') + 1)


import math


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


from collections import Counter

N = int(input())
counter = Counter()
for i in range(N):
    counter[sum([f(i) for i in input()])] += 1
res = 0
for v in counter.values():
    if v > 1:
        res += combinations_count(v, 2)
print(res)

Submission status: Accepted

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

def helper(s):
  d = {}
  for c in s:
    if c not in d:
      d[c] = 0
    d[c] += 1
  
  return d

def pairs(n):
    return int(n * (n - 1) / 2)

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

ans = sum([pairs(v) for v in d.values() if v > 1])
  
print(ans)

Submission status: Accepted

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

Dic = [1]*N
prime = {"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}

from operator import mul
from functools import reduce

def comb(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


for i in range(N):
    for s in S[i]:
        Dic[i]*= prime[s]

Dic.sort()
pre = Dic[0]
ans = 0
temp = 1
for d in Dic[1:]:
    if pre == d:
        temp += 1
    else:
        if temp >= 2:
            ans += comb(temp,2)
        pre = d
        temp = 1

ans += comb(temp,2) if temp >= 2 else 0
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())

result = 0
word_list = {}

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

print(result)

Submission status: Accepted

Solution code:
import sys
import collections

n = int(input())
lst = [None] * n
for i in range(n):
    lst[i] = "".join(sorted(list(sys.stdin.readline().strip())))

ans = 0
for v in collections.Counter(lst).values():
    ans += v * (v - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

m = {}

def sort_str(s):
    return "".join(sorted(list(s)))

for i in range(N):
    s = str(input())
    ss = sort_str(s)

    if ss in m:
        m[ss] += 1
    else:
        m[ss] = 1


count = 0

for c in m.values():
    if c > 1:
        count += cmb(c, 2)

print(count)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from collections import defaultdict

N=input()

C=defaultdict(lambda: 0) 
for i in range(N):
	s=sys.stdin.readline().strip()
	s=list(s)
	s.sort()
	s="".join(s)
	C[s]+=1
ans=0

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

print ans