Problem: 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.

Submission attempts (in order):
N = int(input())

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

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

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

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

    return result

m = {}

def sort_str(s):
    return "".join(sorted(list(s)))

for i in range(N):
    s = str(input())
    ss = sort_str(s)

    if ss in m:
        m[ss] += 1
    else:
        m[ss] = 1


count = 0

for c in m.values():
  	if c > 1:
    	count += cmb(c, 2)

print(count)

N = int(input())

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

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

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

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

    return result

m = {}

def sort_str(s):
    return "".join(sorted(list(s)))

for i in range(N):
    s = str(input())
    ss = sort_str(s)

    if ss in m:
        m[ss] += 1
    else:
        m[ss] = 1


count = 0

for c in m.values():
    count += cmb(c, 2)

print(count)

N = int(input())

arr = []
for i in range(N):
    arr.append(str(input()))

count = 0

def sort_str(s):
    return "".join(sorted(list(s)))

for i, item in enumerate(arr):
    s = sort_str(item)
    for j in range(i+1, N):
        print(s, sort_str(arr[j])) 
        if sort_str(arr[j]) == s:
            count += 1

print(count)

N = int(input())

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

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

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

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

    return result

m = {}

def sort_str(s):
    return "".join(sorted(list(s)))

for i in range(N):
    s = str(input())
    ss = sort_str(s)

    if ss in m:
        m[ss] += 1
    else:
        m[ss] = 1


count = 0

for c in m.values():
    if c > 1:
        count += cmb(c, 2)

print(count)

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Accepted