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:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s

def hhash(d):
    r = ""
    for key in sorted(d.keys()):
        r += (key + str(d[key]))
    return r


n = load()
S = []
for _ in range(n):
    S.append(load(str))

hash_count = defaultdict(int)
for e in S:
    cc = defaultdict(int)
    for c in e:
        cc[c] += 1
    hash_count[hhash(cc)] += 1


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

r = 0
for hash in hash_count.keys():
    if hash_count[hash] > 1:
        r += nC2(hash_count[hash])
    
print(r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 25 18:54:16 2020

@author: Kanaru Sato
"""

n = int(input())
Dict = {}
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = []
for i in range(n):
    s = ""
    l = sorted(list(input()))
    for j in range(len(l)):
        s += l[j]
    L.append(s)

dic = {}
for i in range(n):
    if L[i] not in dic:
        dic[L[i]] = 1
    else:
        dic[L[i]] += 1

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

Submission status: Accepted

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

from collections import defaultdict

def main():
    n = int(input())
    ss = defaultdict(int)
    for i in range(n):
        s = "".join(sorted(input()))
        ss[s] = ss[s] + 1

    print(sum(c2(v) for k, v in ss.items()))

def c2(n):
    # Choosing 2 from n
    return n * (n - 1) // 2

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def count_anagram_strings():
    n = int(input())
    count = 0
    d = dict()
    for i in range(n):
        key = "".join(sorted(list(input())))
        if key not in d:
            d[key] = 1
        else:
            d[key] += 1

    # N numbers have N * (N - 1) / 2 pairs
    for v in d.values():
        count += v * (v - 1) / 2
    
    print(int(count))

if __name__ == '__main__':
    # n = int(input())
    count_anagram_strings()

Submission status: Accepted

Solution code:
n = int(input())
L = []
for i in range(n):
    s= ""
    l = sorted(list(input()))
    for j in range(len(l)):
        s += l[j]
    L.append(s)

dic = {}
for i in range(n):
    if L[i] not in dic:
        dic[L[i]] = 1
    else:
        dic[L[i]] += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

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

s_list.sort()
same_dict = {}
for i in range(n-1):
    if s_list[i] == s_list[i+1]:
        mojiretsu = ''.join(s_list[i])
        if not mojiretsu in same_dict.keys():
            same_dict[mojiretsu] = 1
        same_dict[mojiretsu] += 1
same_dict_values = same_dict.values()
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
ans = 0
for value in same_dict_values:
    ans += combinations_count(value, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
sis = []
for i in range(n):
  si = sorted(input())
  si = ''.join(si)
  sis.append(si)
C = Counter(sis)
ans = 0
for v in C.values():
  ans += sum(range(1, v))
print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    dic = {}
    for i in range(n):
        a = ''.join(sorted(input().rstrip()))
        if a in dic:
            dic[a] += 1
        else:
            dic[a] = 1
    su = 0
    for v in dic.values():
        su += v * (v - 1) // 2
    print(su)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.buffer.readline
ans = {}

def main():
    n = int(input())
    for i in range(n):
        dic = {}
        for a in input().strip():
            if a in dic:
                dic[a] += 1
            else:
                dic[a] = 1
        dic2 = tuple(sorted(dic.items()))
        #print(dic2)
        tmp = ""
        for k, v in dic2:
            tmp += str(k) + str(v)
        if tmp in ans:
            ans[tmp] += 1
        else:
            ans[tmp] = 1
    su = 0
    for vv in ans.values():
        su += vv * (vv -1) // 2
    print(su)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
N = int(input())
L = dict()
res = 0
for i in range(N):
    a = input()
    A = ''.join(sorted(a))
    if A not in L:
        L[A] = 1
        continue
    res += L[A]
    L[A] += 1
print(res)

Submission status: Accepted

Solution code:
import collections

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

for i in range(N):
    si = raw_input()
    s.append(str(sorted(si)))

s = collections.Counter(s)

ans = 0

for item in s:
    n = s[item]
    ans += n*(n-1)/2

print ans

Submission status: Accepted

Solution code:
n = int(input())
s = [''.join(sorted(input())) for i in range(n)]
s.sort()
ans = 0
count = 1
for i in range(1,n):
    if s[i] != s[i-1]:
        ans += count * (count-1) //2
        count = 1
    else:
        count+=1
ans +=count*(count-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
S=[''.join(sorted(input())) for i in range(N)]
T=collections.Counter(S)
ans=0
for i in T.most_common():
    ans+=Comb(i[1],2)
#print(S)
#print(T)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = [''.join(sorted(list(sr()))) for _ in range(N)]

counter = Counter(S)
answer = sum(x*(x-1)//2 for x in counter.values())
print(answer)

Submission status: Accepted

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def combinations_count(n, r): # 組み合わせ
    return factorial(n) // (factorial(n - r) * factorial(r))

N = ir()
S = defaultdict(int)
for _ in range(N):
    s = list(sr())
    s.sort()
    s = str(s)
    if s in S:
        S[s] += 1
    else:
        S[s] = 1

counter = Counter(S)
answer = 0
for v in counter.values():
    if v >= 2:
        answer += combinations_count(v, 2)

print(answer)

Submission status: Accepted

Solution code:
n = int(input())

s = []
for nk in range(n):
  tmp = list(input())
  tmp.sort()
  mo = ''
  for i in tmp:
    mo += i

  s.append(mo)

def comb(x):
  res = x*(x-1)/2
  return res
  
s.sort()
tc = 1
l = []
ans = 0

for i in range(1,n):
  if s[i-1] == s[i]:
    tc += 1

  else:
    if tc >= 2 :
      ans += comb(tc)
      tc = 1

if tc >= 2 :
  ans += comb(tc)
  tc = 1

print(int(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
import math
n = int(input())
res = 0
all = []
for i in range(n):
    s = input()
    sor = sorted(s)
    all.append("".join(sor))
counter = dict(Counter(all))
for i in counter.values():
    if i > 1: 
        res += math.factorial(i)/(2*math.factorial(i-2))
print(int(res))

Submission status: Accepted

Solution code:
N=int(input())
from collections import defaultdict
d = defaultdict(int)
ans=0
for i in range(N):
  s=''.join(sorted(list(input())))
  if d[s]!=0:
    ans+=d[s]
  d[s]+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ss = list(''.join(sorted(list(input()))) for _ in range(n))
words = {}
cnt = 0

for s in ss:
    if s in words:
        cnt += words[s]
        words[s] += 1
    else:
        words[s] = 1
print(cnt)

Submission status: Accepted

Solution code:
import collections

ss = []


N = int(input())
for i in range(N):
    s = str(sorted(input()))
    ss.append(s)

c = collections.Counter(ss)
ans = 0

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

Submission status: Accepted

Solution code:
import math

N = int(input())
s = [""] * N
for i in range(N):
	s[i] = list(input())
	s[i].sort()
s.sort()

st, count = 1, 0
for i in range(1, N):
	if s[i - 1] == s[i]:
		st += 1
	elif st > 1:
		count += math.factorial(st) // (2 * math.factorial(st - 2))
		st = 1
if st > 1:
	count += math.factorial(st) // (2 * math.factorial(st - 2))

print(count)

Submission status: Accepted

Solution code:
n=int(input())
L=[[] for _ in range(n)]
ans=0
for i in range(n):
    L[i]=sorted(list(input()))
L=sorted(L)
tmp=1
ans=0
for i in range(n-1):
    if L[i]==L[i+1]:
        tmp+=1
    else:
        ans+=tmp*(tmp-1)//2
        tmp=1
ans+=tmp*(tmp-1)//2
print(ans)

Submission status: Accepted

Solution code:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
from collections import Counter
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product

def resolve():
    N=int(input())
    S=[''.join(sorted(input())) for i in range(N)]
    C=Counter(S)
    print(sum([i*(i-1)//2 for i in C.values()]))






resolve()

Submission status: Accepted

Solution code:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
from collections import Counter
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product

def resolve():
    N=int(input())
    S=[''.join(sorted(input())) for i in range(N)]
    C=Counter(S)
    print(sum(i*(i-1)//2 for i in C.values()))






resolve()

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
# 列ベクトルとかの受け取り
lists = ["".join(sorted(list(input()))) for _ in range(n)]

strcount = Counter(lists)

ans = 0
for i,n in strcount.items():
    if n == 1:
        continue
    else:
        ans += n*(n-1)//2

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
# 列ベクトルとかの受け取り
lists = ["".join(sorted(list(input()))) for _ in range(n)]

strcount = Counter(lists)

ans = 0
for i,n in strcount.items():
    if n == 1:
        continue
    else:
        ans += n*(n-1)/2

ans = int(ans)
print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

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

s.sort()
s.append('end')

result = 0
count = 1

for i in range(N):
    if s[i] == s[i + 1]:
        count += 1
    else:
        result += count * (count - 1) / 2
        count = 1

print(int(result))

Submission status: Accepted

Solution code:
#C Gereen Bin
from collections import Counter
n=int(input())
s=[]
for i in range(n):
    s.append(''.join(sorted(input())))
s.sort()
s=Counter(s)
ans=0
for i,num in s.items():
    ans+=(num-1)*num/2
print(int(ans))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(s)
ans = 0

for i in set(s):
  n = c[i]
  ans += n * (n - 1) // 2


print(ans)

Submission status: Accepted

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

N = int(input().strip())
s_list = [input().strip() for _ in range(N)]
#-----

check_dic={}
cnt=0
for i,s in enumerate(s_list):
    key = "".join(sorted(s))
    
    if key not in check_dic:
        check_dic[key] = 1
    else:
        cnt += check_dic[key]
        check_dic[key] += 1

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter


def nC2(n):
  return n * (n - 1) // 2


N = int(input())

strings = []

for _ in range(N):
  strings.append(''.join(sorted(input())))

counter = Counter(strings)  
                 
print(sum(nC2(n) for n in counter.values()))

Submission status: Accepted

Solution code:
#137_C
from collections import Counter
n = int(input())
w = []
for _ in range(n):
    s = sorted(input())
    w.append(''.join(s))
w.sort()
w = Counter(w)
ans = 0
for t, idx in w.items():
    ans += idx * (idx - 1) // 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(n):
    ss = input()
    ss = [i for i in ss]
    ss.sort()
    ss = tuple(ss)
    if ss in s:
        s[ss] +=1
    else:
        s[ss] = 1

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
c = Counter([tuple(sorted(input())) for _ in range(N)])
print(sum([(v*(v-1))//2 for v in c.values()]))

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(s)
res = 0
for cnt in counter.values():
  res += (cnt * (cnt - 1)) // 2

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
n=int(input())
s=[]
for i in range(n):
    tmp=list(input())
    tmp.sort()
    tmp=''.join(tmp)
    s.append(tmp)

d=dd(int)
for ss in s:
    d[ss]+=1
ans=0
for k,v in d.items():
    if v>=2:
        ans+=v*(v-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A=[]
for i in range(N):
    x= str(sorted(input()))
    A.append(x)
B=collections.Counter(A)
ans = 0
for i in B.values():
    ans += i*(i-1)//2
print(ans)

Submission status: Accepted

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

    if sn in s:
        ans += s[sn]
        s[sn] += 1
    else:
        s[sn] = 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dic_sorted_s = {}
count = 0
for i in range(n):
    s = input()
    str_sorted_s = ''.join(sorted(s))
    try:
        count += dic_sorted_s[str_sorted_s]
        dic_sorted_s[str_sorted_s] += 1
    except KeyError:
        dic_sorted_s[str_sorted_s] = 1
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Green Bin
https://atcoder.jp/contests/abc137/tasks/abc137_c

"""
import sys

from collections import Counter
from string import ascii_lowercase

def solve(N, strings):
    cnt = Counter()
    for s in strings:
        x = ''.join('{}-{}'.format(ch, s.count(ch)) for ch in ascii_lowercase)
        cnt[x] += 1
    ans = [(1 + v - 1) * (v - 1) // 2 for v in cnt.values() if v > 1]
    return sum(ans)


def main(args):
    N = int(input())
    strings = [input() for _ in range(N)]
    ans = solve(N, strings)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
d,a = {},0
for i in range(int(input())):
    k = ''.join(sorted(input()))
    d[k] = d.get(k,-1)+1
    a+= d[k]
print(a)

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

N = int(input())
counter = Counter(''.join(sorted(input())) for _ in range(N))
print(sum(i * (i - 1) // 2 for i in counter.values()))

Submission status: Accepted

Solution code:
from collections import defaultdict

def solve():
    N = int(input())
    d = defaultdict(int)
    c = 0
    for _ in range(N):
        a = ''.join(sorted(list(input())))
        if a in d:
            c += d[a]
            d[a] += 1
        else:
            d[a] = 1
    return c

print(solve())

Submission status: Accepted

Solution code:
from collections import defaultdict

def solve():
    N = int(input())
    A = [sorted(list(input())) for _ in range(N)]
    d = defaultdict(int)
    for a in A:
        b = ''.join(a)
        d[b] += 1
    c = 0
    for v in d.values():
        if v == 1:
            continue
        for i in range(1, v):
            c += i
    
    return c

print(solve())

Submission status: Accepted

Solution code:
d,a={},0
for i in range(int(input())):
  k=''.join(sorted(input()))
  d[k]=d.get(k,-1)+1
  a+=d[k]
print(a)

Submission status: Accepted

Solution code:
d,a={},0
for s in open(0):k=str(sorted(s));d[k]=d.get(k,-1)+1;a+=d[k]
print(a)

Submission status: Accepted

Solution code:
d,a={},0
for i in range(int(input())):
  k=str(sorted(input()))
  d[k]=d.get(k,-1)+1; a+=d[k]
print(a)

Submission status: Accepted

Solution code:
n=int(input())
d={}
for i in range(n):
  k=''.join(sorted(input()))
  d[k]=d.get(k,0)+1
print(sum(v*~-v//2 for v in d.values()))

Submission status: Accepted

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


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

    
s.sort()
count = 0
ans = 0

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

import math
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
        if i == n-2:
            ans += comb(count+1,2)
    else:
        if count != 0:
            ans += comb(count+1,2)
        count = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
s=[0]*N
t=[0]*N
d=dict()
cnt=0

for i in range(N):
  s[i]=input()
  t[i]=str(sorted(s[i]))
  if t[i] not in d:
    d[t[i]] =1
    #print(i,d)
  else:
    cnt += d[t[i]]
    d[t[i]] +=1
    #print(i,d)

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = sorted([sorted(input()) for _ in range(n)])
cnt = 1
ans = 0
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:
n = int(input())
lst = []
for _ in range(n):
    lst.append(sorted(list(input())))
lst.sort()
ans = 0
count = 0
for i in range(n-1):
    if lst[i] == lst[i+1]:
        count += 1
    else:
        ans += count*(count+1)//2
        count = 0
ans += count*(count+1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def read_input():
    
    return n, slist


def submit():
    n = int(input())
    slist = []
    for _ in range(n):
        s = list(input())
        s.sort()
        slist.append("".join(s))
    s_count = Counter(slist)

    pair_count = 0
    for s, c in s_count.items():
        pair_count += c * (c - 1) // 2

    print(pair_count)

    
if __name__ == "__main__":
    submit()

Submission status: Accepted

Solution code:
from collections import defaultdict

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


DICS = defaultdict(int)
for s in S:
    d = [0] * 26
    for ch in s:
        d[ord(ch)-ord("a")] += 1
    DICS[tuple(d)] += 1
ans = 0
for key in DICS:
    if DICS[key] > 1:
        ans += DICS[key] * (DICS[key] - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
d = dict()
for _ in range(N):
    s = list(input())
    s.sort()
    s = "".join(s)
    if s in d:
        d[s] += 1
    else:
        d[s] = 1

print(sum(v*(v-1)//2 for v in d.values()))

Submission status: Accepted

Solution code:
import collections
N = int(input())
count = []

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

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[list(input()) for _ in range(N)]
b=0
cnt=0
for i in range(N):
    s[i].sort()
s.sort()
for i in range(N-1):
    if s[i]==s[i+1]:
        b=b+1
    if not(s[i]==s[i+1])and(s[i]!=0):
        cnt=cnt+(b*(b+1))//2
        b=0
    #print(b)
cnt=cnt+(b*(b+1))//2
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
strings=sorted([sorted(list(input())) for i in range(N)])
strings.append("000")
cnt=0
i=0
same=1
while i<N:
    if strings[i]==strings[i+1]:
        same+=1
    else:
        cnt+=same*(same-1)/2
        same=1
    i+=1
print(int(cnt))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

for s in S:
    s.sort()
    s = "".join(s)
    d[s] += 1

ans = 0

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

print(ans)

Submission status: Accepted

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

for s in S:
    s.sort()

S.sort()

cnt = 1
ans = 0
tmp = []

for s in S:
    if s == tmp:
        cnt += 1
    else:
        tmp = s
        ans += cnt * (cnt - 1) // 2
        cnt = 1

ans += cnt * (cnt - 1) // 2

print(ans)

Submission status: Accepted

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

for _ in range(N):
    s = ''.join(sorted(list(input())))
    if s in cnt_dict:
        cnt_dict[s] += 1
    else:
        cnt_dict[s] = 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
S=list(''.join(sorted(input())) for _ in range(n))
C=Counter(S)
ans=0
for c in C.values():
    ans += c*(c-1)//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
d = defaultdict(int)
for i in range(n):
    s = list(input())
    s.sort()
    tmp = ''.join(s)
    d[tmp] += 1
res = 0
for key in list(d):
    v = d[key]
    res += v*(v-1)//2
print(res)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
count = 0
ans = 0
s = [""] * N
for i in range(N):
    tmp = sorted(input())
    s[i] = "".join(tmp)
s.sort()
for i in range(N - 1):
    if s[i] == s[i + 1]:
        count += 1
    elif s[i] != s[i + 1] and count > 0:
        ans += count*(count+1)//2
        count = 0
if count > 0:
    ans += count*(count+1)//2
    count = 0
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=0
s={}
for i in range(n):
  t=''.join(sorted(input()))
  b=s.get(t,0)
  a+=b
  s[t]=b+1
print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())

import collections
s_frq_dict = collections.defaultdict(int)
for _ in range(N):
    s = [c for c in input()]
    s.sort()
    s_frq_dict[tuple(s)] += 1

ret = 0
for frq in s_frq_dict.values():
    ret += int(frq * (frq-1) * 0.5)

print(ret)

Submission status: Accepted

Solution code:
def combi2(a):
    if a == 0: return 0
    return a * (a-1) // 2

n = int(input())

datas = []

for _ in range(n):
    datas.append(sorted(input()))

datas.sort()

total = 0
count = 0
for i in range(n-1):
    if datas[i] == datas[i+1]:
        if count == 0: count = 2
        else: count += 1
    else:
        total += combi2(count)
        count = 0
total += combi2(count)
count = 0
print(total)

Submission status: Accepted

Solution code:
#abc137 c
n=int(input())
table=dict()
for i in range(n):
    s=str(sorted(input()))
    if s not in table:
        table[s]=1
    else:
        table[s]+=1
ans=0

for x in table.values():
    kumi=(x*(x-1))/2
    ans+=int(kumi)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    s = defaultdict(int)
    ans = 0
    for i in range(N):
        a = "".join(sorted(input()))
        s[a] += 1
        if s[a] != 1:
            ans += (s[a]-1)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# tab = [0] * 10**5
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())
info = []
ans = 0

for _ in range(n):
    s = ''.join(sorted(input()))
    # print(s)
    # if ''.join(s) in info:
    #     info[s] = 1
    # else:
    #     info[s] = 2
    info.append(s)

meta = collections.Counter(info)
# print(meta)
for nv in meta.values():
    # print(nv)
    if nv >= 2:
        ans += cmb(nv, 2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = {}
ans = 0
for i in range(n):
    tmp = str(sorted(input()))
    if tmp in s:
        ans += s[tmp]
        s[tmp] += 1
    else:
        s[tmp] = 1
print(ans)

Submission status: Accepted

Solution code:


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

def comb2(x):
	if x==0:
		return 0
	ans = x*(x-1)/2
	return ans

for i in range(n):
	s.append(sorted(list(input())))

s = sorted(s)
count = [0 for _ in range(len(s))]

x = 0

for i in range(n-1):
	if s[i] == s[i+1]:
		count[x] += 1
	else:
		count[x] += 1
		x = x + 1
	if i == n-2:
		count[x] += 1

ans = 0
#print (count)
for i in count:
	ans += comb2(i)


print(int(ans))