alexjercan/bugnet huggingface dataset:
Problem p02941 (Python) — buggy submission, verdict: Time Limit Exceeded

from heapq import heapify, heappop, heappush


def solve(n, aaa, bbb):
    q = [(-b, i) for i, b in enumerate(bbb) if b != aaa[i]]
    heapify(q)
    ans = 0
    while q:
        b, i = heappop(q)
        b = -b - aaa[i]
        d, b = divmod(b, bbb[(i - 1) % n] + bbb[(i + 1) % n])
        if d == 0:
            return -1
        b += aaa[i]
        bbb[i] = b
        ans += d
        if b != aaa[i]:
            heappush(q, (-b, i))
    return ans


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print(solve(n, aaa, bbb))


Error: TLE

Fixed submission:

from heapq import heapify, heappop, heappush


def solve(n, aaa, bbb):
    q = [(-b, i) for i, b in enumerate(bbb) if b != aaa[i]]
    heapify(q)
    ans = 0
    while q:
        b, i = heappop(q)
        b = -b
        b -= bbb[(i - 1) % n] + bbb[(i + 1) % n]
        if b <= 0:
            return -1
        bbb[i] = b
        ans += 1
        if b != aaa[i]:
            heappush(q, (-b, i))
    return ans


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print(solve(n, aaa, bbb))
Problem p02945 (Python) — buggy submission, verdict: Runtime Error

A, B = map(int, input().split())
c = A + B
d = A - B
e = A * B
f = [c, d, e]
maxvalue = c
for i in range(len(f)):
    if f[i] > maxvalue:
        maxvalue = f[i]
print(maxvalue)


Error: ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s027556545.py", line 1, in <module>
    A = int(input())
ValueError: invalid literal for int() with base 10: '-13 3'


Fixed submission:

A = int(input())
B = int(input())
c = A + B
d = A - B
e = A * B
f = [c, d, e]
maxvalue = c
for i in range(len(f)):
    if f[i] > maxvalue:
        maxvalue = f[i]
print(maxvalue)
Problem p02945 (Python) — buggy submission, verdict: Runtime Error

A, B = map(int, input().split())
print(max(A + B, A - B, A * B))


Error: ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s567458637.py", line 1, in <module>
    N = int(input())
ValueError: invalid literal for int() with base 10: '-13 3'


Fixed submission:

N = int(input())
s = [list(input()) for _ in range(N)]
S = []
for i in range(N):
    S.append("".join(sorted(s[i])))
print(S)

cnt = 0
for i in range(N):
    cnt += S.count(S[i]) - 1
print(int(cnt / 2))
Problem p02945 (Python) — buggy submission, verdict: Runtime Error

A, B = map(int, input().split())
list = []
list.append(A + B)
list.append(A - B)
list.append(A * B)
print(max(list))


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s147643407.py", line 1, in <module>
    A, B = int(input().split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'


Fixed submission:

A, B = int(input().split())
list = []
list.append(A + B)
list.append(A - B)
list.append(A * B)
print(max(list))
Problem p02945 (Python) — buggy submission, verdict: Runtime Error

data = input()
data = data.split(" ")

A = int(data[0])
B = int(data[1])

answer = max(
    A + B,
    A - B,
    A * B,
)

print(answer)


Error: ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s283924824.py", line 1, in <module>
    A = int(input())
ValueError: invalid literal for int() with base 10: '-13 3'


Fixed submission:

A = int(input())
B = input()

answer = max(
    A + B,
    A - B,
    A * B,
)

print(answer)
Problem p02947 (Python) — buggy submission, verdict: Runtime Error

# じゃあ全部保存していってやるよ
# atcoderのpython3のバージョンが古すぎて(3.4.3)TypeError: unhashable type: 'list'なるのでため息つきながら直した

n = int(input())
d = {}
arr = []
for _ in range(n):
    x = "".join(list(map(str, list(sorted(input())))))
    arr.append(x)
    if x not in d:
        d[x] = 1
    else:
        d[x] += 1

c = 0
for a in arr[:0:-1]:
    if d[a] > 0:
        c += d[a] - 1
        d[a] -= 1

print(c)


Error: TypeError: unhashable type: 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02947/Python/s642649306.py", line 8, in <module>
    if x not in d:
TypeError: unhashable type: 'list'


Fixed submission:

# じゃあ全部保存していってやるよ
n = int(input())
d = {}
arr = []
for _ in range(n):
    x = list(sorted(input()))
    arr.append(x)
    if x not in d:
        d[x] = 1
    else:
        d[x] += 1

c = 0
for a in arr[:0:-1]:
    if d[a] > 0:
        c += d[a] - 1
        d[a] -= 1

print(c)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter
from math import factorial


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


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

t = ["".join(sorted(str)) for str in s]
c = Counter(t)
ll = [i for _, i in c.items() if i > 1]
cnt = 0
for i in ll:
    cnt += count_combinations(i, 2)

print(cnt)


Error: TLE

Fixed submission:

from collections import Counter
from itertools import combinations

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

cnt = 0
for t, u in combinations(s, 2):
    if Counter(t) == Counter(u):
        cnt += 1

print(cnt)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

n = int(input())
counter = collections.Counter(str(sorted(input())) for _ in range(n))
combination_count = sum((v - 1) * v // 2 for v in counter.values())

print(combination_count)


Error: TLE

Fixed submission:

def to_dict(text):
    d = {}
    for c in text:
        d[c] = d.get(c, 0) + 1
    return d


n = int(input())
combination_count = 0
s = []
for _ in range(n):
    si = to_dict(input())
    combination_count += sum(1 for old_si in s if old_si == si)
    s.append(si)

print(combination_count)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


N = int(input())
c = Counter()
for _ in range(N):
    si = "".join(sorted(input()))
    c[si] += 1

ans = sum([n * (n - 1) // 2 for n in c.values()])
print(ans)


Error: TLE

Fixed submission:

N = int(input())
s = []
for _ in range(N):
    si = sorted(input())
    s.append(si)
ans = 0
for i in range(0, N - 1):
    for j in range(i + 1, N):
        if s[i] == s[j]:
            ans += 1
print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


def main():
    N = int(input())
    s = ["".join(sorted(input())) for _ in range(N)]
    S = Counter(s)
    ans = 0
    for a in S.values():
        ans += sigma(a)
    print(ans)


def sigma(x):
    sum = 0
    for i in range(1, x):
        sum += i
    return sum


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

from collections import Counter


def main():
    N = int(input())
    s = [Counter(input()) for _ in range(N)]
    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            if s[i] == s[j]:
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

n = int(input())
S = []
count = 0
for _ in range(n):
    S.append("".join(sorted(list(input()))))

for i, v in collections.Counter(S).items():
    for i in range(1, v):
        count += i

print(count)


Error: TLE

Fixed submission:

from collections import defaultdict

n = int(input())
d = []
count = 0
t = [0] * n
for _ in range(n):
    s = input()
    dic = defaultdict(int)
    for c in s:
        dic[c] += 1
    for i in range(len(d)):
        if all(dic[key] == d[i][key] for key in dic):
            if t[i] != 0:
                count += t[i] + 1
                t[i] += 1
                break
            else:
                count += 1
                t[i] += 1
    d.append(dic)


print(count)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import collections
import math


def main():
    N = int(input())
    S = ["".join(sorted(input())) for i in range(N)]
    cnt = collections.Counter(S)
    ans = 0
    for v in cnt.values():
        if v != 1:
            ans += math.factorial(v) // (math.factorial(v - 2) * 2)

    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    N = int(input())
    S = [sorted(input()) for i in range(N)]
    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            if S[i] == S[j]:
                ans += 1

    print(ans)


if __name__ == "__main__":
    main()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
import collections
import bisect


def main():
    n = int(input())
    s = ["".join(sorted(list(input()))) for _ in range(n)]
    c = collections.Counter(s)
    cVals = c.values()
    cSum = 0
    for cVal in cVals:
        cSum += cVal * (cVal - 1) // 2
    print(cSum)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

n = int(input())
cnt = 0
sListSum = 0
sList = [None] * n

for i in range(n):
    s = sorted(input())
    sListSum += sList.count(s)
    sList[i] = s

print(sListSum)
Problem p02947 (Python) — buggy submission, verdict: Runtime Error

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)


Error: 0

Fixed submission:

import math

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(math.factorial(v) / 2)

print(answer)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

# coding: utf-8
from collections import Counter

n = int(input())
lst = [input() for i in range(n)]
cnt = Counter()
ans = 0
for i in lst:
    a = "".join(sorted(list(i)))
    if a in cnt:
        ans += cnt[a]
    cnt[a] += 1

print(ans)


Error: TLE

Fixed submission:

# coding: utf-8
n = int(input())
lst = [input() for i in range(n)]
cnt = 0
for j in range(n - 1):
    for k in range(j + 1, n):
        if sorted(list(lst[j])) == sorted(list(lst[k])):
            cnt += 1

print(cnt)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
# 整数の入力


def main():
    # a, b
    n = int(input())
    s = []
    for _ in range(n):
        s.append(input())
    print(process(n, s))


def process(n, s):
    s = list(map(lambda s: "".join(sorted(s)), s))
    hash = {}

    count = 0
    for i in range(0, n):
        if s[i] in hash:
            hash[s[i]] += 1
        else:
            hash[s[i]] = 1
        count += hash[s[i]] - 1
    return count


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-
# 整数の入力


def main():
    # a, b
    n = int(input())
    s = []
    for _ in range(n):
        s.append(sorted(input()))
    print(process(n, s))


def process(n, s):
    count = 0
    for i in range(0, n):
        for k in range(i, n):
            if i == k:
                continue
            if s[i] == s[k]:
                count += 1
    return count


if __name__ == "__main__":
    main()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
from collections import defaultdict


def main():
    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)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
from itertools import combinations


def main():
    N = int(input())
    S = [sorted(input()) for _ in range(N)]
    ptn = list(combinations(S, 2))
    result = 0

    for p in ptn:
        result += all([p[0] == p[1]])
    print(result)


if __name__ == "__main__":
    main()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

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

dic = {}
cnt = 0

for moji in moji_lst:
    x = "".join(sorted(moji))

    if x in dic:
        dic[x] += 1
        cnt += dic[x]
    else:
        dic[x] = 0


print(cnt)


Error: TLE

Fixed submission:

import collections
import itertools

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

check = [[0] * N for _ in range(N)]


cnt = 0
for i in range(N):
    c = []
    x = collections.Counter(moji[i])

    c.append(i)

    for j in range(i + 1, N):
        if check[i][j] == 1:
            continue

        y = collections.Counter(moji[j])

        if x == y:
            c.append(j)

    if len(c) > 1:
        for u, v in itertools.combinations(c, 2):
            check[u][v] = 1
            cnt += 1

print(cnt)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    n = int(input())
    s = ["".join(sorted(input())) for _ in range(n)]
    ans = 0
    dic = {}
    for i in s:
        if i not in dic:
            dic[i] = 0
        else:
            dic[i] += 1
            ans += dic[i]
        # print(ans)
    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import itertools


def main():
    n = int(input())
    s = [sorted(input()) for _ in range(n)]
    seq = list(itertools.combinations(s, 2))
    ans = 0
    for i in seq:
        if i[0] == i[1]:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

S = []
for _ in range(n):
    s = list(input())
    s.sort()
    S.append("".join(s))
S.sort()

C = []
c = 1
for i in range(n - 1):
    if S[i] == S[i + 1]:
        c += 1
    else:
        C.append(c)
        c = 1
C.append(c)

ans = 0
for c in C:
    ans += c * (c - 1) // 2

print(ans)


Error: TLE

Fixed submission:

n = int(input())

S = []
for _ in range(n):
    s = list(input())
    s.sort()
    S.append("".join(s))
S.sort()

C = []
i = 0
while i < n:
    c = S.count(S[i])
    if c > 1:
        C.append(c)
    i += c

ans = 0
for c in C:
    ans += c * (c - 1) // 2

print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

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 k, v in d.items():
    ans += (v * (v - 1)) / 2

print(int(ans))


Error: TLE

Fixed submission:

n = int(input())
strs = list()
for _ in range(n):
    strs.append(sorted(list(input())))

total = 0
for i in range(n):
    for j in range(i + 1, n):
        if strs[i] == strs[j]:
            total += 1
print(total)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())

L = [list(input()) for _ in range(N)]

dic = []
flag = {}
res = 0
for i in range(N):
    L[i].sort()

    if str(L[i]) in flag:
        flag[str(L[i])] += 1
        res += flag[str(L[i])]
    else:
        dic.append(L[i])
        flag[str(L[i])] = 0

print(res)


Error: TLE

Fixed submission:

N = int(input())

L = [list(input()) for _ in range(N)]

dic = []
flag = {}
res = 0
for i in range(N):
    L[i].sort()

    if L[i] in dic:
        flag[str(L[i])] += 1
        res += flag[str(L[i])]
    else:
        dic.append(L[i])
        flag[str(L[i])] = 0

print(res)
Problem p02947 (Python) — buggy submission, verdict: Runtime Error

from math import factorial

N = int(input())
d = {}
for _ in range(N):
    s = input()
    s_sorted = "".join(sorted(s))
    if not d.get(s_sorted):
        d[s_sorted] = 1
    else:
        d[s_sorted] += 1

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

print(round(ans))


Error: 0

Fixed submission:

from math import factorial

N = int(input())
d = {}
for _ in range(N):
    s = input()
    s_sorted = "".join(sorted(s))
    if not d.get(s_sorted):
        d[s_sorted] = 1
    else:
        d[s_sorted] += 1

ans = 0
for _, v in d.items():
    if v == 1:
        continue
    x = factorial(v) / factorial(2) / factorial(v - 2)
    ans += x

print(round(ans))
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

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

cnt = Counter(S)
print(sum(v * (v - 1) // 2 for v in cnt.values()))


Error: TLE

Fixed submission:

from itertools import combinations

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

print(sum(1 if s1 == s2 else 0 for s1, s2 in combinations(S, 2)))
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

n = int(input())
x = ["".join(sorted(input())) for _ in range(n)]
a = collections.Counter(x)
print(sum(i * ~-i // 2 for i in a.values()))


Error: TLE

Fixed submission:

x = []
cnt = 0
for _ in range(int(input())):
    a = sorted(input())
    cnt += x.count(a)
    x.append(a)
print(cnt)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
import collections

n = int(input())
lst = [None] * n
for i in range(n):
    lst[i] = "".join(sorted(list(sys.stdin.readline().strip())))

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


Error: TLE

Fixed submission:

import sys

n = int(input())
alphabet = [chr(ord("a") + i) for i in range(26)]
count = [[0] * 26 for _ in range(n)]
for i in range(n):
    for s in sys.stdin.readline().strip():
        count[i][alphabet.index(s)] += 1

uniq = set(list(map(tuple, count)))
ans = 0
for u in uniq:
    n = count.count(list(u))
    ans += n * (n - 1) // 2
print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict
import sys

readline = sys.stdin.readline

N = int(input())

result = 0
d = defaultdict(int)
for _ in range(N):
    s = input()
    s = "".join(sorted(s))
    if s in d:
        d[s] += 1
    else:
        d[s] = 1
print(sum([int((v) * (v - 1) / 2) for k, v in d.items()]))


Error: TLE

Fixed submission:

from collections import defaultdict

N = int(input())

result = 0
store = []
for _ in range(N):
    d = defaultdict(int)
    s = input()
    for c in set(s):
        d[c] += s.count(c)
    result += store.count(d)
    store.append(d)
print(result)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
dic = {}
ans = 0
for _ in range(N):
    si = input()
    sortedSi = "".join(sorted(si))
    if sortedSi in dic:
        dic[sortedSi] += 1
    else:
        dic[sortedSi] = 1

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

print(ans)


Error: TLE

Fixed submission:

N = int(input())
s = []
ans = 0
for _ in range(N):
    si = input()
    sortedSi = "".join(sorted(list(si)))
    ans += s.count(sortedSi)
    s.append(sortedSi)

print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

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


for i in range(n):
    s[i].sort()
    s[i] = "".join(s[i])

c = Counter(s)

ans = 0

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


Error: TLE

Fixed submission:

from collections import Counter

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

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            ans += 1

print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

def resolve():
    N = int(input())
    s = [input() for _ in range(N)]
    ans = 0
    chk = {}
    for tmp in s:
        d1 = "".join(sorted(list(tmp)))
        if d1 in chk.keys():
            ans += chk[d1]
            chk[d1] += 1
        else:
            chk[d1] = 1
    print(ans)


resolve()


Error: TLE

Fixed submission:

import itertools
import collections


def resolve():
    N = int(input())
    s = [input() for _ in range(N)]
    ans = 0
    for tmp in itertools.combinations(s, 2):
        d1 = collections.defaultdict(int)
        d2 = collections.defaultdict(int)
        for ch in tmp[0]:
            d1[ch] += 1
        for ch in tmp[1]:
            d2[ch] += 1
        if d1 == d2:
            ans += 1
    print(ans)


resolve()
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

# https://atcoder.jp/contests/abc137/tasks/abc137_c
# C - Green Bin
import collections
import itertools
import math

n = int(input().split()[0])
s_list = []
c_list = []

for _ in range(n):
    s = input().split()[0]
    s_list.append("".join(sorted(s)))

counter = collections.Counter(s_list)


def C(n, k):
    return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))


p_list = [C(value, 2) for key, value in counter.items() if value > 1]
ans = int(sum(p_list))

print(ans)


Error: TLE

Fixed submission:

# https://atcoder.jp/contests/abc137/tasks/abc137_c
# C - Green Bin
import collections
import itertools

n = int(input().split()[0])
s_list = []
c_list = []

for _ in range(n):
    s = input().split()[0]
    s_list.append(s)
    c_list.append(collections.Counter(s))

p = itertools.combinations(c_list, 2)
ans = sum([1 if a == b else 0 for a, b in list(p)])
print(ans)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

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

num = 0
num_same = 1
prev = sorted_s[0]
for i in range(1, len(sorted_s)):
    if prev != sorted_s[i]:
        num += int(num_same * (num_same - 1) / 2)
        num_same = 1
        prev = sorted_s[i]
    else:
        num_same += 1
num += int(num_same * (num_same - 1) / 2)
print(num)


Error: TLE

Fixed submission:

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


def is_anagram(s_i, s_j):
    for k in range(10):
        if s_i[k] != s_j[k]:
            return False
    return True


num = 0
for i in range(n):
    for j in range(i + 1, n):
        if is_anagram(s[i], s[j]):
            num += 1
print(num)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

s = [""] * n
t = [""] * n

cnt = 0
flg = 0

for i in range(n):
    s[i] = input()
    t[i] = sorted(s[i])

t = sorted(t)

for i in range(n - 1):
    if t[i] == t[i + 1]:
        flg += 1
        cnt += flg
    else:
        flg = 0

print(cnt)


Error: TLE

Fixed submission:

n = int(input())

s = [""] * n
t = [""] * n

cnt = 0

for i in range(n):
    s[i] = input()
    t[i] = sorted(s[i])

t = sorted(t)

for i in range(n - 1):
    for j in range(i + 1, n):
        if t[i] == t[j]:
            cnt += 1

print(cnt)
Problem p02947 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

anagrams = defaultdict(int)
for _ in range(int(input())):
    anagrams[str(sorted(input()))] += 1

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

print(int(total))


Error: TLE

Fixed submission:

from collections import Counter

anagrams = []
for _ in range(int(input())):
    count = Counter(input())
    found = False
    for v in anagrams:
        if v["count"] == count:
            found = True
            v["total"] += 1
            break
    if not found:
        anagrams.append({"count": count, "total": 1})

total = 0
for v in anagrams:
    n = v["total"]
    total += n * (n - 1) / 2

print(int(total))