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.

Solution (Python):
n = int(input())
li = [list(str(input())) for i in range(n)]

for i in range(n):
    li[i].sort()

li.sort()

now = 0
count = 0
res = 0
for i in range(1,n):
    if li[now] == li[i] and i == n-1:
        count += 1
        res += count*(count+1)/2
    elif li[now] == li[i]:
        count += 1
    else:
        now = i
        res += count*(count+1)/2
        count = 0

print(int(res))

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

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

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

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

Solution (Python):
import collections

n = int(input())

listAll = []

ans = 0

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

	slist = list(s)

	

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

	listAll.append(sString)

c = collections.Counter(listAll)

for mykey in c.keys():
	
	
	n = c[mykey]
	ans += n * (n-1) /2


print(int(ans))

Solution (Python):
import collections
from math import factorial

from operator import mul
from functools import reduce

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



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

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

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

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

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
s2 = [0 for _ in s]

s2 = {}

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

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

print(pair)

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

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

s.sort()

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

print(int(ans))

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


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

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

Solution (Python):
from itertools import groupby

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

Solution (Python):
import collections
import math

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

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

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

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

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

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

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

    return result


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

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

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

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

if c != 0:
    res += c

print(res)

Solution (Python):
import collections

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

s_dict = dict()

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

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

print(count)

Solution (Python):
n = int(input())

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

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

print(ans)

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

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

s.sort()

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

print(z)

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

Solution (Python):
import sys

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

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

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

    print(count)

if __name__=='__main__':
    main()

Solution (Python):
import collections

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


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


c = collections.Counter(s)

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

print(int(count))

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

sentences = [None]*N

dp = {}

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

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

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

    return tree


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

    return tree

tree = createTree(sentences)

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

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

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

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

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

print(count)

Solution (Python):
import collections

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

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

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

li.sort()

a = 1
ans = 0

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

print(int(ans))

Solution (Python):
from collections import Counter

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

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

print(ans)

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

            






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



s=[i for i in range(n)]

ans=0
f(0,s)

print(ans)

Solution (Python):


import math

N = int(input())

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


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


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

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

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

print(int(count))

Solution (Python):
import math

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

for _ in range(N):
    S.append(input())

def create_hash(_str):
    hash = ""
    for i, a in enumerate(["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"]):
        count = 0
        for s in _str:
            if s == a:
                count += 1
        hash += str(count)
    return hash

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

hist = {}
for s in S:
    hash = create_hash(s)
    hist[hash] = hist.get(hash, 0) + 1

pairs = 0
for k, v in hist.items():
    if v > 1:
        pairs += combinations(v, 2)
print(pairs)

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

for i, s_i in enumerate(s):
    s_l = list(s_i)
    s_l.sort()
    a = ""
    for c in s_l:
        a += c
    s[i] = a

cnt = 0
count_dict = {}
for j in range(N):
    count_dict[s[j]] = 0
for k in range(N):
    count_dict[s[k]] += 1

for v in count_dict.values():
    if v >= 2:
        
        cnt += cmb(v, 2)

print(cnt)

Solution (Python):
N = int(input())
S = []

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

S.sort()
count = 0
sw = 0
series = []
for i in range(1, len(S)):
    if S[i-1]==S[i]:
        sw = 1
        count += 1
    else:
        if sw==1:
            count += 1
            series.append(count)
            count = 0
            sw = 0
if sw==1:
    count += 1
series.append(count)

count = int(sum([c * (c-1)/2 for c in series]))

print(count)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
li = []
ct = 0
su = 0
alpha2num = lambda c: ord(c) - ord('a') + 1
for i in range(n):
    su = 0
    li.append("".join(sorted(input())))

lll= 1
list.sort(li)
li.append("a")
for i in range(n):

    if li[i] == li[i+1]:
        lll += 1
    else:
        if lll != 1:
            for j in range(1, lll):
                ct += j
        lll = 1
print(ct)

Solution (Python):
import math
N = int(input())
S = [input() for i in range(N)]

def soort(s):
    new_str_list = sorted(s)
    new_str = ''.join(new_str_list)
    return new_str

A = []
for i in range(N):
    A.append(soort(S[i]))
B = {}  
for i in A:
    B[i] = 0

for i in A:
    B[i] +=1
def combinations_count(n):
    return math.factorial(n)//(math.factorial(n-2)*2)
sum = 0
for i in B.values():
    if i == 1:
        sum +=0
    else:
        sum += combinations_count(i)
  
print(sum)

Solution (Python):
def alphas(key):
    value = {}
    for char in key:
        if value.get(char) is None:
            value[char] = 1
        else:
            value[char] += 1
    return value

def main():
    N = int(input())
    anagrams = []
    for i in range(N):
        s = input()
        anagrams.append(s)
    ans = 0
    dict = {}
    for key in anagrams:
        value = alphas(key)
        dict_key = str(sorted(value.items()))
        if dict.get(dict_key) is None:
            dict[dict_key] = 1
        else:
            dict[dict_key] += 1
        """
        for json_key in json.keys():
            if json[json_key] == value:
                ans += 1
                continue
        json[key] = value
        """
    for value in dict.values():
        ans += value * (value - 1) / 2
    print(int(ans))

if __name__ == '__main__':
    main()

Solution (Python):
import collections

n = int(input())
s = []
for i in range(n):
  s_ = sorted(input())
  s.append(s_[0]+s_[1]+s_[2]+s_[3]+s_[4]+s_[5]+s_[6]+s_[7]+s_[8]+s_[9])

c = collections.Counter(s)
count = 0

for i in c:
  if c[i] != 1:
    count += c[i]*(c[i]-1)/2

print(int(count))

Solution (Python):
import collections
n = int(input())
s = []
ans = 0
for i in range(n):
    s_str = list(input())
    s_str.sort()
    u="".join(s_str)
    s+=[u]
c = collections.Counter(s)
for i in c.values():
    if i > 1:
        p=0
        for o in range(i):
            p += o
        ans+=p
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    s = []
    for i in range(N):
        s.append("".join(sorted(list(input().strip()))))
    s = sorted(s)
    
    tmp = []
    pre = s[0]
    count = 1
    for i in range(1, N):
        if s[i] == pre:
            count += 1
        else:
            if count > 1:
                tmp.append(count)
            count = 1
        if i == N-1:
            if count > 1:
                tmp.append(count)
        pre = s[i]
    ans = 0
    for n in tmp:
        ans += (n * (n-1))//2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
import math

N = int(input())
s = [str(sorted(input())) for _ in range(N)]
rl_s = defaultdict(int)
for _s in s:
  rl_s[_s] += 1

c = 0
for v in rl_s.values():
  c += sum([i for i in range(v)])
print( c )

Solution (Python):
import collections
from scipy.misc import comb
n = int(input())
l = []
for i in range(n):
    s = input()
    s = ''.join(sorted(s))
    l.append(s)
c = collections.Counter(l)
r = 0
for i in list(c.values()):
    if i > 1:
        r += comb(i, 2, exact=True)
print(r)

Solution (Python):
import sys
p = [r for r in sys.stdin.read().split('\n') if r != '']
n = int(p[0])
s = p[1:]
for i, r in enumerate(s):
    t = list(r)
    t.sort()
    s[i] = ''.join(t)
d = {}
for i in s:
    if i in d:
        d[i] += 1
    else:
        d[i] = 0
count = 0
for k, v in d.items():
    count += int((v + 1) * v / 2)
print(count)

Solution (Python):
import collections
N = int(input())
l = []
ans = 0

for i in range(N):
    l.append("".join(sorted(input())))

for v in (collections.Counter(l)).values():
    v -= 1
    ans += int(0.5*v*(v+1))

print(ans)

Solution (Python):
letters = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
n = int(input())
words = {}
total = 0
hashv = 1
for i in range(n):
    s = input()
    for l in s:
        hashv = hashv*primes[letters[l] - 1]
    if hashv not in words.keys():
        words[hashv] = 1
    else:
        words[hashv] += 1
    hashv = 1

for key in words:
    if words[key] != 1:
        total += int(words[key] * (words[key]-1)/2)
    
print(total)

Solution (Python):
from fractions import Fraction
import math
n = int(input())
candidate = [sorted(list(input())) for _ in range(n)]
ans = 0
candidate.sort()





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














cnt = []
num = 1
for i in range(1, n):
    if candidate[i] == candidate[i - 1]:
        num += 1
        if i == n - 1 and num > 1:
            cnt.append(num)
    else:
        if num > 1:
            cnt.append(num)
        num = 1
for i in cnt:
    if i > 1:
        ans += combinations_count(i, 2)
print(ans)

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

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

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

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

    return result



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

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

s = sorted(s)


ans = 0
count = 1

for i in range(1,N):
    if s[i-1] == s[i]:
         count += 1
    else:
        if count >= 2:
            ans += cmb(count,2)
            count = 1

ans += cmb(count,2)

print(ans)

Solution (Python):
N = int(input())
grid = []
for i in range(N):
    array = list(sorted(input()))
    grid.append(array)
sss = []
for i in range(N):
  sss.append(','.join(grid[i]))
import collections
aaa = collections.Counter(sss).most_common()
var = 0
for i in range(len(aaa)):
  y = aaa[i][1]
  var = var + y*(y-1)/2
print(int(var))

Solution (Python):
from operator import mul
from functools import reduce
import collections


def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    elif r < 0:
        return 0
    numer = reduce(mul, range(n, n - r, -1))
    denom = reduce(mul, range(1, r + 1))
    return numer // denom


N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
c = collections.Counter(s)
ans = 0
for si in set(s):
    n = c[si]
    ans += cmb(n, 2)
print(ans)

Solution (Python):
import math

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

std=[]
for i in range(n):
    std.append(sorted(s[i]))
stdx=[]
stdx=sorted(std)

cnt=0
k=1
for i in range(n-1):
    if stdx[i]==stdx[i+1]:
        k+=1
        if k>1 and i==n-2:
            cnt+=math.factorial(k)//math.factorial(k-2)//math.factorial(2)
    else:
        if k>1:
            cnt+=math.factorial(k)//math.factorial(k-2)//math.factorial(2)
            k=1

print(cnt)

Solution (Python):


n = int(input())

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

s.sort()
num = 0
i = 0
while i < n - 1:
    if s[i] == s[i+1]:
        m = 1
        i += 1
        while i < n - 1 and s[i] == s[i+1]:
            m += 1
            i += 1
        if m == 1:
            num += m
        else:
            num += int(m*(m+1)/2)
    i += 1
            

print(num)

Solution (Python):



def modc(a, b, m):
    c = 1
    for i in range(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1, m) % m
    return c


def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)


def modinv(a, m):
    (inv, q, gcd_val) = egcd(a, m)
    return inv % m

N = int(input())

S = [''.join(sorted(input())) for _ in range(N)]
D = {}
for s in S :
    if s in D :
        D[s] += 1
    else :
        D[s] = 1
V = list(D.values())

count = 0
for v in V :
    if v == 1 :
        count += 0
    elif v == 2 :
        count += 1
    elif v >= 3 :
        count += modc(v, 2, 2 ** 63 - 1)
print(count)

Solution (Python):

from collections import defaultdict
D = defaultdict(int, {})
n = int(input())
for _ in range(n):
    s = ''.join(sorted(input()))
    D[s] += 1
print(sum([sum(range(i)) for i in D.values()]))

Solution (Python):
from collections import Counter
n=int(input())
strings = []
for i in range(n):
    strings.append(input())
dic = {}
for string in strings:
    c = Counter(string)
    s = "".join([str(c[l]) for l in "abcdefghijklmnopqrstuvwxyz"])
    if s in dic:
        dic[s] += 1
    else:
        dic[s] = 0
res = 0
for k , v in dic.items():
    res += v * (v + 1)/2
print(int(res))

Solution (Python):
N = int(input())
s = [input().split() for i in range(N)]
for i in range(N):
    s[i] = list(s[i][0])
    s[i].sort()
    s[i] = ''.join(map(str, s[i]))
s.sort()
count = 0
ren = 0
for i in range(N-1):
    if s[i] == s[i+1]:
        count += 1+ren
        ren += 1
    else:
        ren = 0
print(count)

Solution (Python):
n = int(input())
ans = 0

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

k = 0

for i in range(n-1):
    if k > 1:
        k = k-1
        ans += k
    else:
        k = 0
        a = A[i]
        for j in range(n-i-1):
                if a == A[j+i+1]:
                    ans += 1
                    k += 1
                else:
                    break

print(ans)

Solution (Python):
import collections
import sys
input = sys.stdin.readline

N = int(input())

list =  [''.join(sorted(list(input().strip("\n")))) for i in range(N)]

list.sort()

c = collections.Counter(list)
c = c.most_common()
ans = 0
for i,v in c:
    ans += v*(v-1)//2

print(ans)

Solution (Python):
from collections import Counter
import json

N = int(input())
ss = []
c = []

for _ in range(N):
    s = input()
    ss.append("".join(sorted(s)))

sum = 0

for i , v in Counter(ss).items():
    sum += (v*(v-1)//2)
print(sum)

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re

from scipy.misc import comb





def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e30)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    N = i_inpl()
    s = defaultdict(int)
    ans = 0
    for _ in range(N):
        si = tuple(sorted(input()))
        s[si] = s[si] +1

    for i in s.values():
        if i > 1:
            ans += 1/2 * (i-1)*i
    print(int(ans))
if __name__ == "__main__":
    main()

Solution (Python):
import math
from operator import mul
from functools import reduce
N = int(input())
ll = []
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

for i in range(N):
    l = list(input())
    l.sort()
    s = ''.join(l)
    ll.append(s)
ll.sort()
k = 0
ans = 0
for j in range(N-1):
    if ll[j]==ll[j+1]:
        k += 1
    elif k>0:
        ans += cmb(k+1, 2)
        k = 0
if k>0:
    ans += cmb(k+1, 2)
print(int(ans))

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

dic = {}
for s in S:
    if s in dic.keys():
        dic[s] += 1
    else:
        dic[s] = 1

count = 0
for k in dic.keys():
    c = dic[k]
    count += (c * (c - 1)) // 2
print(count)

Solution (Python):



n = int(input())
d = {}
arr = []
for _ in range(n):
    x = "".join(list(map(str, list(sorted(input())))))
    arr.append(x)
    if x not in d:
        d[x] = 1
    else:
        d[x] += 1

c = 0
for a in arr[:0:-1]:
    if d[a] > 0:
        c += d[a] - 1
        d[a] -= 1

print(c)

Solution (Python):
n=int(input())
sl=list([input() for i in range(n)])
def sort_str(s):
    cl = list(s)
    cl.sort()
    return ''.join(cl)
m={}
sum=0
for i in range(n):
    s = sort_str(sl[i])
    if not s in m:
        m[s]=0
    else:
        m[s]+=1
        sum+=m[s]
print(sum)

Solution (Python):
import collections
import sys
input = sys.stdin.readline

N = int(input())

list =  [''.join(sorted(list(input().strip("\n")))) for i in range(N)]

list.sort()

c = collections.Counter(list)
c = c.most_common()
ans = 0
for i,v in c:
    ans += v*(v-1)//2

print(ans)

Solution (Python):
from collections import Counter
N=int(input())
L=[''.join(sorted(list(input()))) for i in range(N)]
S=Counter(L)
ans=0
for i in S.values():
    ans+=(i-1)*i//2
print(ans)

Solution (Python):
import collections

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

collect_s = collections.Counter(s)
count = 0
for x in collect_s.values():
    count += x * (x - 1) / 2
print(int(count))

Solution (Python):
n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(input())))
s.sort()
count = 1
counts = []
now = s[0]
for i in range(1, n):
    if s[i] == now:
        count += 1
    else:
        if count > 1:
            counts.append(count)
        count = 1
        now = s[i]
if count > 1:
    counts.append(count)
c = 0
for i in counts:
    c += i * (i-1) // 2
print(c)

Solution (Python):
from collections import Counter  
n=int(input())
val=0
l=[]
for i in range(n):
    s=input()
    s=sorted(s)
    l.append("".join(s))
for i in Counter(l).values():
    val+=(i*(i-1))//2
print(val)

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