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 math
from collections import Counter

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

N = int(input())
counter = Counter([''.join(sorted(input())) for i in range(N)])
ans = 0
for k, v in counter.items():
    if v == 1:
        continue
    ans += combinations_count(v, 2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input().strip())

counter = Counter()

for i in range(N):
  s = input().strip()
  counter[''.join(sorted(s))] += 1

total = 0
for k, v in counter.items():
  total += v * (v - 1) // 2

print(total)

Submission status: Accepted

Solution code:
N = int(input())
s_list = [input() for _ in range(N)]
from collections import defaultdict
def combination(n, r):
    """
    :param n: the count of different items
    :param r: the number of select
    :return: combination
    n! / (r! * (n - r)!)
    """
    r = min(n - r, r)
    result = 1
    for i in range(n, n - r, -1):
        result *= i
    for i in range(1, r + 1):
        result //= i
    return result

di = defaultdict(int)
for S in s_list:

    tmp = defaultdict(int)
    for s in S:
        tmp[s] += 1
    T = ""
    tmp = sorted(tmp.items())
    for t, t2 in tmp:
        T += t+ str(t2)
    di[T] += 1
ans = 0
for k, v in di.items():
    if v > 1:
        ans += combination(v, 2)
print(ans)

Submission status: Accepted

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

ans = 0
cnt = 1
for i in range(n-1):
    # print(sn[i], cnt, ans)
    if sn[i] == sn[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())

s_list = []
for i in range(N):
	s_list.append(input().strip())

ss_list = sorted(["".join(sorted(s)) for s in s_list])

total = 0
count = 1
start = 0
prev = None
for i in range(N):
	s = ss_list[i]
	if prev == s:
		count += 1
	else:
		if count > 1:
			total += count * (count - 1) // 2
		count = 1
	prev = s

if count > 1:
	total += count * (count - 1) // 2

print(total)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import collections
n=int(input())
S=[]
append=S.append
for i in range(n):
  s=input()
  ss=0
  for j in range(10):
    ss+=11**(ord(s[j])-97)
  append(ss)
SS=collections.Counter(S).values()
ans=0
for ss in SS:
  if ss>1:
    ans+=(ss-1)*ss//2
print(ans)

Submission status: Accepted

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

for i in range(n):
    w=list(input())
    w.sort()
    s="".join(w)
    if s in d:
        d[s]+=1
    else:
        d[s]=1
ans=0
for i in d:
    ans+=d[i]*(d[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [sys.stdin.readline()[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

d = defaultdict(int)

N = I()
S = SR(N)

for s in S:
    d[str(sorted(s))] += 1

ans = 0
for key in d.keys():
    value = d[key]
    if value >= 2 :
        ans += value*(value-1)//2
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def hash(S) :
    ret = 0
    for s in S :
        ret *= 26
        ret += ord(s)-97
    return ret
        
s = [hash(sorted(input())) for _ in range(N)]

s.sort()
s.append(-1)

count = 1
ret = 0
for i in range(1, N+1) :
    if s[i] == s[i-1] :
        count += 1
    else :
        if count >= 2 :
            ret += count * (count-1) // 2
        count = 1
        
print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
N=int(input())
d=defaultdict(lambda: 0)
for _ in range(N):
    s="".join(sorted(list(input())))
    d[s]+=1
ans=0
for _, num in d.items():
    ans+=(num*(num-1))
print(ans//2)

Submission status: Accepted

Solution code:
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
index=0
hashMap={}
while index < n:
    listS = list(input())
    listS.sort()
    key = "".join(listS)
    if key in hashMap:
        hashMap[key]+=1
    else:
        hashMap[key]=1
    index+=1
answer = 0
for key in hashMap:
    if hashMap[key] > 1:
        answer+=comb(hashMap[key], 2)
print(answer)

Submission status: Accepted

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

from collections import Counter
c = Counter()
for s in S:
    c[s] += 1

ans = 0

def h(n):
    return (n * (n - 1))//2
for k,v in c.items():
    ans += h(v)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [0] * N
from collections import Counter
counter = Counter()
for i in range(N):
    k = "".join(sorted(input().strip()))
    counter[k] += 1

# 4
answer = 0
for k, c in counter.items():
    f = 1
    if c == 1:
        continue
    answer += (c * (c-1)) // 2
print(answer)

Submission status: Accepted

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

from collections import Counter
c = Counter(S_)

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

  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for i in range(n):
    a = list(input())
    a.sort()
    a = str(a)
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
ans = 0
for i in d:
    ans += d[i]*(d[i]-1)//2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for i in range(N)]
from collections import defaultdict
d=defaultdict(int)
for s in S:
    s=''.join(sorted([c for c in s]))
    d[s]+=1
a=0
for v in d.values():
    if v>1:
        a+=v*(v-1)//2
print(a)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
t=int(raw_input())
f=defaultdict(int)
while(t!=0):
    t=t-1
    s=(raw_input())
    s=sorted(list(s))
    f["".join(s)]+=1
ans=0
for i in f:
    p=f[i]
    ans+=((p)*(p-1))/2
print ans

Submission status: Accepted

Solution code:
n = int(input())
ss = []
dic = dict()
for i in range(n):
    s = input().rstrip()
    t = "".join(sorted(s))
    if t in dic:
        dic[t] += 1
    else:
        dic[t] = 1
ans = 0
for key in dic.keys():
    ans += (dic[key] * (dic[key]-1)) // 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

C = [[0]*26 for i in range(N)]

for i in range(N):
    for j in range(len(S[i])):
        C[i][ord(S[i][j])-97]+=1

from collections import defaultdict as dd

dic=dd(int)

for i in range(N):
    dic[" ".join(map(str,C[i]))]+=1

ans = 0

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

print(ans)

Submission status: Accepted

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

for i in range(n):
    s[i] = [s[i][j] for j in range(len(s[i]))]
    s[i].sort()
    s[i] = ''.join(s[i])

s.sort()
s.append("x")
ans = 0
x = 0
y = "x"
for i in range(len(s)):
    if s[i] == y:
        x += 1
    else:
        ans += x * (x-1) // 2
        y = s[i]
        x = 1

print(ans)

Submission status: Accepted

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

import collections
a = []
for i in range(n):
    xx = list(s[i])
    xx.sort()

    a.append(''.join(xx))

c = collections.Counter(a)

ans = 0
for k in c.keys():
    v = c[k]
    ans += v * (v - 1) // 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s={}
for _ in range(n):
  b=tuple(sorted([i for i in input()]))
  if b in s:
    s[b]+=1
  else:
    s[b]=1
k=0
for i in s:
  j=s[i]
  k+=j*(j-1)//2
print(k)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
dct = defaultdict(int)

for i in range(n):
    s = input()
    sorted_s = "".join(sorted(s))
    dct[sorted_s] += 1

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
INF = float("inf")


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

    numer = [n - r + k + 1 for k in range(r)]
    denom = [k + 1 for k in range(r)]

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

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

    return result


def solve(N: int, s: "List[str]"):
    for i in range(N):
        buf = list(s[i])
        buf.sort()
        s[i] = "".join(buf)
    s.sort()
    # print(s, sep="\n")
    c = Counter(s)
    # print(c)
    tot = 0
    for k in c:
        if c[k] >= 2:
            tot += cmb(c[k], 2)
    print(tot)

    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:
import collections
n=int(input())
l=[list(input()) for i in range(n)]
for i in range(n):
  l[i].sort()
  l[i]=''.join(l[i])
l=collections.Counter(l)
m=list(l.values())
ct=0
for i in range(len(m)):
  ct+=int(m[i]*(m[i]-1)//2)
print(ct)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
S = [''.join(sorted(input())) for _ in range(N)]
d = defaultdict(lambda:0)
for s in S:
    d[s] += 1
ans = 0
for s in d:
    n = d[s]
    ans += n*(n-1)//2

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, S: "List[str]"):
    p = []
    count = {}
    for i, s in enumerate(S):
        s = list(s)
        s.sort()
        s = ''.join(s)
        if s in count:
            count[s] += 1
        else:
            count[s] = 1
        p.append(s)
    ret = 0
    #for v in p:
    #    print(count[s])
    #    ret += len(count[s])
    for c in count.values():
        ret += c * (c - 1) // 2
    print(ret)
    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 Counter

n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
c = Counter(s)
ans = 0
for x in c.values():
    if x >= 2:
        ans += x*(x-1)//2

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from collections import defaultdict

n = ni()
dic = defaultdict(int)

for _ in range(n):
    s = ns()
    cnts = [0]*26
    for si in s:
        cnts[ord(si)-ord('a')] += 1

    dic[tuple(cnts)] += 1

ans = 0
for _, v in dic.items():
    ans += v*(v-1) // 2

print(ans)

Submission status: Accepted

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    n = int(input())
    sl = []
    for _ in range(n):
        s = list(input())
        s.sort()

        sl.append(''.join(s))

    counter = Counter(sl)
    ans = 0
    for s, c in counter.items():
        if c > 1:
            ptn = c * (c-1) // 2
            ans += ptn
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
def choose2(n):
    return n * (n-1) // 2

n = int(input())
counts = defaultdict(int)

for i in range(n):
    norm = ''.join(sorted(input()))
    counts[norm] += 1

pairs = 0
for c in counts.values():
    if c >= 2:
        pairs += choose2(c)
print(pairs)

Submission status: Accepted

Solution code:
n=int(input())
from collections import Counter
ans=0
s=Counter(["".join(sorted(input())) for _ in [0]*n]).values()
for i in s:
    ans+=i*(i-1)//2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = dict()
for i in range(n):
    s = input()
    t = sorted(s)
    t = "".join(t)
    try:
        d[t] += 1
    except:
        d[t] = 1
ans = 0
for i in d.keys():
    ans += (d[i] * (d[i]-1))//2
print(ans)

Submission status: Accepted

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

for _ in range(N):
    C[sum(1<<(4*(ord(i)-97)) for i in input().strip())] += 1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__

from collections import Counter


def main():
    n = int(input())
    c = Counter()
    for i in range(n):
        c[''.join(sorted(input()))] += 1

    cnt = 0
    for val in c.values():
        cnt += (val * (val - 1)) >> 1

    print(cnt)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0,
                              2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
cnt = defaultdict(int)
for _ in range(N):
    s = [c for c in input()]
    s.sort()
    a = ''.join(s)
    cnt[a] += 1

ans = 0
for c, f in cnt.items():
    ans += f * (f - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
G = {}
p = 31
M1, M2 = (10 ** 9 + 7, 998244353)
for _ in range(n):
    s = input()
    s = sorted(list(s))
    yt = 0
    yt2 = 0
    for i in range(len(s)):
        yt += p ** (i + 1) * (ord(s[i]) - 95)
        yt2 += p ** (i + 1) * (ord(s[i]) - 95)
        yt %= M1
        yt2 %= M2
    if (yt, yt2) not in G:
        G[(yt, yt2)] = 0
    G[(yt, yt2)] += 1
ans = 0
for i in G:
    ans += G[i] * (G[i] - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def solve(N: int, s: "List[str]"):
    c = Counter()
    for x in s:
        c["".join(sorted(x))] += 1
    ans = 0
    for i in c.values():
        ans += i * (i - 1) // 2
    return ans


# -----------------------------------------------------------------------------
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]"

    ans = solve(N, s)
    if ans is not None:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def solve(N: int, s: "List[str]"):
    s.sort()
    count = 0
    res = 0
    prev = ""
    for string in s:
        if string != prev:
            prev = string
            res += count * (count+1) // 2
            count = 0
        else:
            count += 1
    res += count * (count+1) // 2    
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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 = [ sorted(next(tokens)) for _ in range(N) ]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
arr=[input() for _ in range(n)]
dic={}
check='abcdefghijklmnopqrstuvwxyz'
for s in arr:
  count=[0]*26
  for i in range(10):
    for j in range(26):
      if s[i]==check[j]:
        count[j]+=1
        break
  count=tuple(count)
  if count not in dic:
    dic[count]=1
  else:
    dic[count]+=1
ans=0
for val in dic.values():
  ans+=val*(val-1)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

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

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

print(sum_)

Submission status: Accepted

Solution code:
N = int(input())

freq_sorted_word = {}
for i in range(N):
    sorted_word = ''.join(sorted(input()))
    freq_sorted_word.setdefault(sorted_word, 0)
    freq_sorted_word[sorted_word] += 1

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

print(sum(map(pair_num, freq_sorted_word.values())))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for _ in range(N):
    s = "".join(sorted(input()))
    d[s] += 1

res = 0
for k in d.keys():
    res += d[k] * (d[k] - 1) // 2

print(res)

Submission status: Accepted

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

import collections


def count_anagrams(n, ss):
    ctr = collections.Counter("".join(sorted(s)) for s in ss)
    res = sum(v * (v - 1) // 2 for v in ctr.values())
    return res


def main():
    n = int(input())
    ss = [input() for _ in range(n)]
    res = count_anagrams(n, ss)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

N = int(input())
A = []
for _ in range(N):
    a = sorted(list(input()))
    A.append(''.join(a))

B = Counter(A)

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

Submission status: Accepted

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

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

print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
s = [raw_input() for _ in range(N)]

s = sorted([sorted(t) for t in s])
ans = 0
i = 0
while i<N:
  j = i
  while j<N and s[i]==s[j]:
    j += 1
  ans += (j-i)*(j-i-1)/2
  i = j
print ans

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ans = 0
dic = {}
for i in range(n):
    s = input()
    c = Counter(s)
    tmp = ''.join([k+str(v) for k,v in sorted(c.items())])
    if tmp not in dic:
        dic[tmp] = 1
    else:
        ans += dic[tmp]
        dic[tmp] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

print(ans)

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
S = [list(sys.stdin.readline().rstrip()) for _ in range(N)]

for s in S:
    s.sort()
S.sort()
ans = 0
cnt = 0
for s, t in zip(S, S[1:]):
    if s == t:
        cnt += 1
    else:
        if cnt > 0:
            ans += cnt * (cnt + 1) // 2
        cnt = 0
if cnt > 0:
    ans += cnt * (cnt + 1) // 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

base = ord('a')

d = defaultdict(int)

ans = 0

n = int(input())
for _ in range(n):
    s = input()
    lst = [0] * 26
    for c in s:
        idx = ord(c) - base
        lst[idx] += 1
    key = tuple(lst)
    ans += d[key]
    d[key] += 1

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    print(max(a+b,a-b,a*b))
    return

#B
def B():
    k,x = LI()
    ans = []
    for i in range(x-k+1,x+k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
n = int(input())
s = [sorted(input()) for i in range(n)]
s = sorted(s) + ["a"]

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

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for _ in range(n):
    s = ''.join(sorted(input()))
    if s not in d:
        d[s] = 1
    else:
        d[s] += 1
res = sum((v * (v - 1)) // 2 for v in d.values())
print(res)

Submission status: Accepted

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

N = int(input())

A = [input().strip() for i in range(N)]

B = []
for a in A:
    NOW=[0]*26

    for k in a:
        NOW[ord(k)-97]+=1

    B.append(NOW)
B.sort()
NOW=1
ANS=0
for i in range(1,N):
    if B[i]==B[i-1]:
        NOW+=1
    else:
        ANS+=NOW*(NOW-1)//2
        NOW=1

ANS+=NOW*(NOW-1)//2

print(ANS)

Submission status: Accepted

Solution code:
def dic(s):
  dics,ans=[],""
  for i in range(len(s)):dics.append(s[i])
  dics.sort()
  for i in dics:ans+=i
  return ans
n=int(input())
s=[dic(input())for _ in range(n)]
s.sort()
a=b=1
c=""
for i in range(n):
  if c!=s[i]:
    b+=a*(a-1)//2
    a=1
    c=s[i]
  else:a+=1
b+=a*(a-1)//2
print(b-1)

Submission status: Accepted

Solution code:
N = int(input())
D = {}
for _ in range(N):
    S = "".join(sorted(input()))
    if S in D:
        D[S] += 1
    else:
        D[S] = 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

stdin = sys.stdin

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

n = ni()
dic = defaultdict(int)
for i in range(n):
    s = ns()
    q = list(s)
    q.sort()
    q = "".join(q)
    dic[q] += 1

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

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

d = {}

ans = 0
for i in s:
    a = [c for c in i]
    a.sort()
    a = "".join(a)
    
    if a in d:
        ans += d[a]
        d[a] += 1
    else:
        d[a] = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(' '.join(sorted(t)) for t in s)
ans = sum(v * (v - 1) // 2 for v in c.values())

print(ans)

Submission status: Accepted

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

Submission status: Runtime Error

Solution code:
def resolve():
    n = int(input())
    words = []
    for _ in range(n):
        words.append(str(sorted(list(input()))))
    a = collections.Counter(words)
    ans = 0
    for i in a.values():
        ans += i*(i-1)//2
    print(ans)
resolve()

Submission status: Runtime Error

Solution code:
#他の人の回答
import collections

N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]　#sortedでリストになった文字列をjoinでもう一度文字列に
c = collections.Counter(s)　　#cは辞書の形をとる
ans = 0
for si in set(s):　# sから重複を無くす
    n = c[si]
    ans += n * (n - 1) // 2   # n C 2を計算
print(ans)

Submission status: Runtime Error

Solution code:
import itertools,math
n = int(input())
word = [input() for _ in range(n)]
count = 0

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

for i in range(n):
    word[i] = sorted(word[i])

new_word = list(sorted(word))

x = 1

for i in range(n-1):
    if new_word[i] == new_word[i+1]:
        x += 1
    else:
        count += combinations_count(x,2)
        x = 1
if x >= 2:
    count +=  combinations_count(x,2)
    
print(count)

Submission status: Runtime Error

Solution code:
n = input()
s = [''.join(sorted(input())) for i in range(n)]
count = 0
for i in range(n):
  for j in range(i+1,n):
    if s[j][0]==s[i][0]:
      count+=1
print(count)

Submission status: Runtime Error

Solution code:
n = input()
s = [''.join(sorted(input())) for i in range(n)]
count = 0
for i in range(n):
  for j in range(i+1,n):
    if s[j]==s[i]:
      count+=1
print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = input()
s = [''.join(sorted(input())) for i in range(n)]
d = Counter(s)
count = 0
for i in d.values():
  if i !=1:
    count+=i
print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = input()
s = [''.join(list(input()).sort()) for i in range(n)]
d = Counter(s)
count = 0
for i in d.values():
  if i !=1:
    count+=i
print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = input()
s = [list(input()).sort() for i in range(n)]
d = Counter(s)
count = 0
for i in d.values():
  if i !=1:
    count+=i
print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = input()
s = [list(input()).sort() for i in range(n)]
d = {a: len(a) for a in s}
count = 0
for i in d.values():
  if i !=1:
    count+=i
print(count)