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: Wrong Answer

Solution code:
import numpy as np

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)}

lst = []
for i in range(n):
    s = input()
    lst.append(s)
    for j in s:
        d[i][j] += 1
    for j in range(len(s)):
        d2[j][s[j]] += 1

matrix = np.matrix(lst)

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

ii = -1
k_0 = []
k_1 = []
k2 = sorted(k2)
k = sorted(k)
if n % 2:
    if len(k) > 1:
        if k[1] < 2:
            f = False

    for i in range(n):
        fl = False
        for j in range(n):
            if j != i:
                if d[i] == d[j]:
                    fl = True
        if not fl:
            ii = i
    if ii != -1:
        s0 = lst[ii]
        if len(s0) % 2:
            for i in s0:
                k_0.append(s0.count(i))
            k_0 = sorted(k_0)
            if len(k_0) > 1:
                for i in range(1, len(k_0)):
                    if k_0[i] % 2:
                        f = False
        else:
            for i in s0:
                k_0.append(s0.count(i))
            k_0 = sorted(k_0)
            for i in range(len(k_0)):
                if k_0[i] % 2:
                    f = False

else:
    if k[0] < 2:
        f = False

ii = -1

if m % 2:
    if len(k) > 1:
        if k2[1] < 2:
            f = False

    for i in range(m):
        fl = False
        for j in range(m):
            if j != i:
                if d2[i] == d2[j]:
                    fl = True
        if not fl:
            ii = i
    if ii != -1:
        s0 = ''
        for i in range(n):
            s0 += lst[i][ii]

        if len(s0) % 2:
            for i in s0:
                k_1.append(s0.count(i))
            k_1 = sorted(k_1)
            if len(k_1) > 1:
                for i in range(1, len(k_1)):
                    if k_1[i] % 2:
                        f = False
        else:
            for i in s0:
                k_1.append(s0.count(i))
            k_1 = sorted(k_1)
            for i in range(len(k_1)):
                if k_1[i] % 2:
                    f = False

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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)}

lst = []
for i in range(n):
    s = input()
    lst.append(s)
    for j in s:
        d[i][j] += 1
    for j in range(len(s)):
        d2[j][s[j]] += 1

matrix = np.matrix(lst)

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

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

    for i in range(n):
        fl = False
        for j in range(n):
            if j != i:
                if d[i] == d[j]:
                    fl = True
        if not fl:
            ii = i
    if lst[ii] != lst[ii][::-1]:
        f = False

else:
    if k[0] < 2:
        f = False

if m % 2:
    if len(k) > 1:
        if k2[1] < 2:
            f = False


    for i in range(m):
        fl = False
        for j in range(m):
            if j != i:
                if d2[i] == d2[j]:
                    fl = True
        if not fl:
            ii = i
    s0 = ''
    for i in range(n):
        s0 += lst[i][ii]

    if s0 != s0[::-1]:
        f = False

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

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

Submission status: Wrong Answer

Solution code:
print("YES")

Submission status: Wrong Answer

Solution code:
_in_ = input().split(" ")
H,M = int(_in_[0]),int(_in_[1])

col =[]
_row = []
for i in range (M):
    _row.append([])

for i in range(H):
    temp =list(input())
    col.append(str(sorted(temp)))
    for j in range (M):
        _row[ j ].append(temp[ j ])
        
row = []
for i in range(M):
    row.append(str(sorted(_row[i])))

r_cnt = 0
s_row = list(set(row))
for _row_ in s_row:
    r_cnt += row.count(_row_)%2

c_cnt =0
s_col = list(set(col))
for _col_ in s_col:
    c_cnt += col.count(_col_)%2

if(c_cnt >1 or r_cnt>1):
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
_in_ = input().split(" ")
H,M = int(_in_[0]),int(_in_[1])

col =[]
_row = []
for i in range (M):
    _row.append([])

for i in range(H):
    temp =list(input())
    col.append(sorted(temp))
    for j in range (M):
        _row[ j ].append(temp[ j ])
        
row = []
for i in range(M):
    row.append(sorted(_row[i]))

r_cnt = 0
for _row_ in row:
    r_cnt += row.count(_row_)%2

c_cnt =0
for _col_ in col:
    c_cnt += col.count(_col_)%2

if(c_cnt >1 or r_cnt>1):
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

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

word = {}

for i in range(H):
    S = input()

    for l in range(W):
        if S[l] in word:
            word[S[l]] += 1
        else:
            word[S[l]] = 1

flg = 0

for key in word.values():
    if key % 2 == 1:
        flg += 1
        
    
if flg == 0:
    print('YES')
elif H * W % 2 == 1 and flg == 1:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

word = {}

for i in range(H):
    S = input()

    for l in range(W):
        if S[l] in word:
            word[S[l]] += 1
        else:
            word[S[l]] = 1

flg = 0

for key in word.values():
    if key % 2 == 1:
        flg = 1
        break
    
if flg == 0:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

chars = 'abcdefghijklmnopqrstuvwxyz'

tate_list = [[0 for _ in range(26)] for __ in range(W)]
yoko_list = [[0 for _ in range(26)] for __ in range(H)]
tate_dict = dict()
yoko_dict = dict()
for h in range(H):
    for w in range(W):
        index = chars.find(S[h][w])
        tate_list[w][index] += 1
        yoko_list[h][index] += 1
for tate in tate_list:
    if tuple(tate) in tate_dict:
        tate_dict[tuple(tate)] += 1
    else:
        tate_dict[tuple(tate)] = 1
for yoko in yoko_list:
    if tuple(yoko) in yoko_dict:
        yoko_dict[tuple(yoko)] += 1
    else:
        yoko_dict[tuple(yoko)] = 1
        
tate_vals = tate_dict.values()
yoko_vals = yoko_dict.values()
tate_odd = 0
yoko_odd = 0
for tate in tate_vals:
    if tate % 2 != 0:
        tate_odd += 1
for yoko in yoko_vals:
    if yoko % 2 != 0:
        yoko_odd += 1

if H % 2 == 0 and W % 2 == 0:
    if tate_odd == 0 and yoko_odd == 0:
        print('YES')
    else:
        print('NO')
elif H % 2 == 1 and W % 2 == 0:
    if tate_odd == 0 and yoko_odd == 1:
        print('YES')
    else:
        print('NO')
elif H % 2 == 0 and W % 2 == 1:
    if tate_odd == 1 and yoko_odd == 0:
        print('YES')
    else:
        print('NO')
else:
    if tate_odd == 1 and yoko_odd == 1:
        print('YES')
    else:
        print('NO')

Submission status: Wrong Answer

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

chars = 'abcdefghijklmnopqrstuvwxyz'

tate_list = [[0 for _ in range(26)] for __ in range(W)]
yoko_list = [[0 for _ in range(26)] for __ in range(H)]
tate_dict = dict()
yoko_dict = dict()
for h in range(H):
    for w in range(W):
        index = chars.find(S[h][w])
        tate_list[w][index] += 1
        yoko_list[h][index] += 1
for tate in tate_list:
    if tuple(tate) in tate_dict:
        tate_dict[tuple(tate)] += 1
    else:
        tate_dict[tuple(tate)] = 1
for yoko in yoko_list:
    if tuple(yoko) in yoko_dict:
        yoko_dict[tuple(yoko)] += 1
    else:
        yoko_dict[tuple(yoko)] = 1
        
tate_vals = tate_dict.values()
yoko_vals = yoko_dict.values()
tate_odd = 0
yoko_odd = 0
for tate in tate_vals:
    if tate % 2 != 0:
        tate_odd += 1
for yoko in yoko_vals:
    if yoko % 2 != 0:
        yoko_odd += 1

if H % 2 == 0 and W % 2 == 0:
    if tate_odd == 0 and yoko_odd == 0:
        print('YES')
    else:
        print('NO')
elif H % 2 == 0 and W % 2 == 1:
    if tate_odd == 0 and yoko_odd == 1:
        print('YES')
    else:
        print('NO')
elif H % 2 == 1 and W % 2 == 0:
    if tate_odd == 1 and yoko_odd == 0:
        print('YES')
    else:
        print('NO')
else:
    if tate_odd == 1 and yoko_odd == 1:
        print('YES')
    else:
        print('NO')

Submission status: Wrong Answer

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

chars = 'abcdefghijklmnopqrstuvwxyz'

tate_list = [[0 for _ in range(26)] for __ in range(W)]
yoko_list = [[0 for _ in range(26)] for __ in range(H)]
tate_dict = dict()
yoko_dict = dict()
for h in range(H):
    for w in range(W):
        index = chars.find(S[h][w])
        tate_list[w][index] += 1
        yoko_list[h][index] += 1
for tate in tate_list:
    if tuple(tate) in tate_dict:
        tate_dict[tuple(tate)] += 1
    else:
        tate_dict[tuple(tate)] = 1
for yoko in yoko_list:
    if tuple(yoko) in yoko_dict:
        yoko_dict[tuple(yoko)] += 1
    else:
        yoko_dict[tuple(yoko)] = 1
tate_vals = tate_dict.values()
yoko_vals = yoko_dict.values()
ans = True
for tate in tate_vals:
    if tate % 2 != 0:
        ans = False
        break
for yoko in yoko_vals:
    if yoko % 2 != 0 or not ans:
        ans = False
        break
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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 = "NO"

def check_col_switch(S_arr, H, W):
    if W % 2 == 1:
        res_bar = -1
    else:
        res_bar = 0
    done_col = np.zeros(W)
    for j in range(W):
        if done_col[j] == 1:
            continue
        rev_j = S_arr[::(-1), j]
        for k in range(W):
            if np.prod(S_arr[:, k] == rev_j) == 1 and j != k and done_col[k] == 0:
                done_col[j] = 1
                done_col[k] = 1
                break
        if done_col[j] == 0:
            if np.prod(S_arr[:, j] == rev_j) == 1:
                done_col[j] = 1
                res_bar += 1
            else:
                res_bar += 10
    if res_bar <= 0:
        return "YES"
    else:
        return "NO"
    
def check_col_switch_fast(S_arr, H, W):
    
    col_raw = np.array(["".join(list(S_arr[:, j])) for j in range(W)])
    col_reverse = np.array(["".join(list(S_arr[::(-1), j])) for j in range(W)])
    
    argsa = np.argsort(col_raw)
    argsb = np.argsort(col_reverse)
    
    res = "YES"
    for j in range(W):
        if col_raw[argsa[j]] != col_reverse[argsb[j]]:
            res = "NO"
            break
    cnt_single = 0
    for j in range(W):
        if argsa[j] == argsb[j]:
            cnt_single += 1
    j = 0
    while j < W-1:
        if col_raw[argsa[j]] != col_raw[argsa[j+1]]:
            cnt_single -= 2
            j += 2
        else:
            j += 1
    if cnt_single >= 2:
        res = "NO"
    
    return res
    
def make_pairs(x_list):
    if len(x_list) <= 2:
        return [[x_list]]
    else:
        res = []
        for j in range(1, len(x_list)):
            x_list_s = [x_list[k] for k in range(1, len(x_list)) if k != j]
            res = res + [[[x_list[0], x_list[j]]] + xl for xl in make_pairs(x_list_s)]
        if len(x_list) % 2 == 1:
            x_list_l = [x_list[k] for k in range(1, len(x_list))]
            res = res + [xl + [[x_list[0]]] for xl in make_pairs(x_list_l)]
        return res
    
def run_row_switch(S_arr, row_matching, H, W):
    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], :]
    return S_arr_row
            
row_match_list = make_pairs([i for i in range(H)])


    

# row_matching
same_row_list = []
for i in range(H):
    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)
    same_row_list.append(j_list)
    
def pass_condition(rs):
    res = True
    for pair in rs:
        if len(pair) == 1:
            continue
        if pair[1] not in same_row_list[pair[0]]:
            res = False
            break
    return res

row_match_list_s = [rs for rs in row_match_list if pass_condition(rs)]


for rs in row_match_list_s:
    res = check_col_switch_fast(run_row_switch(S_arr, rs, H, W), H, W)
    if res == "YES":
        break
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = 12, 12
S = list()
S.append(list("bimonigaloaf"))
S.append(list("faurwlkbleht"))
S.append(list("dexwimqxzxbb"))
S.append(list("lxdgyoifcxid"))
S.append(list("ydxiliocfdgx"))
S.append(list("nfoabgilamoi"))
S.append(list("ibxbdqmzxxwe"))
S.append(list("pqirylfrcrnf"))
S.append(list("wtehfkllbura"))
S.append(list("yfrnpflcrirq"))
S.append(list("wvcclwgiubrk"))
S.append(list("lkbrwgwuiccv"))
S_arr = np.array(S)

res = "NO"

def check_col_switch(S_arr, H, W):
    if W % 2 == 1:
        res_bar = -1
    else:
        res_bar = 0
    done_col = np.zeros(W)
    for j in range(W):
        if done_col[j] == 1:
            continue
        rev_j = S_arr[::(-1), j]
        for k in range(W):
            if np.prod(S_arr[:, k] == rev_j) == 1 and j != k and done_col[k] == 0:
                done_col[j] = 1
                done_col[k] = 1
                break
        if done_col[j] == 0:
            if np.prod(S_arr[:, j] == rev_j) == 1:
                done_col[j] = 1
                res_bar += 1
            else:
                res_bar += 10
    if res_bar <= 0:
        return "YES"
    else:
        return "NO"
    
def check_col_switch_fast(S_arr, H, W):
    
    col_raw = np.array(["".join(list(S_arr[:, j])) for j in range(W)])
    col_reverse = np.array(["".join(list(S_arr[::(-1), j])) for j in range(W)])
    
    argsa = np.argsort(col_raw)
    argsb = np.argsort(col_reverse)
    
    res = "YES"
    for j in range(W):
        if col_raw[argsa[j]] != col_reverse[argsb[j]]:
            res = "NO"
            break
    cnt_single = 0
    for j in range(W):
        if argsa[j] == argsb[j]:
            cnt_single += 1
    j = 0
    while j < W-1:
        if col_raw[argsa[j]] != col_raw[argsa[j+1]]:
            cnt_single -= 2
            j += 2
        else:
            j += 1
    if cnt_single >= 2:
        res = "NO"
    
    return res
    
def make_pairs(x_list):
    if len(x_list) <= 2:
        return [[x_list]]
    else:
        res = []
        for j in range(1, len(x_list)):
            x_list_s = [x_list[k] for k in range(1, len(x_list)) if k != j]
            res = res + [[[x_list[0], x_list[j]]] + xl for xl in make_pairs(x_list_s)]
        if len(x_list) % 2 == 1:
            x_list_l = [x_list[k] for k in range(1, len(x_list))]
            res = res + [xl + [[x_list[0]]] for xl in make_pairs(x_list_l)]
        return res
    
def run_row_switch(S_arr, row_matching, H, W):
    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], :]
    return S_arr_row
            
row_match_list = make_pairs([i for i in range(H)])


    

# row_matching
same_row_list = []
for i in range(H):
    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)
    same_row_list.append(j_list)
    
def pass_condition(rs):
    res = True
    for pair in rs:
        if len(pair) == 1:
            continue
        if pair[1] not in same_row_list[pair[0]]:
            res = False
            break
    return res

row_match_list_s = [rs for rs in row_match_list if pass_condition(rs)]


for rs in row_match_list_s:
    res = check_col_switch_fast(run_row_switch(S_arr, rs, H, W), H, W)
    if res == "YES":
        break
print(res)

Submission status: Wrong Answer

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 = "NO"

def check_col_switch(S_arr, H, W):
    if W % 2 == 1:
        res_bar = -1
    else:
        res_bar = 0
    done_col = np.zeros(W)
    for j in range(W):
        if done_col[j] == 1:
            continue
        rev_j = S_arr[::(-1), j]
        for k in range(W):
            if np.prod(S_arr[:, k] == rev_j) == 1 and j != k and done_col[k] == 0:
                done_col[j] = 1
                done_col[k] = 1
                break
        if done_col[j] == 0:
            if np.prod(S_arr[:, j] == rev_j) == 1:
                done_col[j] = 1
                res_bar += 1
            else:
                res_bar += 10
    if res_bar <= 0:
        return "YES"
    else:
        return "NO"
    
def check_col_switch_fast(S_arr, H, W):
    
    col_raw = np.array(["".join(list(S_arr[:, j])) for j in range(W)])
    col_reverse = np.array(["".join(list(S_arr[::(-1), j])) for j in range(W)])
    
    argsa = np.argsort(col_raw)
    argsb = np.argsort(col_reverse)
    
    res = "YES"
    for j in range(W):
        if col_raw[argsa[j]] != col_reverse[argsb[j]]:
            res = "NO"
            break
    cnt_single = 0
    for j in range(W):
        if argsa[j] == argsb[j]:
            cnt_single += 1
    j = 0
    while j < W-1:
        if col_raw[argsa[j]] != col_raw[argsa[j+1]]:
            cnt_single -= 2
            j += 2
        else:
            j += 1
    if cnt_single >= 2:
        res = "NO"
    
    return res
    
def make_pairs(x_list):
    if len(x_list) <= 2:
        return [[x_list]]
    else:
        res = []
        for j in range(1, len(x_list)):
            x_list_s = [x_list[k] for k in range(1, len(x_list)) if k != j]
            res = res + [[[x_list[0], x_list[j]]] + xl for xl in make_pairs(x_list_s)]
        if len(x_list) % 2 == 1:
            x_list_l = [x_list[k] for k in range(1, len(x_list))]
            res = res + [xl + [[x_list[0]]] for xl in make_pairs(x_list_l)]
        return res
    
def run_row_switch(S_arr, row_matching, H, W):
    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], :]
    return S_arr_row
            
row_match_list = make_pairs([i for i in range(H)])

for rs in row_match_list:
    res = check_col_switch_fast(run_row_switch(S_arr, rs, H, W), H, W)
    if res == "YES":
        break
print(res)

Submission status: Wrong Answer

Solution code:
#! -*- coding: utf-8 -*-

import numpy as np

h,w = map(int,raw_input().split())
s_l = []
al = "abcdefghijklmnopqrstuvwxyz"
al_gyou = np.zeros([h,26])
al_retsu = np.zeros([w,26])
for i in range(h):
    line = raw_input()
    #s_l.append(line)
    for j in range(w):
        for k in range(26):
            if line[j] == al[k]:
                al_gyou[i][k]+=1
                al_retsu[j][k]+=1

#print al_gyou
#print al_retsu

flag = 0

gyou_kakunin = np.zeros([h])
count = 0

for i in range(h):
    status = 0
    if gyou_kakunin[i] == 1:
        status = 1
    else:
        gyou_kakunin[i] = 1
        for j in range(h):
            if i==j or gyou_kakunin[j] == 1:
                pass
            else:
                if np.allclose(al_gyou[i],al_gyou[j]):
                    status = 1
                    gyou_kakunin[j] = 1
                    break
    if status == 0:
        count += 1
    if count > h%2:
        flag =1
        break

retsu_kakunin = np.zeros([w])
count = 0

if flag == 1:
    pass
else:
    for i in range(w):
        status = 0
        if retsu_kakunin[i] == 1:
            status = 1
        else:
            retsu_kakunin[i] = 1
            for j in range(w):
                if i==j or retsu_kakunin[j] == 1:
                    pass
                else:
                    if np.allclose(al_retsu[i],al_retsu[j]):
                        status = 1
                        retsu_kakunin[j] = 1
                        break
        if status == 0:
            count += 1
        if count > w%2:
            flag = 1
            break

if flag == 0:
    ans = "YES"
else:
    ans = "NO"

print ans

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
if n == 1 and m == 1:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import collections

H, W = map(int, input().split())
S = ""
for _ in range(H):
    S+=input()

S_ = collections.Counter(S)
cnt = 0
for k, v in S_.items():
    if v%2 == 1:
        cnt += 1

if (H * W)%2 == 0:
    if cnt == 0:
        print("YES")
    else:
        print("NO")
else:
    if cnt <= 1:
        print("YES")
    else:
        print("NO")

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
if n == 1 and m == 1:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
#! -*- coding: utf-8 -*-

import numpy as np

h,w = map(int,raw_input().split())
s_l = []
al = "abcdefghijklmnopqrstuvwxyz"
al_gyou = np.zeros([h,26])
al_retsu = np.zeros([w,26])
for i in range(h):
    line = raw_input()
    #s_l.append(line)
    for j in range(w):
        for k in range(26):
            if line[j] == al[k]:
                al_gyou[i][k]+=1
                al_retsu[j][k]+=1

#print al_gyou
#print al_retsu

count = 0
flag = 0

gyou_kakunin = np.zeros([h])
retsu_kakunin = np.zeros([w])

for i in range(h):
    status = 0
    if gyou_kakunin[i] == 1:
        status = 1
    else:
        gyou_kakunin[i] = 1
        for j in range(h):
            if i==j or gyou_kakunin[j] == 1:
                pass
            else:
                if np.allclose(al_gyou[i],al_gyou[j]):
                    status = 1
                    gyou_kakunin[j] = 1
                    break
    if status == 0:
        count += 1

#print count
if count > h%2:
    flag =1

count = 0
for i in range(w):
    status = 0
    if retsu_kakunin[i] == 1:
        status = 1
    else:
        retsu_kakunin[i] = 1
        for j in range(w):
            if i==j or retsu_kakunin[j] == 1:
                pass
            else:
                if np.allclose(al_retsu[i],al_retsu[j]):
                    status = 1
                    retsu_kakunin[j] = 1
                    break
    if status == 0:
        count += 1

#print count
if count > w%2:
    flag = 1

if flag == 0:
    ans = "YES"
else:
    ans = "NO"

print ans

Submission status: Wrong Answer

Solution code:
from sys import exit
from itertools import permutations

def main():
    r, s = map(int, input().split())
    a = [input() for i in range(r)]

    def check(x):
        cnt = [0 for i in range(26)]
        for i in x:
            cnt[ord(i) - ord('a')] += 1
        odd = sum(i % 2 for i in cnt)
        return odd == len(x) % 2

    def f(i1, i2):
        if len(i1) + len(i2) == 1:
            return
        dr, ds = dict(), dict()
        for i in i1:
            b = tuple(sorted([a[i][j] for j in i2]))
            if b not in dr:
                dr[b] = 1
            else:
                dr[b] += 1
        for j in i2:
            b = tuple(sorted([a[i][j] for i in i1]))
            if b not in ds:
                ds[b] = 1
            else:
                ds[b] += 1
        if sum(dr[k] % 2 for k in dr) != len(i1) % 2 or\
                sum(ds[k] % 2 for k in ds) != len(i2) % 2:
            print('NO')
            exit(0)

        if len(dr.keys()) > 1:
            for k in dr:
                indeksi = [i for i in i1 if tuple(
                    sorted(a[i][j] for j in i2)) == k]
                f(indeksi, i2)
        if len(ds.keys()) > 1:
            for k in ds:
                indeksi = [j for j in range(s) if tuple(
                            sorted([a[q][j] for q in i1])) == k]
                f(i1, indeksi)
    
    f([i for i in range(r)], [i for i in range(s)])

    def check(pr, ps):
        for i in range(r//2):
            for j in range(s//2):
                x = r - i - 1
                y = s - j - 1
                if a[pr[i]][ps[j]] != a[pr[x]][ps[y]]:
                    return False
        return True

    if r <= 6 and s <= 6:
        for pr in permutations([w for w in range(r)]):
            for ps in permutations([q for q in range(s)]):
                if check(pr, ps):
                    print("YES")
                    exit(0)
        print("NO")
    else:
        print('YES')

main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def solve(f):
    h, w = f.read_int_list()
    s = [list(f.read_str()) for i in xrange(h)]
    s2 = [[s[hi][wi] for hi in xrange(h)] for wi in xrange(w)]

    sh = ["".join(sorted(si)) for si in s]
    sw = ["".join(sorted([s[hi][wi] for hi in xrange(h)])) for wi in xrange(w)]
    odd_h = []
    dh = collections.Counter(sh)
    for k, v in dh.items():
        if v % 2 == 1: odd_h.append(sh.index(k))

    odd_w = []
    dw = collections.Counter(sw)
    for k, v in dw.items():
        if v % 2 == 1: odd_w.append(sw.index(k))


    if len(odd_h) != h % 2 or len(odd_w) != w % 2: return "NO"

    def checkh(oh):
        d = collections.defaultdict(list)
        res = None
        for i, si in enumerate(s[oh]):
            d[si].append(i)
        for k, v in d.items():
            if len(v) % 2 == 0:
                dd = collections.Counter(["".join(sorted(s2[vi])) for vi in v])
                od = 0
                for vv in dd.values():
                    if vv % 2 == 1: od += 1
                if od > 1: return False
            else:
                res = k
        return res

    def checkw(ow):
        d = collections.defaultdict(list)
        res = None
        for i, si in enumerate(s2[ow]):
            d[si].append(i)
        for k, v in d.items():
            if len(v) % 2 == 0:
                dd = collections.Counter(["".join(sorted(s[vi])) for vi in v])
                od = 0
                for vv in dd.values():
                    if vv % 2 == 1: od += 1
                if od > 1: return False
            else:
                res = k
        return res

    resh = resw = None
    if len(odd_h) == 1:
        resh = checkh(odd_h[0])
        if resh == False: return "NO"
    if len(odd_w) == 1:
        resw = checkw(odd_w[0])
        if resw == False: return "NO"
    if resh is not None and resw is not None and resh != resw: return "NO"

    return "YES"


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)