Problem:


Score : 700 points

Problem Statement

There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter.
Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i.

Snuke can apply the following operation to this grid any number of times:

Choose two different rows and swap them. Or, choose two different columns and swap them.

Snuke wants this grid to be symmetric.
That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal.

Determine if Snuke can achieve this objective.

Constraints

1 \leq H \leq 12

1 \leq W \leq 12

|S_i| = W

S_i consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

If Snuke can make the grid symmetric, print YES; if he cannot, print NO.

Sample Input 1

2 3
arc
rac

Sample Output 1

YES

If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:

Sample Input 2

3 7
atcoder
regular
contest

Sample Output 2

NO

Sample Input 3

12 12
bimonigaloaf
faurwlkbleht
dexwimqxzxbb
lxdgyoifcxid
ydxiliocfdgx
nfoabgilamoi
ibxbdqmzxxwe
pqirylfrcrnf
wtehfkllbura
yfrnpflcrirq
wvcclwgiubrk
lkbrwgwuiccv

Sample Output 3

YES



Sample input:
2 3
arc
rac


Sample output:
YES

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def can_palindrome(s):
    l = []
    for c in s:
        if c in l:
            l.remove(c)
        else:
            l.append(c)

    if len(s) % 2 == 0:
        return len(l) == 0
    else:
        return len(l) == 1


def can_pair(s1, s2):
    l = []
    for c1, c2 in zip(s1, s2):
        c = sorted([c1, c2])
        if c in l:
            l.remove(c)
        else:
            l.append(c)

    if len(s1) % 2 == 0:
        return len(l) == 0
    else:
        return len(l) == 1 and l[0][0] == l[0][1]


def pair_graph(s1, s2):
    N = len(s1)
    G = np.zeros([N, N])
    for i in range(N):
        for j in range(i+1, N):
            if s1[i] == s2[j] and s1[j] == s2[i]:
                G[i, j] = 1
                G[j, i] = 1
    return G

import numpy as np
H, W = list(map(int, input().split(' ')))
S = []
d = defaultdict(list)

for h in range(H):
    s = input()
    S.append(s)
    d[''.join(sorted(S))].append(h)

already_paired = np.zeros(H)

Gs = []

for i in range(H):
    if already_paired[i]:
        continue

    for j in range(i+1, H):
        if already_paired[j]:
            continue
    
        if can_pair(S[i], S[j]):
            already_paired[i] = 1
            already_paired[j] = 1
            G = pair_graph(S[i], S[j])
            Gs.append(G)
            break

if already_paired.sum() < H - 1:
    print('NO')
    exit()

elif already_paired.sum() == H - 1:
    for i in range(H):
        if already_paired[i] == 0:
            Gs.append(pair_graph(S[i], S[i]))
            break

def is_perfect_matching(G):
    N = len(G)
    if N == 1:
        return True
    for i in range(N):
        for j in range(i+1, N):
            if G[i, j] == 1:
                l = list(range(N))
                l.remove(i)
                l.remove(j)
                if len(l) == 0:
                    return True
                if is_perfect_matching(G[l][:, l]):
                    return True
    return False

if is_perfect_matching(G):
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

Solution code:
H, W = (int(i) for i in input().split())
S = []

for i in range(H):
    S.append(list(input()))

ST = [[0] * H for i in range(W)]

for i in range(H):
    for j in range(W):
        ST[j][i] = S[i][j]

for s in S:
    s.sort()
for st in ST:
    st.sort()

m = 0
for i in range(H - 1):
    for j in range(i + 1 - m, H - 2 * m):
        if S[i - m] == S[j]:
            S.pop(i - m)
            S.pop(j - 1)
            m += 1
            break
    if len(S) <= 1:
        break
m = 0
for i in range(W - 1):
    for j in range(i + 1 - m, W - 2 * m):
        if ST[i - m] == ST[j]:
            ST.pop(i - m)
            ST.pop(j - 1)
            m += 1
            break
    if len(ST) <= 1:
        break

m = 0
if len(S) == 1:
    for i in range(W - 1):
        if S[0][i - m] == S[0][i + 1 - m]: 
            S[0].pop(i - m)
            S[0].pop(i - m)
            m += 1
        if len(S[0]) <= 1:
            break
m = 0
if len(ST) == 1:
    for i in range(H - 1):
        if ST[0][i - m] == ST[0][i + 1 - m]: 
            ST[0].pop(i - m)
            ST[0].pop(i - m)
            m += 1
        if len(ST[0]) <= 1:
            break

a = 0 if W % 2 == 0 else 1
b = 0 if H % 2 == 0 else 1
if len(S) <= b and len(ST) <= a :
    print("YES")
else:
    if len(S[0]) <= a and len(ST[0]) <= b:
        print("YES")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
for i in range(H):
    S.append(list(input()))

ST = [[0] * H for i in range(W)]

for i in range(H):
    for j in range(W):
        ST[j][i] = S[i][j]

for s in S:
    s.sort()
for st in ST:
    st.sort()

m = 0
for i in range(H - 1):
    for j in range(i + 1 - m, H - 2 * m):
        if S[i - m] == S[j]:
            S.pop(i - m)
            S.pop(j - 1)
            m += 1
            break
    if len(S) <= 1:
        break
m = 0
for i in range(W - 1):
    for j in range(i + 1 - m, W - 2 * m):
        if ST[i - m] == ST[j]:
            ST.pop(i - m)
            ST.pop(j - 1)
            m += 1
            break
    if len(ST) <= 1:
        break

m = 0
if len(S) == 1:
    for i in range(W - 1):
        if S[0][i - m] == S[0][i + 1 - m]: 
            S[0].pop(i - m)
            S[0].pop(i - m)
            m += 1
        if len(S[0]) <= 1:
            break
m = 0
if len(ST) == 1:
    for i in range(H - 1):
        if ST[0][i - m] == ST[0][i + 1 - m]: 
            ST[0].pop(i - m)
            ST[0].pop(i - m)
            m += 1
        if len(ST[0]) <= 1:
            break

a = 0 if W % 2 == 0 else 1
b = 0 if H % 2 == 0 else 1
if len(S) <= b and len(ST) <= a :
    print("YES")
else:
    if len(S[0]) <= a and len(ST[0]) <= b:
        print("YES")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter

H,W = map(int,input().split())

grid = np.array([list(map(lambda c: ord(c)-ord('a'), input())) for _ in range(H)], dtype='u1')


if H%2 == 1 and W%2 == 1:
  s = None
  for a,b in Counter(grid.flat).items():
    if b%2 == 1:
      if s is not None:
        print('NO')
        exit()
      s = a

  # check x
  xflag = False
  for i in range(W):
    for a,b in Counter(grid[i,:]).items():
      if b%2 != (a == s):
        break
    else:
      xflag = True
      break

  yflag = False
  for i in range(H):
    for a,b in Counter(grid[:,i]).items():
      if b%2 != (a == s):
        break
    else:
      yflag = True
      break

  print('YES' if xflag and yflag else 'NO')

elif H%2==1 or W%2 == 1:
  if W%2 == 1:
    grid = grid.T


  for a,b in Counter(grid.flat).items():
    if b%2 == 1:
      print('NO')
      exit()

  flag = False

  for i in range(W):
    even = 0
    odd = 0
    for a,b in Counter(grid[i,:]).items():
      odd += b%2
    if odd == 0:
      flag = True
      break

  print('YES' if flag else 'NO')

else:
  for a,b in Counter(grid.flat).items():
    if b%2 == 1:
      print('NO')
      exit()
  print('YES')

Submission status: Runtime Error

Solution code:
import numpy as np

H,W = map(int,input().split())

grid = np.array([list(map(lambda c: ord(c)-ord('a'), input())) for _ in range(H)], dtype='u1')


if H%2 == 1 and W%2 == 1:
  s = None
  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      if s is not None:
        print('NO')
        exit()
      s = a

  # check x
  xflag = False
  for i in range(W):
    for a,b in zip(*np.unique(grid[i,:], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      xflag = True
      break

  yflag = False
  for i in range(H):
    for a,b in zip(*np.unique(grid[:,i], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      yflag = True
      break

  print('YES' if xflag and yflag else 'NO')

elif H%2==1 or W%2 == 1:
  if W%2 == 1:
    grid = grid.T


  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()

  flag = False

  for i in range(W):
    even = 0
    odd = 0
    for a,b in zip(*np.unique(grid[i,:], return_counts=True)):
      odd += b%2
    if odd == 0:
      flag = True
      break

  print('YES' if flag else 'NO')

else:
  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()
  print('YES')

Submission status: Runtime Error

Solution code:
import numpy as np

H,W = map(int,input().split())

grid = np.array([list(map(lambda c: ord(c)-ord('a'), input())) for _ in range(H)], dtype='u1')


if H%2 == 1 and W%2 == 1:
  s = None
  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      if s is not None:
        print('NO')
        exit()
      s = a

  # check x
  xflag = False
  for i in range(W):
    for a,b in zip(*np.unique(grid[i,:], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      xflag = True
      break

  yflag = False
  for i in range(H):
    for a,b in zip(*np.unique(grid[:,i], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      yflag = True
      break

  print('YES' if xflag and yflag else 'NO')

elif H%2==1 or W%2 == 1:
  if W%2 == 1:
    grid = grid.T


  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()

  flag = False

  for i in range(W):
    even = 0
    odd = 0
    for a,b in zip(*np.unique(grid[:,i], return_counts=True)):
      odd += b%2
    if odd == 1:
      flag = True
      break

  print('YES' if flag else 'NO')

else:
  for a,b in zip(*np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()
  print('YES')

Submission status: Runtime Error

Solution code:
import numpy as np

H,W = map(int,input().split())

grid = np.array([list(map(lambda c: ord(c)-ord('a'), input())) for _ in range(H)], dtype='u1')


if H%2 == 1 and W%2 == 1:
  s = None
  for a,b in zip(np.unique(grid, return_counts=True)):
    if b%2 == 1:
      if s is not None:
        print('NO')
        exit()
      s = a

  # check x
  xflag = False
  for i in range(W):
    for a,b in zip(np.unique(grid[i,:], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      xflag = True
      break

  yflag = False
  for i in range(H):
    for a,b in zip(np.unique(grid[:,i], return_counts=True)):
      if b%2 != (a == s):
        break
    else:
      yflag = True
      break

  print('YES' if xflag and yflag else 'NO')

elif H%2==1 or W%2 == 1:
  if W%2 == 1:
    grid = grid.T


  for a,b in zip(np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()

  flag = False

  for i in range(W):
    even = 0
    odd = 0
    for a,b in zip(np.unique(grid[:,i], return_counts=True)):
      odd += b%2
    if odd == 1:
      flag = True
      break

  print('YES' if flag else 'NO')

else:
  for a,b in zip(np.unique(grid, return_counts=True)):
    if b%2 == 1:
      print('NO')
      exit()
  print('YES')

Submission status: Runtime Error

Solution code:
# E
import numpy as np

H, W = map(int, input().split())
S = list()
for _ in range(H):
    S.append(list(input()))
S_arr = np.array(S)

res = "YES"

def check_ok(S_arr, H, W):
    res = True
    for i in range(H):
        for j in range(W):
            if S_arr[i, j] != S_arr[H-1-i, W-1-j]:
                res = False
    return res


def apply_switch(S_arr, row_matching, col_matching):
    S_arr_row = S_arr.copy()
    i = 0
    for mat in row_matching:
        if len(mat) == 2:
            S_arr_row[i, :] = S_arr[mat[0], :]
            S_arr_row[H-1-i, :] = S_arr[mat[1], :]
            i += 1
        else:
            S_arr_row[(H-1)//2, :] = S_arr[mat[0], :]
    S_arr_rowcol = S_arr_row.copy()
    j = 0
    for mat in row_matching:
        if len(mat) == 2:
            S_arr_rowcol[:, j] = S_arr_row[:, mat[0]]
            S_arr_rowcol[:, W-1-j] = S_arr_row[:, mat[1]]
            j += 1
        else:
            S_arr_rowcol[:, (W-1)//2] = S_arr_row[:, mat[0]]
    
# row_matching
same_row_list = []
done_row_list = np.zeros(H)
for i in range(H):
    if done_row_list[i] == 1:
        continue
    done_row_list[i] = 1
    j_list = [i]
    for j in range(H):
        if i != j and np.prod(np.sort(S_arr[i, :]) == np.sort(S_arr[j, :])) == 1:
            j_list.append(j)
            done_row_list[j] = 1
    same_row_list.append(j_list)

# col_matching
same_col_list = []
done_col_list = np.zeros(W)
for i in range(W):
    if done_col_list[i] == 1:
        continue
    done_col_list[i] = 1
    j_list = [i]
    for j in range(W):
        if i != j and np.prod(np.sort(S_arr[:, i]) == np.sort(S_arr[:, j])) == 1:
            j_list.append(j)
            done_col_list[j] = 1
    same_col_list.append(j_list)
    
if len(same_row_list) == H // 2 and len(same_col_list) == W // 2:
    if check_ok(apply_switch(S_arr, same_row_list, same_col_list), H, W) == False:
        res = "NO"
elif len(same_row_list) > H // 2:
    res = "NO"
elif len(same_col_list) > W // 2:
    res = "NO"
print(res)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
s = [input() for _ in range(H)]
if H == 1 and W == 1:
    print('YES')
else:

    d_h = [[0 for i in range(25)] for _ in range(H)]
    d_w = [[0 for i in range(25)] for _ in range(W)]

    for i in range(H):
        for c in s[i]:
            d_h[i][ord(c) - ord("a")] += 1

    for i in range(H):
        for j in range(W):
            d_w[j][ord(s[i][j]) - ord("a")] += 1
    #
    # for i in range(H):
    #     d_h[i].sort()
    #     d_w[i].sort()

    check_h = []
    check_w = []

    for i in range(H):
        if i in check_h:
            continue
        for j in range(i + 1, H):
            if j in check_h:
                continue
            else:
                if d_h[i] == d_h[j]:
                    check_h.append(i)
                    check_h.append(j)
                    break

    for i in range(W):
        if i in check_w:
            continue
        for j in range(i + 1, W):
            if j in check_w:
                continue
            else:
                if d_w[i] == d_w[j]:
                    check_w.append(i)
                    check_w.append(j)
                    break

    c_w = 0
    c_h = 0

    for i in range(H):
        if i in check_h:
            c_h += 1
    for i in range(W):
        if i in check_w:
            c_w += 1

    if c_h >= H - 1 and c_w >= W - 1:
        
        print('YES')
    else:
        print('NO')

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = list(map(int, input().split(' ')))
S = []
d = defaultdict(list)

for h in range(H):
    s = input()
    S.append(s)
    d[''.join(sorted(S))].append(h)

already_paired = np.zeros(H)

Gs = []

for i in range(H):
    if already_paired[i]:
        continue

    for j in range(i+1, H):
        if already_paired[j]:
            continue

        if can_pair(S[i], S[j]):
            already_paired[i] = 1
            already_paired[j] = 1
            G = pair_graph(S[i], S[j])
            Gs.append(G)

if already_paired.sum() < H - 1:
    print('NO')
    exit()

elif already_paired.sum() == H - 1:
    for i in range(H):
        if already_paired[i] == 0:
            Gs.append(pair_graph(S[i], S[i]))
            break

G = np.array(Gs).sum(axis=0) // len(Gs)

def is_perfect_matching(G):
    N = len(G)
    for i in range(N):
        for j in range(i+1, N):
            if G[i, j] == 1:
                l = list(range(N))
                l.remove(i)
                l.remove(j)
                if len(l) == 0:
                    return True
                if is_perfect_matching(G[l][:, l]):
                    return True
    return False

if is_perfect_matching(G):
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split(' ')))
S = []
d = defaultdict(list)

for h in range(H):
    s = input()
    S.append(s)
    d[''.join(sorted(S))].append(h)

already_paired = np.zeros(H)

Gs = []

for i in range(H):
    if already_paired[i]:
        continue

    for j in range(i+1, H):
        if already_paired[j]:
            continue

        if can_pair(S[i], S[j]):
            already_paired[i] = 1
            already_paired[j] = 1
            G = pair_graph(S[i], S[j])
            Gs.append(G)

if already_paired.sum() < H - 1:
    print('NO')

elif already_paired.sum() == H - 1:
    for i in range(H):
        if already_paired[i] == 0:
            Gs.append(pair_graph(S[i], S[i]))
            break

G = np.array(Gs).sum(axis=0) // len(Gs)

def is_perfect_matching(G):
    N = len(G)
    for i in range(N):
        for j in range(i+1, N):
            if G[i, j] == 1:
                l = list(range(N))
                l.remove(i)
                l.remove(j)
                if len(l) == 0:
                    return True
                if is_perfect_matching(G[l][:, l]):
                    return True
    return False

print(is_perfect_matching(G))

Submission status: Runtime Error

Solution code:
# E
import numpy as np

H, W = map(int, input().split())
S = list()
for _ in range(H):
    S.append(list(input()))
S_arr = np.array(S)

res = "YES"

def check_ok(S_arr, H, W):
    res = True
    for i in range(H):
        for j in range(W):
            if S_arr[i, j] != S_arr[H-1-i, W-1-j]:
                res = False
    return res


def apply_switch(S_arr, row_matching, col_matching):
    S_arr_row = S_arr.copy()
    i = 0
    for mat in row_matching:
        if len(mat) == 2:
            S_arr_row[i, :] = S_arr[mat[0], :]
            S_arr_row[H-1-i, :] = S_arr[mat[1], :]
            i += 1
        else:
            S_arr_row[(H-1)//2, :] = S_arr[mat[0], :]
    S_arr_rowcol = S_arr_row.copy()
    j = 0
    for mat in row_matching:
        if len(mat) == 2:
            S_arr_rowcol[:, j] = S_arr_row[:, mat[0]]
            S_arr_rowcol[:, W-1-j] = S_arr_row[:, mat[1]]
            j += 1
        else:
            S_arr_rowcol[:, (W-1)//2] = S_arr_row[:, mat[0]]
    
# row_matching
same_row_list = []
done_row_list = np.zeros(H)
for i in range(H):
    if done_row_list[i] == 1:
        continue
    done_row_list[i] = 1
    j_list = [i]
    for j in range(H):
        if i != j and np.prod(np.sort(S_arr[i, :]) == np.sort(S_arr[j, :])) == 1:
            j_list.append(j)
            done_row_list[j] = 1
    same_row_list.append(j_list)

# col_matching
same_col_list = []
done_col_list = np.zeros(W)
for i in range(W):
    if done_col_list[i] == 1:
        continue
    done_col_list[i] = 1
    j_list = [i]
    for j in range(W):
        if i != j and np.prod(np.sort(S_arr[:, i]) == np.sort(S_arr[:, j])) == 1:
            j_list.append(j)
            done_col_list[j] = 1
    same_col_list.append(j_list)
    
if len(same_col_list) == H // 2 and len(same_col_list) == W // 2:
    if check_ok(apply_switch(S_arr, same_row_list, same_col_list), H, W) == False:
        res = "NO"
print(res)

Submission status: Runtime Error

Solution code:
n, m = map(int,input().split())
d = {i: {chr(i): 0 for i in range(ord('a'), ord('z')+1)} for i in range(n)}
d2 = {i: {chr(i): 0 for i in range(ord('a'), ord('z')+1)} for i in range(m)}
for i in range(n):
    s = input()
    for j in s:
        d[i][j] += 1
    for j in range(len(s)):
        d2[j][s[j]] += 1

k = [0 for i in range(n)]
k2 = [0 for i in range(m)]

f = True
for i in range(n):
    for j in range(n):
        if d[i] == d[j]:
            k[i] += 1


for i in range(m):
    for j in range(m):
        if d2[i] == d2[j]:
            k2[i] += 1

k2 = sorted(k2)
k = sorted(k)
if n % 2:
    if k[1] < 2:
        f = False
else:
    if k[0] < 2:
        f = False

if m % 2:
    if k2[1] < 2:
        f = False
else:
    if k2[0] < 2:
        f = False

if f:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count<2 and count2<2:
    #if count==0 and count2==0:
    if H==W:
        raise
    #    raise
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = list(map(int, input().split()))
S = np.array([list(input()) for _ in range(H)])

def main(S):
    u, c = np.unique(S, return_counts=True)
    if all([i%2==1 for i in S.shape]):
        if np.sum(c%2) != 1:
            print("NO")
            return
    else:
        if np.sum(c%2) >= 1:
            print("NO")
            return
    print("YES")

main(S)

Submission status: Runtime Error

Solution code:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

int main() {
	int H, W;
	std::string s[13], t[13];

	cin >> H >> W;
	for(int i = 0; i < H; ++i) {
		cin >> s[i];
	}

	for(int i = 0; i < W; ++i) {
		for(int j = 0; j < H; ++j) {
			t[i] += s[j][i];
		}
		std::sort(t[i].begin(), t[i].end());
	}

	for(int i = 0; i < H; ++i) {
		std::sort(s[i].begin(), s[i].end());
	}

	std::map<std::string, int> hori, vert;
	int cnth = H, cntv = W, oddh = 0, oddv = 0;

	for(int i = 0; i < H; ++i) {
		hori[s[i]]++;
		//cout << s[i] << " " << hori[s[i]] << endl;
		if(hori[s[i]] == 2) {
			oddh--;
			cnth -= 2;
			hori[s[i]] -= 2;
		}
		if(hori[s[i]] == 1) {
			oddh++;
		}
	}

	if(H % 2 == 0 && cnth > 0 || H % 2 == 1 && cnth != 1) {
		cout << "NO" << endl;
		return 0;
	}
	cnth -= oddh;

	for(int i = 0; i < W; ++i) {
		vert[t[i]]++;
		//cout << t[i] << " " << vert[t[i]] << endl;
		if(vert[t[i]] == 2) {
			oddv--;
			cntv -= 2;
			vert[t[i]] -= 2;
		}
		if(vert[t[i]] == 1) {
			oddv++;
		}
	}

	if(W % 2 == 0 && cntv > 0 || W % 2 == 1 && cntv != 1) {
		cout << "NO" << endl;
	}
	cntv -= oddv;

	if(cntv == cnth && cntv == 0) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}

	return 0;
}

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = list(map(int, input().split()))
S = np.array([list(input()) for _ in range(H)])

u, c = np.unique(S, return_counts=True)
if all([i%2==1 for i in S.shape]):
    if np.sum(c%2) != 1:
        print("NO")
        exit()
else:
    if np.sum(c%2) >= 1:
        print("NO")
        exit()
print("YES")

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count<2 and count2<2:
    if count==0 and count2==0:
        raise
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = list(map(int, input().split()))
S = np.array([list(input()) for _ in range(H)])

u, c = np.unique(S, return_counts=True)
if all([i%2==1 for i in S.shape]):
    if np.sum(c%2) != 1:
        print("NO")
else:
    if np.sum(c%2) >= 1:
        print("NO")
print("YES")

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count<2 and count2<2:
    if count2==1:
        raise
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count<2 and count2<2:
    if count==1:
        raise
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]
#print(X)

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1
if not H%2 ==count%2:
    raise
for key in counted2:
    if counted2[key]%2:
        count2+=1

if W%2 == count2%2:
    raise
if count<2 and count2<2:
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]
#print(X)

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count==1 and count2==1:
    raise

elif count<2 and count2<2:
    print("YES")
else:
    print("NO")

#print(count,count2)

Submission status: Runtime Error

Solution code:
H,W = list(map(int,(input().split())))

sosuu = list(map(int,'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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349'.split()))

X = [[ 0 for _ in range(W) ]for _ in range(H)]

for i in range(H):
    for j,s in enumerate(input()):
        X[i][j] = sosuu[ord(s) - 93]
#print(X)

from functools import reduce
data = []
data2 = []
for j in range(W):
    data.append((reduce(lambda x,y:x*y,list(X[i][j] for i in range(H)))))
for i in range(H):
    data2.append((reduce(lambda x,y:x*y,list(X[i][j] for j in range(W)))))

import collections
counted = collections.Counter(data)
counted2 = collections.Counter(data2)
count = 0
count2= 0
for key in counted:
    if counted[key]%2:
        count+=1

for key in counted2:
    if counted2[key]%2:
        count2+=1

if count<2 and count2<2:
    print("YES")
    raise
else:
    print("NO")

#print(count,count2)