Problem p03593 (python)

Fixed submission:
from collections import Counter


H, W = map(int, input().split())
A = [input() for _ in range(H)]

char_to_cnt = Counter()
for h in range(H):
    for w in range(W):
        char_to_cnt.update(A[h][w])

if H % 2 != 0 and W % 2 != 0:
    g1 = 1
    g2 = H // 2 + W // 2
    g4 = (H // 2) * (W // 2)
else:
    g1 = 0
    if H % 2 == 0 and W % 2 == 0:
        g2 = 0
    else:
        if H % 2 == 0:
            g2 = H // 2
        else:
            g2 = W // 2
    g4 = (H * W - g1 - g2 * 2) // 4

for _ in range(g1):
    for char in char_to_cnt.keys():
        cnt = char_to_cnt[char]
        if cnt % 4 == 1 or cnt % 4 == 3:
            char_to_cnt[char] -= 1
            break

for _ in range(g2):
    for char in char_to_cnt.keys():
        cnt = char_to_cnt[char]
        if cnt % 4 == 2:
            char_to_cnt[char] -= 2
            break

for _ in range(g4):
    for char in char_to_cnt.keys():
        cnt = char_to_cnt[char]
        if cnt != 0 and cnt % 4 == 0:
            char_to_cnt[char] -= 4
            break

for _, cnt in char_to_cnt.items():
    if cnt % 4 != 0:
        print("No")
        exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import Counter


H, W = map(int, input().split())
A = [input() for _ in range(H)]

char_to_cnt = Counter()
for h in range(H):
    for w in range(W):
        char_to_cnt.update(A[h][w])

def check(table):
    for i in range(H):
        if "".join(table[i]) != "".join(reversed(table[i])):
            return False
    for j in range(W):
        row = [table[i][j] for i in range(H)]
        if "".join(row) != "".join(reversed(row)):
            return False
    return True



ans = False
def dfs(i, j, table, remain):
    global ans
    if ans:
        return

    # print(i, j, table, remain)
    for char, cnt in remain.items():
        if cnt == 0:
            continue

        new_table = list(table)
        new_table[i][j] = char

        if i >= H // 2:
            if new_table[i][j] != new_table[(H - i - 1)][j]:
                continue

        if j >= W // 2:
            if new_table[i][j] != new_table[i][(W - j - 1)]:
                continue

        if i == H - 1 and j == W - 1:
            if check(new_table):
                ans = True
            return

        new_remain = Counter(remain)
        new_remain[char] -= 1

        if j == W - 1:
            dfs(i + 1, 0, new_table, new_remain)
        else:
            dfs(i, j + 1, new_table, new_remain)

table = [["" for _ in range(W)] for i in range(H)]
dfs(0, 0, table, char_to_cnt)

if ans:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
from collections import Counter


H, W = map(int, input().split())
A = [input() for _ in range(H)]

char_to_cnt = Counter()
for h in range(H):
    for w in range(W):
        char_to_cnt.update(A[h][w])

def check(table):
    for i in range(H):
        if "".join(table[i]) != "".join(reversed(table[i])):
            return False
    for j in range(W):
        row = [table[i][j] for i in range(H)]
        if "".join(row) != "".join(reversed(row)):
            return False
    return True

ans = False
def dfs(i, j, table, remain):
    global ans
    if ans:
        return
    # print(i, j, table, remain)
    for char, cnt in remain.items():
        if cnt == 0:
            continue

        new_table = list(table)
        new_table[i][j] = char
        if i == H - 1 and j == W - 1:
            if check(new_table):
                ans = True
            return

        new_remain = Counter(remain)
        new_remain[char] -= 1

        if j == W - 1:
            dfs(i + 1, 0, new_table, new_remain)
        else:
            dfs(i, j + 1, new_table, new_remain)

table = [["" for _ in range(W)] for i in range(H)]
dfs(0, 0, table, char_to_cnt)

if ans:
    print("Yes")
else:
    print("No")