Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

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

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

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

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

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

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

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



Sample input:
3
acornistnt
peanutbomb
constraint


Sample output:
1

Submission status: Accepted

Solution code:
def 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 += int(factorial(v)) / int(factorial(2)) / int(factorial(v - 2))
        cnt += cmb(v, 2)

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

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

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))
# def comb(n, k):

#     if n < 0 or k < 0:
#         pdt = 0

#     else:
#         pdt = 1
#         for s in range(1, k + 1):
#             pdt *= Fraction(n - s + 1, s)

#     # 戻り値をint型にするために分子を取り出す
#     return pdt.numerator


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)

Submission status: Accepted

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

# print(cmb(1,2))

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

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

s = sorted(s)

# print(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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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)

Submission status: Accepted

Solution code:
#import sys
# import math

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())
#A, B = list(map(int,input().split()))
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
# o(10**5 * 10**2 = 10**7)
list =  [''.join(sorted(list(input().strip("\n")))) for i in range(N)]
# o(10**5 * log(10**5) = 10**7)
list.sort()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
from scipy.misc import comb

# 再帰がやばいとき
# import sys
# sys.setrecursionlimit(10**9)

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 # 10^9 + 7

# field[H][W]
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# じゃあ全部保存していってやるよ
# atcoderのpython3のバージョンが古すぎて(3.4.3)TypeError: unhashable type: 'list'なるのでため息つきながら直した

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        n = int(lines[0])
        for i in range(n):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        n = int(lines[0])

    s = lines[1:]

    return (n, s)


def cnt_dict_to_tuple(cnt_dict):
    t = (
        cnt_dict.get("a", 0),
        cnt_dict.get("b", 0),
        cnt_dict.get("c", 0),
        cnt_dict.get("d", 0),
        cnt_dict.get("e", 0),
        cnt_dict.get("f", 0),
        cnt_dict.get("g", 0),
        cnt_dict.get("h", 0),
        cnt_dict.get("i", 0),
        cnt_dict.get("j", 0),
        cnt_dict.get("k", 0),
        cnt_dict.get("l", 0),
        cnt_dict.get("m", 0),
        cnt_dict.get("n", 0),
        cnt_dict.get("o", 0),
        cnt_dict.get("p", 0),
        cnt_dict.get("q", 0),
        cnt_dict.get("r", 0),
        cnt_dict.get("s", 0),
        cnt_dict.get("t", 0),
        cnt_dict.get("u", 0),
        cnt_dict.get("v", 0),
        cnt_dict.get("w", 0),
        cnt_dict.get("x", 0),
        cnt_dict.get("y", 0),
        cnt_dict.get("z", 0),
    )
    return t



def solve(n, s):

    cnt_tuple_dict = dict()
    for i in range(n):
        cnt_dict = dict()    
        for c in s[i]:
            cnt = cnt_dict.get(c, 0)
            cnt = cnt + 1
            cnt_dict[c] = cnt
    
        t = cnt_dict_to_tuple(cnt_dict)
        if debug:
            log("t=%s" % str(t))

        same_cnt_index_list = cnt_tuple_dict.get(t, [])
        same_cnt_index_list.append(i)
        cnt_tuple_dict[t] = same_cnt_index_list


    result = 0
    for t, same_cnt_index_list in cnt_tuple_dict.items():
        n = len(same_cnt_index_list)

        result = result + int(n*(n-1)/2)

    return result


def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
N = int(input())
s = [0]*(N+1)
for i in range(N):
    tmp = sorted(list(input()))
    s[i] = ''.join(tmp)
s[-1] = 'zzzzzzzzzzz'
s.sort()

ans = 0
i = 0
while i < N:
    k = 1
    while s[i] == s[i+k]:
        k += 1
    ans += (k-1)*k/2
    i += k
        
print(int(ans))

Submission status: Accepted

Solution code:
import math
N = int(input())
sorted_map = {}

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

for i in range(N):
    inserted_flag = True
    input_sorted = ''.join(sorted(input()))
    if input_sorted in sorted_map.keys():
        sorted_map[input_sorted] += 1
        inserted_flag = False
    if inserted_flag:
        sorted_map[input_sorted] = 1

count = 0
for count_num in sorted_map.values():
    if count_num == 1:
        continue
    count += combinations_count(count_num, 2)
print(count)

Submission status: Accepted

Solution code:

def combain(X, Y):
    return int(( X * (X - 1)) / Y)

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

ans = 0

for i in range(N):
    S.append(sorted(input()))

S.sort()

ans = 0

X = 1
for i in range(N-1):
    if S[i] == S[i+1]:
        X += 1
    elif X > 1:
        C = combain(X, 2)
        X = 1
        ans += C

if X > 1:
    C = combain(X, 2)
    X = 1
    ans += C
    
print(ans)

Submission status: Accepted

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

c=[]
i=0
while i<n-1:
  cnt=0
  while i<n-1 and s[i]==s[i+1]:
    cnt+=1
    i+=1
  c+=[cnt+1]
  i+=1
ans=0
for i in c:
  if i>1:
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

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

for n in range( N ):
    s = input()
    s1 = list( s )     
    s1.sort()
    s2 = ''.join(s1)

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

total = 0
for n in dic.values():
    if n >= 2:
        num = n * ( n - 1 ) // 2
        total += num

print( total )

Submission status: Accepted

Solution code:
n = int(input())
r = dict()
find = False
for i in range(n):
    l = str(sorted([c for c in input()]))
    if l not in  r:
        r[l] = 1
    else:
        r[l] = r[l] + 1
        find=True

sum = 0    
def comb(n, k):
    bo = 1
    for kk in range(1, k+1):
        bo = bo * kk
    si = 1
    for kk in range(n-k+1 ,n+1):
        si = si * kk
    return int(si / bo)

for k, v in r.items():
    sum = sum + comb(v, 2)
print(sum)

Submission status: Accepted

Solution code:
n=int(input())
s=["".join(sorted(list(input()))) for i in range(n)]
s.sort()
dp=[1]
for i in range(n-1):
  if s[i]==s[i+1]:
    dp[len(dp)-1]+=1
  else:
    dp.append(1)
ans=0
for i in dp:
  ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
m = {}
 
for i in range(n):
  s = "".join(sorted(list(input())))
  if s in m.keys():
    m[s] = m[s] + 1
  else:
    m[s] = 1
 
ans = 0
for s in m.keys():
  ans += m[s] * (m[s] - 1) // 2
 
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import collections
import math

l = int(input())
st = []
for i in range(l):
    x = ''.join(sorted(input()))
    st.append(x)
c = collections.Counter(st)
cv = c.values()
sum = 0
for i in cv:
    if int(i) > 1:
        tm = 0
        for j in range(int(i)-1):
            tm = tm + int(i)-1-j
        sum = sum + tm

print(math.floor(sum))

Submission status: Accepted

Solution code:
import math

N = int(input())
S = []
for i in range(N):
    S_tmp = str(input())
    S_tmp = list(S_tmp)
    S_tmp.sort()
    S.append("".join(S_tmp))
S.sort()

count=0
x = 0
tmp = S[0]
count_all = []
for i in S[1:]:
    if tmp==i:
        count+=1
    else:
        if count>0:
            x += math.factorial(count+1)//(math.factorial(count+1-2) * math.factorial(2))
        count = 0
    tmp = i
if count>0:
    x += math.factorial(count+1)//(math.factorial(count+1-2) * math.factorial(2))
        
    tmp = i
print(x)

Submission status: Accepted

Solution code:
# 3.4.3

import re
from collections import deque
from functools import reduce
from itertools import permutations
from math import pi
from operator import itemgetter
from operator import mul
from operator import xor
from os import linesep
from queue import PriorityQueue
from sys import stdin


def rline() -> str:
    return stdin.readline().strip()


def rlines(hint: int = 1):
    ret = ['' for i in range(hint)]
    for i in range(hint):
        ret[i] = rline()
    return ret


def htokens(hint: int = 1):
    lns = rlines(hint)
    ret = list(map(lambda ln: ln.split(), lns))
    # if return value has one and only one element(list),
    if hint == 1:
        # then add an empty list.
        ret.append([])
    return ret


def vtokens(hint: int = 1):
    m, _ = htokens()
    wint = len(m)
    ret = [[None] * hint for i in range(wint)]
    for y in range(hint):
        if y != 0:
            m, _ = htokens()
        # convert horizontal to vertical
        x = 0
        for v in m:
            ret[x][y] = v
            x += 1
    # if return value has one and only one element(list),
    if wint == 1:
        # then add an empty list.
        ret.append([])
    return ret


def rint(radix: int = 10) -> int:
    return int(rline(), radix)


def hints(hint: int = 1, radix: int = 10):
    ret = htokens(hint)
    for i in range(len(ret)):
        ret[i] = list(map(lambda v: int(v, radix), ret[i]))
    return ret


def vints(hint: int = 1, radix: int = 10):
    ret = vtokens(hint)
    for i in range(len(ret)):
        ret[i] = list(map(lambda v: int(v, radix), ret[i]))
    return ret


def mat(hint: int, wint: int):
    return [[None]*wint for i in range(hint)]


def filllist(lst, value: int) -> None:
    # destructive.
    for i in range(len(lst)):
        lst[i] = value


def isprime(n: int) -> bool:
    if n <= 1:
        return False
    if n in (2, 3, 5):
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False
    lst = int(n**0.5)
    f = 5
    while f <= lst:
        if n % f == 0:
            return False
        if n % (f + 2) == 0:
            return False
        f += 6
    return True


def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str:
    return lnsep.join(map(lambda row: fieldsep.join(map(str, row)), matrix))


def strbool(boolval: int) -> str:
    return ['No', 'Yes'][boolval]


def solve() -> None:
    # read here
    n = rint()
    ss = rlines(n)

    # solve here
    dict = {}
    for s in ss:
        cnt = [0] * 26
        for c in s:
            cnt[ord(c) - ord('a')] += 1
        tup = tuple(i for i in cnt)
        if not tup in dict.keys():
            dict[tup] = 0
        dict[tup] += 1

    # 2-combination of x.
    ans = sum(map(lambda x: 0 if x == 1 else x*(x-1)//2, dict.values()))

    # print here
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

s = []
s_ap = s.append

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

result = 0

for i in Counter(s).values():
    if not i == 1:
        result += int(i * (i - 1) / 2)
        
print(result)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
S = [input() for i in range(N)]
ans = defaultdict(list)
for s in S:
    count = [0] * 26
    for c in s:
        count[ord(c)-ord('a')] += 1
    ans[tuple(count)].append(s)

ret = 0
l = 0
for a in ans.keys():
    l = len(ans[a])
    ret += l*(l-1)//2

print(ret)

Submission status: Accepted

Solution code:
N = int(input())
S = [[c for c in input()] for i in range(N)]
dic={}
for i in range(len(S)):
    S[i].sort()
    S[i] = str(S[i])
    if S[i] in dic:
        dic[S[i]] +=1
    else:
        dic[S[i]] = 1

comb=0
for i in dic.keys():
    if dic[i] !=0:
        comb = comb + dic[i]*(dic[i]-1)/2
print(int(comb))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

n = int(input())
s=[]
m={}
sum=0
for i in range(0,n):
    # print((list(input()))
    tmp = "".join(sorted(input()))
    if tmp in m:
        m[tmp]+=1
    else:
        m[tmp] = 1
for k in m:
    if m[k]>1:
        # print(m[k])
        # print("["+k)
        sum+=cmb(m[k],2)
print(sum)