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:
m = {}
ans = 0
n = int(input())
def C(x:int) -> 'conbination_nC2':
	return x*(x-1)/2

for i in range(n):
	s = input()
	new_str = ''.join(sorted(s))
	if new_str in m.keys() :
		m[new_str] += 1
	else :
		m[new_str] = int(1)
	

for i in m:
	ans += C(m[i])

print(int(ans))

Submission status: Accepted

Solution code:
import collections

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

lis=[list(s[i]) for i in range(n)]
lis_sort = [sorted(lis[i]) for i in range(n)]
lis_sort = [''.join(lis_sort[i]) for i in range(n)]
lis_sort.sort()

c = collections.Counter(lis_sort)
values, counts = zip(*c.most_common(len(c)))
cnt=0
for i in range(len(counts)):
    if counts[i]==2: cnt+=1
    elif counts[i]>2: cnt+=counts[i]*(counts[i]-1)//2
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

S = [["" for i in range(10)] for j in range(N)]
for i in range(N):
    tmp = list(input())
    tmp_s = sorted(tmp)
    S[i] = tmp_s
S_s = sorted(S)
res = 0
i = 0
while i<N:
    j = 1
    count = 1
    while i+j<N and S_s[i] == S_s[i+j]:
        j = j + 1
        count = count + 1

    if count > 1:
        res += count*(count-1)//2
    else:
        res += 0
    i = i + j

print(res)

Submission status: Accepted

Solution code:
N = int(input())
s = [0]*N
d = {}

for i in range(N):
    s_lst = sorted(input())
    s[i] = ''.join(s_lst)
    if d.get(s[i]) is None:
        d[s[i]] = 1
    else:
        d[s[i]] += 1

ans = 0
for k, v in d.items():
    if v >= 2:
        ans += v*(v-1)/2

print(int(ans))

Submission status: Accepted

Solution code:
def nc2(x):
    return 0 if x == 1 else x * (x - 1) // 2


N = int(input())
S = {}
for i in range(N):
    w = ''.join(sorted(input()))
    S[w] = S.get(w, 0) + 1
ans = sum(map(nc2, S.values()))
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
slist=[''.join(sorted(input())) for i in range(N)]
c=collections.Counter(slist)
ans=0
for i,j in c.most_common():
    ans+=j*(j-1)//2
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
tmp=[sorted(input()) for i in range(N)]
slist=[''.join(tmp[i]) for i in range(N)]
c=collections.Counter(slist)
ans=0
for i in c.most_common():
    ans+=i[1]*(i[1]-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = []
for i in range(n):
  	s.append(input())

s = ["".join(sorted(i)) for i in s]
table = {}

count = 0
for word in s:
  if word in table:
    count += table[word]
    table[word] += 1
  else :
    table[word] = 1
print(count)

Submission status: Accepted

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

import collections
cnt = collections.Counter(s)

ans = 0
for n in cnt.values():
    if n != 1:
        k = n * (n-1) // 2
        ans += k 

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [str(input()) for _ in range(n)]
abc_str = 'abcdefghijklmnopqrstuvwxyz'
anagram_lst = []
number = 0

def count_lst(x):
    count_lst = [0]*26
    for j in range(10):
        abc = x[j]
        for k in range(26):
            if abc==abc_str[k]:
                count_lst[k] += 1
                break
    return count_lst

def addition(n):
    answer = 0
    for i in range(1,n+1):
        answer += i
    return answer

for i in range(n):
    string = s[i]
    anagram_lst.append(count_lst(string))

anagram_lst.sort()
count = 1
for i in range(n-1):
    anagram1 = anagram_lst[i]
    anagram2 = anagram_lst[i+1]
    if anagram1==anagram2:
        count +=1
        if i==n-2:
            number += addition(count-1)
    else:
        number += addition(count-1)
        count = 1

print(number)

Submission status: Accepted

Solution code:
n=int(input())
a=[list(input()) for i in range(n)]
for i in range(n):
    a[i]=sorted(a[i])
import collections
import math
d=collections.defaultdict(int)
for i in a:
    d[''.join(i)]+=1
ans=0
for i in d.values():
    if i>2:
        ans+=math.factorial(i)//(math.factorial(i-2)*math.factorial(2))
    elif i==2:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
M = {}
N = int(input())
ans = 0
for _ in range(N):
  S = input()
  S = ''.join(sorted(S))
  if S not in M:
    M[S] = 1
  else:
    ans += M[S]
    M[S] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = {}
count = 0

for i in range(n):
    s = ''.join(sorted(input()))
    if i == 0:
        l[s] = 1
        continue

    # ハッシュを確認し、登場していればcountを+l[s]、していなければハッシュに追加する
    if s in l:
        count+=l[s]
        l[s] += 1
    else:
        l[s] = 1

print (count)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
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()
    d = defaultdict(int)
    ans = 0
    for _ in range(N):
        S=str(sorted(ST()))
        ans += d[S]
        d[S]+=1
    print(ans)
        

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

 
a = []
x = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)

 
y = 0
for j in a:
    y += j*(j-1)/2
 
print(int(y))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l = ["".join(sorted(input())) for i in range(n)]
d = {}
cnt = 0

for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    cnt += d[l[i]]
    d[l[i]] += 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
L = []
for i in range(n):
  s = input()
  cur = []
  for j in range(10):
    cur.append(s[j])
  cur.sort()
  t = ''
  for j in range(10):
    t += cur[j]
  L.append(t)
L.sort()
ans = 0
L.append('end')
temp = 1
for i in range(1,n+1):
  if L[i] != L[i-1]:
    ans += temp*(temp-1)//2
    temp = 1
  else:
    temp += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
words = []
for i in range(N):
  s = str(input())
  s_sort = ''.join(sorted([ch for ch in s]))
  words.append(s_sort)

words_sort = sorted(words)

counter = 0
target_word = ''
counts = []
for word in words_sort:
  if word == target_word:
    counter += 1
  else:
    counts.append(counter)
    counter = 1
    target_word = word

counts.append(counter)

from math import factorial

ans = 0
for cnt in counts:
  if cnt > 1:
    ans += factorial(cnt) // factorial(cnt-2) // 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ss.sort()

ans = 0
while len(ss) != 0:
    f = ss.pop(0)
    c = 1
    while len(ss) > 0:
        d = ss[0]
        if f == d:
            ss.pop(0)
            ans += c
            c += 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=["".join(sorted(input())) for _ in range(n)]
D={}
for i in S:
    if i not in D:
        D[i]=1
    else:
        D[i]+=1
ans=0
for i in D.values():
    ans+=(i*(i-1))//2
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def comb_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

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

dic = defaultdict(int)
for x in S:
	dic[x] += 1

wa = 0
for n in dic.values():
	wa += int(n*(n - 1)/2)

print(wa)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
s = ["".join(sorted(input())) for _ in range(N)]
dic = defaultdict(int)
for x in s:
	dic[x] += 1
ans = 0
for n in dic.values():
	ans += n*(n-1)//2
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def comb_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = INT()
S = [input() for _ in range(N)]
for i in range(N):
	S[i] = sorted(S[i])
S = sorted(S)

ku = []
kugiri = 0
for i in range(N - 1):
	if S[i] == S[i + 1]:
		kugiri += 1
		if i == N - 2:
			ku.append(kugiri + 1)

	else:
		ku.append(kugiri + 1)
		kugiri = 0

wa = 0
for i in range(len(ku)):
	if ku[i] == 1:
		continue
	wa += comb_count(ku[i], 2)

print(wa)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  import sys
  import math
  import collections
  n=int(input())
  word=["".join(sorted(str(sys.stdin.readline().rstrip()))) for _ in range(n) ]
  dic=collections.Counter(word)
  ans=0
  for k in dic.values():
    if k>1:
      ans+=math.factorial(k)//math.factorial(k-2)//2
  print(ans)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
  import math
  import collections
  n=int(input())
  word=["".join(sorted(str(input()))) for _ in range(n) ]
  dic=collections.Counter(word)
  ans=0
  for k in dic.values():
    if k>1:
      ans+=math.factorial(k)//math.factorial(k-2)//2
  print(ans)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:


def main():
    N=int(input())
    r=[]
    for i in range(0,N):
        r.append(sorted(input()))
    r.sort()
    s=1
    ans=0
    for i in range(0,N-1):
        if r[i]==r[i+1]:
            s=s+1
        else:
            ans+=s*(s-1)//2
            s=1
    print(ans+s*(s-1)//2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def letternumber(S):
    alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    s=[0 for i in range(0,26)]
    for i in range(0,10):
        s[alphabet.index(S[i])]+=1
    return s

def main():
    N=int(input())
    r=[]
    for i in range(0,N):
        r.append(letternumber(input()))
    r.sort()
    s=1
    ans=0
    for i in range(0,N-1):
        if r[i]==r[i+1]:
            s=s+1
        else:
            ans+=s*(s-1)//2
            s=1
    print(ans+s*(s-1)//2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
d = {}
ans = 0
for i in range(N):
    s = input()
    s = "".join(sorted(list(s)))
    d.setdefault(s, -1)
    d[s] += 1
    ans += d[s]
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    from collections import defaultdict
    cnt = defaultdict(int)
    for _ in range(N):
        s = input()
        s = "".join(sorted(s))
        cnt[s] += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter
c = Counter(s_list)

ans = 0
for k, v in Counter(c.values()).items():
    ans += (k*(k-1)) / 2 * v
print(int(ans))

Submission status: Accepted

Solution code:
N=int(input())
s=[0]*N
essence={}
count=0
for i in range(N):
   s_i="".join(sorted(input()))
   s[i]=s_i
   if s[i] not in essence:
      essence.setdefault(s[i],1)
   else:
      count+=essence[s[i]]
      essence[s[i]]+=1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
S = ["".join(sorted(input())) for _ in range(n)]
ans = 0
dict = {}
for s in S:
    tmp = dict.get(s, 0)
    ans += tmp
    dict[s] = tmp + 1
print(ans)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

N = int(input())
s = [0]*(N+1)
for i in range(1, N+1):
    s[i] = input()

ss = {}
for i in range(1, N+1):
    now = "".join(sorted(list(s[i])))
    if now in ss:
        ss[now] += 1
    else:
        ss[now] = 1

ans = 0
for v in ss.values():
    if v >= 2:
        ans += v * (v-1) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = {}
for i in range(N):
  s = "".join(sorted(input()))
  try:
    S[s]+=1
  except:
    S[s]=1
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
res = dict()
for i in range(n):
    m = input()
    m = sorted(m)
    m = ''.join(m)
    if m in res:
        res[m] += 1
    else:
        res[m] = 1

for i in res:
  res[i] = res[i]*(res[i]-1)//2
print(sum(res.values()))

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
d = defaultdict(int)

for _ in range(N):
    d[tuple(sorted(input()))]+=1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=dict()
for i in range(n):
    s="".join(sorted(input()))
    if s in d:
        d[s]+=1
    else:
        d[s]=1
ans=0
for k in d.values():
    ans+=k*(k-1)//2
print(ans)

Submission status: Accepted

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

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

str_l.sort()
ans = 0
num = 0
st = "a"

for j in range(n):
    if str_l[j] == st:
        num += 1
        ans += num
    else:
        num = 0
        st = str_l[j]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
count_dict = {}
add_dict = {}
for _ in range(N):
    s = "".join(sorted(list(input())))
    if s in count_dict:
        count_dict[s] += add_dict[s]
        add_dict[s] += 1
    else:
        count_dict[s] = 0
        add_dict[s] = 1

print(sum(count_dict.values()))

Submission status: Accepted

Solution code:
def nC2(num):
  if num <= 1:
    return 0
  else:
    return int(num * (num-1) / 2)

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

count = 0
for text, c in text_dict.items():
  count += nC2(c)
  
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for s in ss:
    d[''.join(sorted(s))] += 1
ans = 0
for v in d.values():
    ans += v * (v-1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [str(sorted(input())) for _ in range(N)]
count = 0
for i in Counter(S).values():
  count += i*(i-1)//2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = "".join(s)
    a.setdefault(s, 0)
    a[s] += 1
count = 0
for i in a.values():
    count += i * (i - 1) // 2
print(count)

Submission status: Accepted

Solution code:
N = int(input())
from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    s = input()
    s = sorted(s)
    s = "".join(s)
    d[s] += 1

ans = 0
for s in d.keys():
    n = d[s]
    ans += n*(n-1)//2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = []
for i in range(n):
    s = input()
    li = sorted(list(s))
    S.append(li)
ans = 0
S.sort()
fac = S[0]
cnt = -1
for i in range(n):
    if S[i] == fac:
        cnt += 1
    else:
        fac = S[i]
        ans += max((cnt+1)*cnt//2,0)
        cnt = 0
ans += max((cnt+1)*cnt//2,0)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=dict()

for i in range(n):
  s="".join(sorted(input()))
  #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(ans)

Submission status: Accepted

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

dic = {}
for i in range(len(S)):
    if S[i] in dic: dic[S[i]] += 1
    else: dic[S[i]] = 1

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

Submission status: Accepted

Solution code:
from functools import lru_cache
from itertools import groupby

@lru_cache(maxsize=None)
def comb(n, r):
    if r == 1:
        return n
    if n < r:
        return 0
    return comb(n-1, r-1) + comb(n-1, r)

for n in range(10**5):
    comb(n, 2)

N = int(input())
S = tuple(sorted(tuple(sorted(input())) for _ in range(N)))

print(sum(comb(len(tuple(g)), 2) for _, g in groupby(S)))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
S = []
for _ in range(n):
    s = sorted(list(input()))
    S.append(''.join(s))

S_cnt = Counter(S)

ans = 0
for k,v in S_cnt.items():
    ans += v*(v-1)//2
print(ans)

Submission status: Accepted

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

# むずい！
anagrams = defaultdict(int)
for _ in range(N):
    chars = input()
    d = defaultdict(int)
    tuple_d = tuple(sorted(chars))
    anagrams[tuple_d] += 1

tmp = [i-1 for i in anagrams.values()]
print(sum([n*(n+1)//2 for n in tmp]))

Submission status: Accepted

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

for a in x:
  cnt+=x[a]*(x[a]-1)//2
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = [0]*n
count = 0
for i in range(n):
  a[i] = list(input())
  a[i].sort()
  d = ""
  for j in a[i]:
    d += j
  a[i] = d
c = collections.Counter(a)
for i in c.values():
  count += (i-1) * i // 2
print(count)

Submission status: Accepted

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

lssorted = [''] * n

for i in range(n):
	ls[i].sort()
	lssorted[i] = ''.join(ls[i])

lssorted.sort()


cnt = 1
ans = 0

for j in range(1, n):
	if lssorted[j] == lssorted[j - 1]:
		cnt += 1
		if j == n - 1:
			ans += cnt * (cnt - 1) / 2
	else:
		ans += cnt * (cnt - 1) / 2
		cnt = 1

print(int(ans))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	n = int(input())
	dd=defaultdict(lambda :0)
	for i in range(n):
		s = input()
		a=list(s)
		a.sort()
		s=''.join(a)
		dd[s]+=1
		
	ret=0
	for k in dd.keys():
		r=dd[k]
		ret += r*(r-1)//2
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
import math

n = int(input())
s = [0]*n
ans = {}
ans_v = 0

for i in range(n):
    s[i] = "".join(sorted(list(input())))
    
s_set = set(s)

for i in s_set:
    ans[i] = 0
    
for i in s:
    ans[i] += 1

for j in ans.values():
    if j >= 2:
        ans_v += math.factorial(j) /( (math.factorial(j - 2)* math.factorial(2)))
    
print(int(ans_v))

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 si in set(s):
    n = c[si]
    ans += n * (n - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = 0

for key in c:
    val = c[key]
    cnt += val*(val-1)//2

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
words = [[] for i in range(N)]
for i in range(N):
  s = input()
  l = []
  for t in s:
    l.append(t)
  l.sort()
  words[i] = l
words.sort()
#print(words)
materials = [1]
for i in range(1, N):
  if words[i] == words[i-1]:
    materials[-1] += 1
  else:
    materials.append(1)
print(sum([i * (i-1) // 2 for i in materials]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, s: "List[str]"):
    l = [''.join(sorted(list(i))) for i in s]
    from collections import Counter
    c = Counter(l)

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

    ans = 0
    for k, v in c.most_common():
        if v >= 2:
            ans += combination(v, 2)

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

def solve(n, S):
    counts = defaultdict(lambda: 0)
    for s in S:
        counts[''.join(sorted(list(s)))] +=  1
    total = 0
    for v in counts.values():
        if v > 1:
            total += int(v * (v - 1) / 2)
    return total

_n = int(input())
_S = [input() for _ in range(_n)]
print(solve(_n, _S))

Submission status: Accepted

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

def main():
    N = int(input())
    INF = 10**9+7
    h = []
    prime = [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]
    for _ in range(N):
        s = str(input())
        num = 1
        for i in range(10):
            num *= prime[ord(s[i])-97]
        h.append(num)
        
    c = collections.Counter(h)
    T = list(c.values())
    ans = 0
    for use in T:
        ans += (use*(use-1))//2
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
hantei_list = [1 for _ in range(n)]
moji_list = [str(input()) for _ in range(n)]
prime_number = [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]
for i in range(n):
  for j in range(10):
    hantei_list[i] *= prime_number[ord(moji_list[i][j])- ord('a')]

# unique = set(hantei_list)
appeared = dict()

counter = 0
for i in hantei_list:
  if not i in appeared:
    appeared[i] = 1
  else:
    counter += appeared[i]
    appeared[i] += 1
print(counter)

Submission status: Accepted

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

ans = 0
for _ in range(N):
  s = list(str(input()))
  string = "".join(sorted(s))
  if S.get(string) is None:
  	S[string] = 1
  else:
    ans += S[string]
    S[string] += 1
     
print(ans)

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

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


cnt = 0
tmpdic = {}
for i in s:
    tmp = "".join(sorted(i))
    if tmp in tmpdic.keys():
        cnt += tmpdic[tmp]
        tmpdic[tmp] += 1
    else:
        tmpdic[tmp] = 1


print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

S = []
for _ in range(N):
    tmp=sorted(input())
    S.append(tmp)

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

import collections
from math import factorial

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

print(int(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
import math

N = int(input())
li = [input() for i in range(N)]
d = defaultdict(int)

count = 0

for i in li:
  s = sorted(list(i))
  t = "".join(s)

  if d[t] != 0:
    count += d[t]
  d[t] += 1
    
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math

N = int(input())
li = [input() for i in range(N)]
d = defaultdict(int)

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

count = 0

for i in li:
  s = sorted(list(i))
  t = "".join(s)

  d[t] += 1

for i in d:
  if d[i] > 1:
    count += combinations_count(d[i], 2)
    
print(count)

Submission status: Accepted

Solution code:
# 2回目のソートに気づけずTLE、納得のアルゴリズム
n = int(input())
s = []
for _ in range(n): #文字列の各要素をソートしリストに格納
    #s.append(list(input())) # !!!
    #s.append(sorted(list(input()))) # !!!
    #s.append(sorted(input())) # !!!
    #s.append([''.join(sorted(input()))])
    s += [''.join(sorted(input()))] # appendよりも'+='の方が高速！
s = sorted(s) #さらにソートして全検索を避ける
#print(s)

a = []
x = 1
for i in range(1,n): #隣り合う要素が重複する数をリストに格納
    if s[i] == s[i-1]:
        x += 1
    else:
        a.append(x)
        x = 1
else:
    a.append(x)
#print(a)

y = 0
for j in a: #各重複する数から各組み合わせ数を返し合算
    y += j*(j-1)/2

print(int(y))