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 = [input() for _ in range(n)]
ss = [''.join(sorted(s_tmp)) for s_tmp in s]
ss.sort()
count = 1
ans = 0
for i in range(n-1):
    if ss[i] == ss[i+1]:
        count += 1
    else:
        ans += int((count*(count-1))/2)
        count = 1
ans += int((count*(count-1))/2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = [input() for _ in range(n)]
dict = {}
alpha = "abcdefghijklmnopqrstuvwxyz"

for s in arr:
    count = [0]*26
    for i in range(10):
        for j in range(26):
            if s[i] == alpha[j]:
                count[j] += 1
                break
    count = tuple(count)
    if count not in dict:
        dict[count] = 1
    else:
        dict[count] += 1
ans = 0
for val in dict.values():
    ans += val * ( val - 1 ) / 2
print(int(ans))

Submission status: Accepted

Solution code:
import collections
def resolve():
    N = int(input())
    S = [input() for i in range(N)]
    ans = 0

    for i in range(N):
        S[i]=str(sorted(S[i]))
    c = collections.Counter(S)
    for i in c.values():
        for j in range(1,i):
            ans += j
    print(ans)
    
resolve()

Submission status: Accepted

Solution code:
N=int(input())
s=[]
for i in range(N):
  ss=list(input())
  ss.sort()
  s.append(ss)
s.sort()
res=0

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

Submission status: Accepted

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

bun_lis.sort()

total = 0
cnt = 1
bun_lis.append('XXX') #タグ

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

for n in range(N):
    if bun_lis[n] != bun_lis[n+1]:
        total += pea(cnt)
        cnt = 1
        continue
    cnt += 1

print(total)

Submission status: Accepted

Solution code:
n=int(input())
ss=[input() for _ in range(n)]

samedic={}
for s in ss:
  sort_s=''.join(sorted(list(s)))
  if sort_s in samedic:
    samedic[sort_s]+=1
  else:
    samedic[sort_s]=1

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

Submission status: Accepted

Solution code:

N = int(input())
ans = 0
ss = []
for _ in range(N):
    s = input()
    s = list(s)
    s.sort()
    s = "".join(s)
    ss.append(s)
ss.sort()
now = ss[0]
ruiseki = 0
for i in range(1,N):
    if ss[i] == now:
        ruiseki +=1
        ans += ruiseki
    else:
        ruiseki = 0
        now = ss[i]
print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

N = int(input())
counter = defaultdict(int)

value = 0
for i in range(N):
    sorted_str = ''.join(sorted(input()))
    value += counter[sorted_str]
    counter[sorted_str] += 1
print(value)

Submission status: Accepted

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

samedic={}
for s in s_list:
    sorder=''.join(sorted(list(s)))
    if sorder in samedic:
        samedic[sorder]+=1
    else:
        samedic[sorder]=1

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

Submission status: Accepted

Solution code:
from math import factorial
from collections import Counter


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

sorted_strings = [''.join(sorted(s)) for s in strings]
num_strings = Counter(sorted_strings).values()

num_anagram = int(sum(factorial(num) / (factorial(num-2) * 2) if num >= 2 else 0
                      for num in num_strings))
print(num_anagram)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())

dic = defaultdict(int)
for _ in range(n):
  s = ''.join(sorted(input()))  
  dic[s] +=  1
  
ans = sum(v*(v-1)/2 for v in dic.values())  
print(int(ans))

Submission status: Accepted

Solution code:
ans = 0
N = int(input())
s_list = [''.join(sorted(input())) for _ in range(N)]
s_list.sort()

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

    if i == N-1:
        ans += (count*(count-1))/2

print(int(ans))

Submission status: Accepted

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
lst = []
for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    lst.append(s)
lst = list(Counter(lst).items())
ans = 0
for i,j in lst:
    if j == 1:
        continue
    ans += ((j-1)*j)//2
print(ans)

Submission status: Accepted

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

ss = sorted(ss)

res = 0
while True:
    if len(ss) <= 1:
        break
    if ss[0] != ss[1]:
        ss.pop(0)
        continue
    i = 1
    while True:
        if i == len(ss):
            res += i * (i-1) / 2
            for num in range(i):
                ss.pop(0)
            break
        if ss[0] == ss[i]:
            i += 1
        else:
            res += i * (i-1) / 2
            for num in range(i):
                ss.pop(0)
            break

print(int(res))

Submission status: Accepted

Solution code:
a = int(input())
b = {}
strings = ''
ans = 0

for i in range(a):
    check = False
    strings = ''.join(sorted(list(input())))
    if strings in b.keys():
      check = True
    if check == True:
      ans += b[strings]
      b[strings] += 1
    else:
      b[strings] = 1

print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = {}
strings = ''
ans = 0

for i in range(a):
    check = True
    strings = ''.join(sorted(list(input())))
    if strings not in b.keys():
      check = False
      b[strings] = 1
    if check == True:
      ans += b[strings]
      b[strings] += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

cnt = int(raw_input())
lines = []
for i in range(cnt):
    line = sorted([s for s in raw_input()])
    line = ''.join(line)
    lines.append(line)
counters = Counter(lines)
total = 0
for elm, counter in counters.items():
    total += sum(range(1, counter))
print total

Submission status: Accepted

Solution code:
# ABC137C_GreenBin.py
# ちなみに D は DP, E は ヘルマンフォード法 の良問。

import collections

"""
英子文字 26 個を素数で置き換えて掛け合わせると、アナグラムの組は同じ値となる。
1 <= i < j <= n であって、 si が sj のアナグラムであるようなものの個数を求める。
"""


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


# 入力と同時にアナグラムを素数の積に変換
Era = eratosthenes_sieve(150)  # 小さいほうから素数 26 個あまり用意する
chars = "abcdefghijklmnopqrstuvwxyz"  # Python では文字列もイテラブル
N = int(input())
array = [0] * N
for i in range(N):
    new = input()
    var = 1
    for j in range(10):
        h = chars.index(new[j])
        var *= Era[h]
    array[i] = var

# 加工と出力
kinds = collections.Counter(array)
kind = list(kinds.values())
print(sum(kind[i] * (kind[i] - 1) // 2 for i in range(len(kinds))))

"""
# collection モジュールのまとめ
Counter は辞書型 dict のサブクラスで、キーに要素、値に出現回数という形のデータを持つ。
キーのリスト（keys()メソッド）、値のリスト（values()）、
キーと値のペアのタプルのリスト（items()メソッド）を取得したりできる。
リストに変換したい場合はlist()を使えばよい。
"""

Submission status: Accepted

Solution code:
N = int(input())
str_unions = sorted([tuple(sorted(input())) for _ in range(N)])
count_dict = {}
ret = 0

for s in str_unions:
    if s not in count_dict:
        count_dict[s] = 1
    else: count_dict[s] += 1

sum_n = lambda n: n*(n+1)//2
print(sum(map(lambda x:sum_n(x-1), count_dict.values())))

Submission status: Accepted

Solution code:
import collections
n=int(input())
l=[]
for _ in range(0, n):
  s=list(input())
  s.sort()
  l.append("".join(s))
cc=collections.Counter(l)
i=0
for k, v in cc.items():
  if v >= 2:
    i+=(v*(v-1))/2
print(int(i))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

c = Counter(s)


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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ans = 0
d = dict()
for _ in range(n):
    s = input()
    t = sorted(list(s))
    t = ''.join(t)
    if t not in d: d[t] = 1
    else: 
        ans += d[t]
        d[t] += 1

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

def main():
    n = int(input())
    l = ["".join(sorted(input())) for i in range(n)]

    c = {}
    ans = 0
    for s in l:
        if s in c:
            c[s] = c[s] + 1
        else:
            c[s] = 0
        ans += c[s]

    print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
count = {}
ans = 0
for i in s:
    count[i] = count.get(i, 0) + 1
for key, value in count.items():
    ans += value * (value - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = [input() for i in range(n)]
for i in range(n):
  S[i] = sorted(S[i])
S = sorted(S)
c = 0
i = 0
while i < n - 1:
  j = i + 1
  while j < n:
    if S[i] == S[j]:
      j += 1
    else:
      break
  c += (j-i) * (j-i-1) // 2
  i = j  
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict
x=defaultdict(int)
N=int(input())
for i in range(N):
    x[''.join(sorted(input()))]+=1
an=0
for i in x.values():
    an+=int(i*(i-1)/2)
print(an)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
s=[input() for i in range(n)]
ans=0
dic={}
for i in range(n):
    tmp=list(s[i])
    tmp.sort()
    tmp="".join(tmp)
    if tmp in dic:
        dic[tmp]+=1
    else:
        dic[tmp]=1
for i in dic.values():
    ans+=i*(i-1)//2
# print(dic)
print(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:
        ans+=d[s]
        d[s]+=1
    else:
        d[s]=1
print(ans)

Submission status: Accepted

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

count = {}

for v in s:
    count[v] = count.get(v, 0) + 1

def sig(k):
    return ((k + 1) * k) // 2

ans = 0
for key, value in count.items():
    ans += (sig(value - 1))
print(ans)

Submission status: Accepted

Solution code:
n1 = int(input())

s1 = []
t1 = ""
d1 = {}
for i1 in range(n1):
    t1 = ''.join(sorted(input()))
    if d1.get(t1, 0) == 0:
        d1[t1] = 1
    else:
        d1[t1] += 1

r1 = 0

for v1 in d1.values():
    if v1 != 1:
        r1 += ((v1 * (v1 - 1)) // 2)

print(r1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

n = int(input())
l = [str(sorted(str(input()))) for _ in range(n)]
 
d = {}
 
for i in range(n):
  s = l[i]
  # print(s)
  if s in d:
    d[s] += 1
  else:
    d[s] = 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(n):
  w=sorted(list(input()))
  l.append(w)
l=sorted(l)
c=0
c1=0
w1=l[0]
for i in range(n):
  if w1==l[i]:
    c1+=1
  else:
    w1=l[i]
    c+=c1*(c1-1)//2
    c1=1
    
c+=c1*(c1-1)//2
print(c)

Submission status: Accepted

Solution code:
# C - Green Bin
def main():
    count = 0
    dictCount = {}
    for i in [input() for i in range(int(input()))]:
        strInput = ''.join(sorted(i))
        if strInput in dictCount:
            count += dictCount[strInput]
            dictCount[strInput] += 1
        else:
            dictCount[strInput] = 1
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C - Green Bin
def main():
    count = 0
    dictCount = {}
    for i in [input() for i in range(int(input()))]:
        strInput = ''.join(sorted([c for c in i]))
        if strInput in dictCount:
            count += dictCount[strInput]
            dictCount[strInput] += 1
        else:
            dictCount[strInput] = 1
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections

n = int(input())

counter = collections.Counter()
for _ in range(n):
    s = ''.join(sorted(input()))
    counter[s] += 1

res = 0
for value in counter.values():
    if value > 1:
        res += value * (value - 1) // 2

print(res)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
s_c = collections.Counter(s)
print(sum(i*(i-1)//2 for i in s_c.values()))

Submission status: Accepted

Solution code:
import math
def combo(a):
    return (math.factorial(a))/(math.factorial(a-2)*2)

from collections import Counter
n = int(input())
count = 0
string = [input() for i in range(n)]
string2 = [''.join(sorted(x)) for x in string]

'''for i in range(n-1):
    for j in range(i+1, n):
        if string2[i] == string2[j]:
            count += 1'''
li = Counter(string2)
li1=[[key,value] for key, value in li.items()]
for i in range(len(li1)):
    if li1[i][1] == 1:
        li1[i][1] = 0
for i in range(len(li1)):
    if li1[i][1] != 0:
        li1[i][1] = combo(li1[i][1])
for i in range(len(li1)):
    count += li1[i][1]
print(int(count))

Submission status: Accepted

Solution code:
def dns(n):
    fact = [0, 0, 1]
    for i in range(2, n+1):
        fact.append(fact[-1]+i)
    return fact


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

hoge = dns(100008)

anagram_dict = {}

new_S = []

for s in S:
    tmp = "".join(sorted(list(s)))
    new_S.append(tmp)

for s in new_S:
    anagram_dict.setdefault(s, 0)
    anagram_dict[s] += 1

ans = 0
for v in anagram_dict.values():
    ans += hoge[v]

print(ans)

Submission status: Accepted

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

S_dict = {}

for S in S_lists:
    S_list = list(S)
    S_list.sort()

    S_str = "".join(S_list)
    if S_str in S_dict.keys():
        S_dict[S_str] = S_dict[S_str] + 1
    else:
        S_dict[S_str] = 1

answer = 0
for v in S_dict.values():
    if v > 1:
        answer += int(v * (v - 1) / 2)

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
s=[input() for i in range(n)]
count=[[0]*26 for _ in range(n)]
ans=0
for i in range(n):
    for j in range(10):
        count[i][ord(s[i][j])-ord("a")]+=1
for i in range(n):
    count[i]=tuple(count[i])
for i in dict(Counter(count)).values():
    for j in range(i):
        ans+=j
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
d={}
for i in range(N):
  text=''.join(sorted(input()))
  if text in d :
    d[text]+=1
  else:
    d[text]=1
count=0
for v in d.values() :
  count+=(v-1)*v/2
print(int(count))

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for x in range(n)]
count = 0
d = {}

for i in range(n):
    s1 = list(s[i])
    s1.sort()
    string = "".join(s1)
    if (string not in d.keys()):
        d[string] = 0
    else:
        d[string] += 1
        count += d[string]

print(count)

Submission status: Accepted

Solution code:
N=int(input())
l=[]
from collections import Counter
for i in range(N):
  l.append(''.join(sorted(input())))
l1=Counter(l).values()
from math import factorial
ans=0
for a in l1:
  if a >= 2:
    ans+=int(factorial(a)/(factorial(a-2)*factorial(2)))
print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    str_cnt = defaultdict(int)
    for _ in range(N):
        s = str(sorted(ST()))
        str_cnt[s]+=1
    ans = 0
    for cnt in str_cnt.values():
        ans += cnt*(cnt-1)//2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s = [ str(input()) for _ in range(n)]
s = sorted([sorted(x) for x in s])
pairs = 0
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

i=0
while i < n-1:
    sames = 0
    for j in range(i+1, n):
        if s[i] == s[j]:
            sames += 1
            continue
        else:
            break
    i += sames + 1
    if sames != 0:
        pairs += combinations_count(sames + 1, 2)


print(int(pairs))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnts = defaultdict(int)

for s in S:
    cnts[s] += 1

res = 0

for _, v in cnts.items():
    res += v * (v - 1) // 2

print(res)

Submission status: Accepted

Solution code:
import collections
n = int(input())
input_str = [input() for _ in range(n)]

sorted_input = ["".join(sorted(i)) for i in input_str]

count = collections.Counter(sorted_input)

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



print(int(result))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# original_author prd_xxx
from collections import Counter

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

# collections.Counter()を使うと0回目の時にバグらないので23行目elseを省略可能
ctr = Counter()
ctr = {}
ans = 0

for s in S:
    # 文字列を昇順に並べ替え
    # 文字列をリスト化し、sortしたものを文字列化
    a = ''.join(sorted(list(s)))

    # ソート済み文字列が以前に存在していたら、答えに前回の合計値を加算する
    # 1..0 2..1 3..3 4..6 5..10 ... 組み合わせの数n(n-1)/2があり、
    # 最後にO(n)で計算も可能だが、冗長。
    # 一個ずつ処理していくと、
    # 1..0 2..+1(1) 3..+2(3) 4..+3(6) 5..+4(10)の増加になる
    if a in ctr:
        ans += ctr[a]
    else:
        ctr[a] = 0

    ctr[a] += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
ss = ["".join(sorted(input())) for _ in range(n)]
c = Counter(ss)
total = 0
for a, b in c.most_common(len(c)):
    total += (b * (b - 1)) // 2
print(total)

Submission status: Accepted

Solution code:
def return_sorted_string(string):
    lst=list(map(ord,list(string)))
    lst.sort()
    lst=list(map(chr,lst))
    string="".join(lst)
    return string
    



n = int(input())
strings=[input() for i in range(n)]
dic={}
for i in range(n):
    string=return_sorted_string(strings[i])
    if string in dic:
        dic[string]+=1
    else:
        dic[string]=1

count=0        

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


print(int(count))

Submission status: Accepted

Solution code:
def stringtonum(s):
  s_n = list(map(ord, s))
  s_n.sort()
  res = 0
  for i, n in enumerate(s_n):
    res += (27**i) * (n-97)
  return res

d = {}
N = int(input())
for _ in range(N):
  s = input()
  n = stringtonum(s)
  if n in d:
    d[n] += 1
  else:
    d[n] = 1
    
res = 0
for v in d.values():
  res += v*(v-1)//2
print(res)

Submission status: Accepted

Solution code:
N=int(input())
count=0
S=[input() for i in range(N)]
d={}

for i in S:
    a=''.join(sorted([c for c in i]))
    if a in d:
        count+=d[a]
        d[a]+=1
    else:
        d[a]=1
print(count)

Submission status: Accepted

Solution code:
import string


def solve():
    s = read()
    result = think(s)
    write(result)


def read():
    n = read_int(1)[0]
    s = []
    for _ in range(n):
        s.append(read_line())
    return s


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(s):
    dictionary_of_histogram = {}
    for line in s:
        key_of_histgram = make_key_for_histgram(line)
        if key_of_histgram in dictionary_of_histogram:
            dictionary_of_histogram[key_of_histgram] += 1
        else:
            dictionary_of_histogram[key_of_histgram] = 1

    result = 0
    for k, v in dictionary_of_histogram.items():
        if v >= 2:
            result += combination_2(v)
    return result


def make_key_for_histgram(s):
    key = ''
    for c in string.ascii_lowercase:
        count = s.count(c)
        if count > 0:
            key += ',{0:s}{1:d}'.format(c, count)
    return key


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


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(str)

print(sum(i*(i-1)//2 for i in c.values()))

Submission status: Accepted

Solution code:
from collections import Counter
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()))

Submission status: Accepted

Solution code:
N = int(input())
counters = []
for i in range(0, N):
	lst = []
	for j in range(0, 26):
		lst.append(0)
	S = input()
	for j in range(0, 10):
		lst[ord(S[j])-97] += 1
	counters.append(lst)
counters.sort()
cntlist = [1]
for i in range(1, N):
	if counters[i-1] == counters[i]:
		cntlist[-1] += 1
	else:
		cntlist.append(1)
ans = 0
for i in range(0, len(cntlist)):
	if cntlist[i] != 1:
		ans += (cntlist[i] * (cntlist[i]-1)) // 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    str_unions = sorted([tuple(sorted(input())) for _ in range(N)])
    count_dict = {}
    ret = 0
    
    for s in str_unions:
        if s not in count_dict:
            count_dict[s] = 1
        else: count_dict[s] += 1

    sum_n = lambda n: n*(n+1)//2
    # print(count_dict)
    print(sum(map(lambda x:sum_n(x-1), count_dict.values())))
    
solve()

Submission status: Accepted

Solution code:
from scipy.misc import comb
 
N = int(input())
s = {}
for _ in range(N):
    b = list(input())
    b.sort()
    c = ''.join(b)
    if not c in s:
        s[c] = 1
    else:
        s[c] += 1
 
a = [comb(v, 2, exact=True) for v in s.values()]
print(sum(a))

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    str_unions = sorted([sorted(input()) for _ in range(N)])
    ret = 0
    count_tmp = 1
    s_temp = []
    
    for s in str_unions:
        if s == s_temp:
            ret += count_tmp
            count_tmp += 1
        else:
            s_temp = s
            count_tmp = 1
    # print(str_unions)
    print(ret)

solve()

Submission status: Accepted

Solution code:
N = int(input())
l = ["".join(sorted(list(input()))) for i in range(N)]
d = {}
ans = 0

for text in l:
  if text in d.keys():
    d[text] += 1
    ans += d[text]
  else:
    d[text] = 0

print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
s_hist = collections.Counter()

for n in range(N):
    s = input().strip()
    s_hist[" ".join(sorted(s))] += 1

ans = 0
for pair in s_hist.most_common():
    ans += s_hist[pair[0]]*(s_hist[pair[0]]-1)/2
    if(pair[1] == 1):
        break
print(int(ans))

Submission status: Accepted

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

t = {}
ans = 0
for s in S:
    st = "".join(sorted(list(s)))
    if st in t.keys():
        t[st] += 1
        ans += t[st]
    else:
        t[st] = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
from math import factorial

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

c = collections.Counter(S)
cnt = 0
r = 2

for n in c.values():
    if n >= 2:
        cnt += factorial(n)/(factorial(r)*factorial(n - r))
print(int(cnt))

Submission status: Accepted

Solution code:
import collections
from math import factorial

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

c = collections.Counter(S)
cnt = 0
r = 2

for n in c.values():
    if n >= 2:
        a = factorial(n)
        b = factorial(r)*factorial(n - r)
        cnt += a/b
print(int(cnt))

Submission status: Accepted

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

for i in s:
    if i in dict:
        ans += dict[i]
        dict[i] += 1
    else:
        dict[i] = 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(20000000)


def main():

    n = int(input())
    dic = {}
    for _ in range(n):
        t = ''.join(sorted(list(input())))
        if t in dic:
            dic[t] += 1
        else:
            dic[t] = 0

    def pattern(num):
        tmp = 0

        for i in range(1, num+1):
            tmp += i
        return tmp

    ans = 0
    for d in dic:
        if dic[d]:
            ans += pattern(dic[d])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

ct = Counter(s).values()
ct2 = Counter(ct).items()

ans = 0
for i, j in ct2:
    if i >= 2:
        ans += j*i*(i-1)//2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=[''.join(sorted(input())) for i in range(n)]
x=set()
d={}
cnt=0
for i in range(n):
  if s[i] not in x:
    x.add(s[i])
    d[s[i]]=1
  else:
    cnt+=d[s[i]]
    d[s[i]]=d[s[i]]+1
print(cnt)

Submission status: Accepted

Solution code:
# C - Green Bin

n = int(input())
s = dict()

for i in range(0, n):
    char = list(input())
    char.sort()
    _str = ''.join(char)
    cnt = s[_str] + 1 if _str in s else 1
    s[_str] = cnt

ret = 0
for key, val in s.items():
    if val > 1:
        ret += sum([x for x in range(0, val)])

print(ret)

Submission status: Accepted

Solution code:
N =int(input())
#横文字数、縦N行の行列を作成
s = [list(input()) for i in range(N)]

#文字列をアルファベット順に並び替える
for i in range(len(s)):
    s[i].sort()
s.sort()   
ans = 0 
cnt = 1
for i in range(1,N):
    for j in range(10):
        #1文とその次の文の文字を比較
        #異なっていた場合はbreak
        if(s[i][j] != s[i-1][j]):
            break
    else:
        cnt +=1
        continue
    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())
s = list(input() for i in range(n))

for i in range(n):
    new_str_list = sorted(list(s[i]))
    new_str = ''.join(new_str_list)
    s[i] = new_str

c = collections.Counter(s)
cl = list(c.values())
cl = [i for i in cl if i >= 2]
ans = 0
for j in cl:
    ans = ans + math.factorial(j) // (math.factorial(j - 2) * math.factorial(2))

print(ans)

Submission status: Accepted

Solution code:
dic = {}
lop = int(input())
for i in range(lop):
    N = list(input())
    N.sort()
    N = "".join(N)
    if N in dic:
        dic[N] += 1
    else:
        dic[N] = 1
print(sum(list(map(lambda x: int((x * (x - 1)) / 2), list(dic.values())))))