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:
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 len(k) > 1:
        if k[1] < 2:
            f = False
else:
    if k[0] < 2:
        f = False

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

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('k'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('j'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
T = [[None]*H for i in range(W)]
D = {}
for i in range(H):
    for j in range(W):
        c = S[i][j]
        D[c] = D.get(c, 0) ^ 1
if sum(D.values()) != (1 if (H%2==1)and(W%2==1) else 0):
    print("NO")
    exit(0)
for i in range(H):
    s = S[i]
    for j in range(W):
        T[j][i] = s[j]
def check(S, H, W):
    res = []
    for p in range(H):
        sp = S[p]
        for q in range(p+1, H):
            sq = S[q]
            d = {}
            for k in range(W):
                if sp[k] < sq[k]:
                    k = sp[k], sq[k]
                    d[k] = d.get(k, 0) + 1
                elif sp[k] == sq[k]:
                    k = sp[k], sp[k]
                    d[k] = d.get(k, 0) ^ 1
                else:
                    k = sq[k], sp[k]
                    d[k] = d.get(k, 0) - 1
            cap = (W % 2)
            ok = 1
            for a, b in d:
                if a == b:
                    if d[a, b]:
                        if cap == 0:
                            ok = 0
                        cap -= 1
                else:
                    if d[a, b] != 0:
                        ok = 0
            if ok: res.append((p, q))
    for p in range(H):
        d = {}
        for s in S[p]:
            d[s] = d.get(s, 0) ^ 1
        if sum(d.values()) == W % 2:
            res.append((p, p))
    return res
sc = check(S, H, W)
tc = check(T, W, H)
def solve(N, P):
    def dfs(state, c):
        if N == c:
            return 1
        for p, q in P:
            v = (1 << p) | (1 << q)
            if state & v:
                continue
            if dfs(state | v, (c+2 if p != q else c+1)):
                return 1
        return 0
    return dfs(0, 0)
if solve(H, sc) and solve(W, tc):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
TH=[sorted(S[i]) for i in range(H)]
PH=[-1 for i in range(H)]
TW=[sorted([S[i][j] for i in range(H)]) for j in range(W)]
PW=[-1 for j in range(W)]
#0<=i<H,0<=j<W
for i in range(H):
    if PH[i]!=-1:
        continue
    else:
        for k in range(H):
            if k==i:continue
            if TH[k]==TH[i]:
                PH[i]=k
                PH[k]=i
                break
for j in range(W):
    if PW[j]!=-1:
        continue
    else:
        for k in range(W):
            if k==j:continue
            if TW[k]==TW[j]:
                PW[j]=k
                PW[k]=j
                break
if H%2==0:
    for i in range(H):
        if PH[i]==-1:
            print("NO")
            exit()
else:
    if PH.count(-1)>=2:
        print("NO")
        exit()
if W%2==0:
    for i in range(W):
        if PW[i]==-1:
            print("NO")
            exit()
else:
    if PW.count(-1)>=2:
        print("NO")
        exit()
#usedcheck
QH=[0 for i in range(H)]
QW=[0 for j in range(W)]
RH=[]
RW=[]
SH=[-1 for i in range(H)]
SW=[-1 for j in range(W)]
for i in range(H):
    if QH[i]==0:
        if PH[i]==-1:
            SH[H//2]=i
            continue
        RH.append((i,PH[i]))
        QH[i]=1
        QH[PH[i]]=1
for j in range(W):
    if QW[j]==0:
        if PW[j]==-1:
            SW[W//2]=j
            continue
        RW.append((j,PW[j]))
        QW[j]=1
        QW[PW[j]]=1
for i in range(H//2):
    SH[i]=RH[i][0]
    SH[H-1-i]=RH[i][1]
for j in range(W//2):
    SW[j]=RW[j][0]
    SW[W-1-j]=RW[j][1]
XH=sorted(SH)
XW=sorted(SW)
for i in range(H):
    if i!=XH[i]:
        print("NO")
        exit()
for j in range(W):
    if j!=XW[j]:
        print("NO")
        exit()
U=[[S[SH[i]][SW[j]] for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if U[i][j]!=U[H-1-i][W-1-j]:
            print("NO")
            exit()
print("YES")

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
[h,w]=i2()
s=[]
for i in range(h):
  s.append("".join(sorted(input())))
from collections import Counter
c= Counter(s)
x=[]
d=0
a="YES"
for i in c.items():
  if i[1]%2==0:
     for k in range(i[1]//2):
       x.append(i[0])
     for k in range(i[1]//2):
       x.append(i[0][::-1])
  else:
     d+=1
     if d==1 and (h%2==0 or i[0]!=i[0][::-1]):
        a="NO"
        b=i[0]
     if d>1:
        a="NO"

d=0
xx=["" for i in range(w)]
for i in range(w): 
  for j in range(len(x)):
    xx[i]+=x[j][i]
  xx[i]="".join(sorted(xx[i]))

cc= Counter(xx)
dd=0
for i in cc.items():
  if i[1]%2==1:
     dd+=1
     if dd==1 and (w%2==0 or i[0]!=i[0][::-1]):
        a="NO"
        bb=i[0]
     if dd>1:
        a="NO"
if d==1 and dd==1 and b[w//2]!=bb[h//2]:
     a="NO"
print(a)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000)

h, w = map(int, input().split())
s = [None] * h
ss = [None] * h
for i in range(h):
    s[i] = input()
    ss[i] = sorted(s[i])

def c(x, y):
    l = len(y)
    if l == w: return True
    if l % 2 == 0:
        for i in range(w):
            if i not in y:
                y.append(i)
                if c(x, y): return True
                y.pop()
    else:
        for i in range(w):
            if i not in y:
                for j in range(0, h, 2):
                    if j + 1 == h: k = j
                    else: k = j + 1
                    a, b = x[j], x[k]
                    d, e = y[-1], i
                    if s[a][d] == s[b][e] and s[a][e] == s[b][d]:
                        y.append(i)
                        if c(x, y): return True
                        y.pop()
    return False
    
def r(x):
    l = len(x)
    if l == h:
        for i in range(w):
            if c(x, [i]): return True
    if l % 2 == 0:
        for i in range(h):
            if i not in x:
                x.append(i)
                if r(x): return True
                x.pop()
    else:
        for i in range(h):
            if i not in x and ss[x[-1]] == ss[i]:
                x.append(i)
                if r(x): return True
                x.pop()
    return False
            

ok = True
for i in range(h):
    ok = r([i])
    if ok: break
if ok: print('YES')
else: print('NO')

Submission status: Wrong Answer

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(30)] for _ in range(H)]
    d_w = [[0 for i in range(30)] 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: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('i'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('h'):
        print('NO')
    else:
        print('YES')


main()

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(300)]
        for i in x:
            cnt[ord(i)] += 1
        odd = sum(i % 2 for i in cnt)
        return odd == len(x) % 2

    midr, mids = None, None

    dr = dict()
    for i in range(r):
        b = tuple(sorted(a[i]))
        if b not in dr:
            dr[b] = 1
        else:
            dr[b] += 1
    odd = 0
    for k in dr:
        if dr[k] % 2 == 1:
            odd += 1
            midr = k
    if odd != r % 2 or midr and not check(midr):
        print('NO')
        exit(0)

    ds = dict()
    for j in range(s):
        b = tuple(sorted([a[i][j] for i in range(r)]))
        if b not in ds:
            ds[b] = 1
        else:
            ds[b] += 1
    odd = 0
    for k in ds:
        if ds[k] % 2 == 1:
            odd += 1
            mids = k
    if odd != s % 2 or mids and not check(mids):
        print('NO')
        exit(0)

    for k in dr:
        indeksi = [i for i in range(r) if tuple(sorted(a[i])) == k]

        d = dict()
        for j in range(s):
            b = tuple(sorted([a[i][j] for i in indeksi]))
            if b not in d:
                d[b] = 1
            else:
                d[b] += 1
        odd = 0
        for k in d:
            if d[k] % 2 == 1:
                odd += 1
                x = k
        if odd != s % 2 or odd == 1 and not check(x):
            print('NO')
            exit(0)

    for k in ds:
        indeksi = [j for j in range(s) if tuple(
                    sorted([a[q][j] for q in range(r)])) == k]
        d = dict()
        for i in range(r):
            b = tuple(sorted([a[i][j] for j in indeksi]))
            if b not in d:
                d[b] = 1
            else:
                d[b] += 1
        odd = 0
        for k in d:
            if d[k] % 2 == 1:
                odd += 1
                x = k
        if odd != r % 2 or odd == 1 and not check(x):
            print('NO')
            exit(0)

    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:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('g'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('f'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
import random
import numpy as np

h,w=map(int,input().split())
s=[list(input())for i in range(h)]
x=""
for i in s:
    for j in i:x+=j
co=Counter(x)
coun=0
for k,v in co.items():
    if(v%2==1):
        coun+=1
if(coun>1):
    print("NO")
elif(coun==1):
    if(h%2==1 and w%2==1):
        for i in range(h):
            for j in range(w):
                S=s[i][j]
                for k in range(h):
                    if(S in s[k]):
                        x=s[k].index(S)
                        y=k
                        if((x==j and w%2==0) or (y==i and h%2==0)):
                            print("NO")
                            exit()
        print("YES")
    else:
        print("NO")
else:
    for i in range(h):
        for j in range(w):
            S=s[i][j]
            for k in range(h):
                if(S in s[k]):
                    x=s[k].index(S)
                    y=k
                    if(x==j and y==i):continue
                    if((x==j and w%2==0) or (y==i and h%2==0)):
                        print("NO")
                        exit()
    print("YES")

Submission status: Wrong Answer

Solution code:
from sys import exit
from itertools import permutations

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

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

midr, mids = None, None

dr = dict()
for i in range(r):
    b = tuple(sorted(a[i]))
    if b not in dr:
        dr[b] = 1
    else:
        dr[b] += 1
odd = 0
for k in dr:
    if dr[k] % 2 == 1:
        odd += 1
        midr = k
if odd != r % 2 or midr and not check(midr):
    print('NO')
    exit(0)

ds = dict()
for j in range(s):
    b = tuple(sorted([a[i][j] for i in range(r)]))
    if b not in ds:
        ds[b] = 1
    else:
        ds[b] += 1
odd = 0
for k in ds:
    if ds[k] % 2 == 1:
        odd += 1
        mids = k
if odd != s % 2 or mids and not check(mids):
    print('NO')
    exit(0)

for k in dr:
    indeksi = [i for i in range(r) if tuple(sorted(a[i])) == k]

    d = dict()
    for j in range(s):
        b = tuple(sorted([a[i][j] for i in indeksi]))
        if b not in d:
            d[b] = 1
        else:
            d[b] += 1
    odd = 0
    for k in d:
        if d[k] % 2 == 1:
            odd += 1
            x = k
    if odd != s % 2 or odd == 1 and not check(x):
        print('NO')
        exit(0)

for k in ds:
    indeksi = [j for j in range(s) if tuple(
                sorted([a[q][j] for q in range(r)])) == k]
    d = dict()
    for i in range(r):
        b = tuple(sorted([a[i][j] for j in indeksi]))
        if b not in d:
            d[b] = 1
        else:
            d[b] += 1
    odd = 0
    for k in d:
        if d[k] % 2 == 1:
            odd += 1
            x = k
    if odd != r % 2 or odd == 1 and not check(x):
        print('NO')
        exit(0)

def check(pr, ps):
    for i in range(r):
        for j in range(s):
            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 <= 5 and s <= 5:
    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')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('e'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
s = [input() for _ in range(H)]

d_h = [[0 for i in range(30)] for _ in range(H)]
d_w = [[0 for i in range(30)] 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: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('d'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('c'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    h, w = map(int, input().split())
    s = []
    for _ in range(h):
        col = []
        s.append(col)
        for c in input().strip():
            col.append(ord(c))
    c_row = defaultdict(int)
    for col in s:
        c_row[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_row.values()) > h % 2:
        print('NO')
        return
    for row, c in c_row.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > w % 2:
                print('NO')
                return

    c_col = defaultdict(int)
    for col in zip(*s):
        c_col[tuple(sorted(col))] += 1
    if sum(c % 2 for c in c_col.values()) > w % 2:
        print('NO')
        return

    for col, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in col:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    if s[0][0] == ord('b'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
[h,w]=i2()
s=[]
for i in range(h):
  s.append("".join(sorted(input())))
from collections import Counter
c= Counter(s)
x=[]
d=0
a="YES"
for i in c.items():
  if i[1]%2==0:
     for k in range(i[1]//2):
       x.append(i[0])
     for k in range(i[1]//2):
       x.append(i[0][::-1])
  else:
     d+=1
     if d==1 and (h%2==0 or i[0]!=i[0][::-1]):
        a="NO"
     if d>1:
        a="NO"

d=0
xx=["" for i in range(w)]
for i in range(w): 
  for j in range(len(x)):
    xx[i]+=x[j][i]
  xx[i]="".join(sorted(xx[i]))

cc= Counter(xx)
d=0
for i in cc.items():
  if i[1]%2==1:
     d+=1
     if d==1 and (w%2==0 or i[0]!=i[0][::-1]):
        a="NO"
     if d>1:
        a="NO"
print(a)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
s = [input() for _ in range(H)]

d_h = [[0 for i in range(30)] for _ in range(H)]
d_w = [[0 for i in range(30)] 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 = [0]
check_w = [0]

for i in range(H):
    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):
    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')