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:
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('a'):
        print('NO')
    else:
        print('YES')


main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

cin = sys.stdin
# cin = open("in.txt", "r")

def line2N():
    return map(int, cin.readline().split())

H, W = line2N()
S = []
for i in range(H):
    s = cin.readline().strip().replace(' ', '').lower()
    S.append(list(s))
cnt1 = Counter("".join(sorted(s)) for s in S)
# print(cnt1)
flag = True
c = 0
for v in cnt1.values():
    if v%2!=0:
        c+=1
if (H%2==0 and c>0) or (H%2==1 and c>1):
    flag = False
cnt2 = Counter("".join(sorted(S[i][j] for i in range(H))) for j in range(W))
# print(cnt2)
c = 0
for v in cnt2.values():
    if v%2!=0:
        c+=1
if (W%2==0 and c>0) or (W%2==1 and c>1):
    flag = False
if flag:
    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] == 'b':
        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] == 'a':
        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)]
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:
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)}
for i in range(n):
    s = input()
    for j in s:
      d[i][j] += 1

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

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

Submission status: Wrong Answer

Solution code:
from collections import Counter


def ce(c1, c2):
    for k in c1:
        if k not in c2:
            return False
        if c1[k] != c2[k]:
            return False

    return True


def main():
    W, H = list(map(int, input().split()))

    S = []
    for _ in range(W):
        S.append(input().strip())

    # print('------------')
    count = Counter()
    for s in S:
        # print(s)
        for c in s:
            count[c] += 1

    # print(count)
    rm = 0
    if H * W % 2 == 1:
        rm = 1
    for c, v in count.items():
        if v % 2 == 1:
            rm -= 1
        if rm < 0:
            print('NO')
            return

    rc = [Counter() for _ in range(W)]
    cc = [Counter() for _ in range(H)]
    for i in range(W):
        for j in range(H):
            rc[i][S[i][j]] += 1
            cc[j][S[i][j]] += 1

    cont = True
    while cont:
        for i in range(1, len(rc)):
            if ce(rc[0], rc[i]):
                break
        else:
            cont = False
        if cont:
            rc.pop(i)
            rc.pop(0)

    cont = True
    while cont:
        for i in range(1, len(cc)):
            if ce(cc[0], cc[i]):
                break
        else:
            cont = False
        if cont:
            cc.pop(i)
            cc.pop(0)

    # print(rc)
    # for r in rc:
    #     print(r)
    # print(cc)
    if len(rc) > W % 2:
        print('NO')
        return

    if len(cc) > H % 2:
        print('NO')
        return
    print('YES')


if __name__ == '__main__':
    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 c.items():
  if i[1]%2==1:
     d+=1
     if d==1 and (h%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 Counter


def check(field):
    hw = len(field)
    center = None
    prev = None
    for row in field:
        if prev is None:
            prev = row
            continue
        if prev == row:
            prev = None
            continue
        if center is not None:
            return False
        if hw % 2 == 0:
            return False
        center = prev
        prev = row

    if center is not None:
        cnt = Counter(center)
        cntcnt = Counter(c % 2 for c in cnt.values())
        if cntcnt[1] > 1:
            return False

    return True


h, w = map(int, input().split())
field = []
for i in range(h):
    field.append(list(map(ord, input())))
field1 = sorted(sorted(row) for row in field)
field2 = sorted(sorted(col) for col in zip(*field))
print('YES' if check(field1) and check(field2) else '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

    print('NO')


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

h, w = map(int, input().split())

d = defaultdict(int)
for i in range(h) :
    for c in input() :
        d[c] += 1

center = 0
for c in ascii_lowercase :
    if d[c]%2 == 1 :
        center += 1

if (h*w % 2 == 1 and center == 1) or (h*w % 2 == 0 and center == 0):
    print('YES')
else :
    print('NO')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
grid = [input() for i in range(H)]
row = [sum(20**(ord(c) - ord('a') + 1) for c in s) for s in grid]
col = [sum(20**(ord(grid[i][j]) - ord('a') + 1) for i in range(H)) for j in range(W)]
from collections import Counter
cnt = 0
for a, b in Counter(row).items():
  cnt += b % 2
if cnt > 1 or (cnt == 1 and H % 2 != 0):
  print('NO')
  exit(0)
cnt = 0
for a, b in Counter(col).items():
  cnt += b % 2
if cnt > 1 and (cnt == 1 and W % 2 != 0):
  print('NO')
  exit(0)
print('YES')

Submission status: Wrong Answer

Solution code:
from sys import exit
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)

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):
        row = []
        s.append(row)
        for c in input().strip():
            row.append(ord(c))
    c_row = defaultdict(int)
    for row in s:
        c_row[tuple(sorted(row))] += 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 row, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count.values()) > h % 2:
                print('NO')
                return

    print('YES')


main()

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W = map(int,input().split())

arr = np.array([list(map(ord,list(input()))) for i in range(H)])

sums1 = {}
sums2 = {}
flag = 0

for i in range(H):
    s = np.sum(arr[i])
    if not s in sums1:
        sums1[s] = 1
    else:
        sums1[s] += 1


for i in range(W):
    s = np.sum(arr[:,i:i+1])
    if not s in sums2:
        sums2[s] = 1
    else:
        sums2[s] += 1

odd_H = H%2
odd_W = W%2


c = 0
for s in sums1.values():
    if s != 2:
        c += 1
    if c > odd_H:
        flag += 1

c = 0
for s in sums2.values():
    if s != 2:
        c += 1
    if c > odd_W:
        flag += 1


if flag == 0:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

cin = sys.stdin
# cin = open("in.txt", "r")

def line2N():
    return map(int, cin.readline().split())

H, W = line2N()
S = []
for i in range(H):
    s = cin.readline().strip()
    S.append(list(s))
cnt1 = Counter("".join(sorted(s)) for s in S)
# print(cnt1)
flag = True
c = 0
for v in cnt1.values():
    if v%2!=0:
        c+=1
if (H%2==0 and c>0) or (H%2==1 and c>1):
    flag = False
cnt2 = Counter("".join(sorted(S[i][j] for i in range(H))) for j in range(W))
# print(cnt2)
c = 0
for v in cnt2.values():
    if v%2!=0:
        c+=1
if (W%2==0 and c>0) or (W%2==1 and c>1):
    flag = False
if flag:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

h, w = map(int, input().split())

d = defaultdict(int)
for i in range(h) :
    for c in input() :
        d[c] += 1

for c in ascii_lowercase :
    if d[c]%2 == 1 :
        print('NO')
        break
else :
    print('YES')

Submission status: Wrong Answer

Solution code:
from sys import exit
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 = sum(d[k] % 2 for k in d)
    if odd != s % 2:
        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 = sum(d[k] % 2 for k in d)
    if odd != r % 2:
        print('NO')
        exit(0)

print('YES')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
grid = [input() for i in range(H)]
row = [sum(20**(ord(c) - ord('a')) for c in s) for s in grid]
col = [sum(20**(ord(grid[i][j]) - ord('a')) for i in range(H)) for j in range(W)]
from collections import Counter
cnt = 0
for a, b in Counter(row).items():
  cnt += b % 2
if cnt > 1:
  print('NO')
  exit(0)
cnt = 0
for a, b in Counter(col).items():
  cnt += b % 2
if cnt > 1:
  print('NO')
  exit(0)
print('YES')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = ''
for i in range(H):
    S += input()
if H % 2 == 1 and W % 2 == 1:
    odd_cnt = 0
    for i in 'abcdefghijklmnopqrstuvwxyz':
        if S.count(i) % 2 == 1 and odd_cnt == 0:
            odd_cnt += 1
        elif S.count(i) % 2 == 1 and odd_cnt == 1:
            odd_cnt += 1
            print('NO')
            break
    if odd_cnt == 1:
        print('YES')
    elif odd_cnt == 0:
        print('NO')
else:
    for i in 'abcdefghijklmnopqrstuvwxyz':
        if S.count(i) % 2 == 1:
            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):
        row = []
        s.append(row)
        for c in input().strip():
            row.append(ord(c))
    c_row = defaultdict(int)
    for row in s:
        c_row[tuple(sorted(row))] += 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) > 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 row, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count) > h % 2:
                print('NO')
                return

    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):
        row = []
        s.append(row)
        for c in input().strip():
            row.append(ord(c))
    c_row = defaultdict(int)
    for row in s:
        c_row[tuple(sorted(row))] += 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) > 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 row, c in c_col.items():
        if c % 2:
            count = defaultdict(int)
            for ch in row:
                count[ch] += 1
            if sum(cc % 2 for cc in count) > w % 2:
                print('NO')
                return

    print('YES')


main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

def tostr(ctr):
    ret = ''
    for k,v in sorted(ctr.items()):
        ret += k + str(v)
    return(ret)

hctrs = [tostr(Counter(row)) for row in src]
vctrs = [tostr(Counter(col)) for col in zip(*src)]
hc2 = Counter(hctrs)
vc2 = Counter(vctrs)

allow_odd = H%2
for k,v in hc2.items():
    if v%2:
        if allow_odd:
            allow_odd = False
        else:
            print('NO')
            exit()

allow_odd = W%2
for k,v in vc2.items():
    if v%2:
        if allow_odd:
            allow_odd = False
        else:
            print('NO')
            exit()
print('YES')

Submission status: Wrong Answer

Solution code:
from collections import Counter


def check(field):
    center = None
    prev = None
    for i, row in enumerate(field):
        if prev is None:
            prev = row
            continue
        if prev == row:
            prev = None
            continue
        if center is not None:
            return False
        center = prev
        prev = row

    if center is not None:
        if prev is not None:
            return False
        cnt = Counter(center)
        cntcnt = Counter(c % 2 for c in cnt.values())
        if cntcnt[1] > 1:
            return False

    return True


h, w = map(int, input().split())
field = []
for i in range(h):
    field.append(list(map(ord, input())))
field1 = sorted(sorted(row) for row in field)
field2 = sorted(sorted(col) for col in zip(*field))
print('YES' if check(field1) and check(field2) else 'NO')

Submission status: Wrong Answer

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

def check(s):
    xs = sorted(["".join(sorted(x)) for x in S])
    while len(xs) >= 2:
        x = xs.pop(0)
        if x in xs:
            xs.remove(x)
        else:
            return False
    if len(xs) == 1:
        return xs[0] == xs[0][::-1]
    return True

def check1(S):
    if not (check(S) and check(zip(*S))):
        return False
    return True

def ck(s):
    xs = s[:]
    r = []
    while len(xs) >= 2:
        x = xs.pop(0)
        x0 = sorted(x)
        for i, _ in enumerate(xs):
            if sorted(_) == x0:
                del xs[i]
                r = [x] + r + [_]
                break
        else:
            return False
    if len(xs) == 1:
        if xs[0] != xs[0][::-1]:
            return False
        r = r[:len(xs)//2] + [xs[0]] + r[len(xs)//2:]
    return r

def ck2(s):
    xs = list(zip(*s))
    while len(xs) >= 2:
        x = xs.pop(0)
        xr = x[::-1]
        if xr in xs:
            xs.remove(xr)
        else:
            return False
    if len(xs) == 1:
        return xs[0] == xs[0][::-1]
    return True

r = ck(S)
if r == False:
    print("NO")
else:
    if ck2(r):
        print("YES")
    else:    
        print("NO")

#if check(S) and check(zip(*S)):
#    print("YES")
#else:
#    print("NO")

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
        solo=k
        if(coun>=2):break
if(coun>1):
    print("NO")
elif(coun==1):
    if(h%2==1 and w%2==1):
        x=w//2
        y=h//2
        t=s[y]
        co2=Counter(t)
        for k,v in co2.items():
            if(v%2==1):
                print("NO")
                exit()
        t=[]
        for i in s:
            t.append(i[x])
        co2=Counter(t)
        for k,v in co2.items():
            if(v%2==1):
                print("NO")
                exit()
        print("YES")
    else:
        print("NO")
else:
    if(h%2==1 and w%2==1):
        print("NO")
    else:
        for i in s:
            if(h%2==1):break
            co2=Counter(i)
            for k,v in co2.items():
                if(v>=2 and co[k]==v):
                    print("NO")
                    exit()
        for i in range(w):
            if(w%2==1):break
            t=[]
            for j in s:t.append(j[i])
            co2=Counter(t)
            for k,v in co2.items():
                if(v>=2 and co[k]==v):
                    print("NO")
                    exit()
            
        print("YES")

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)]
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]:
                    ...
                else:
                    k = sq[k], sp[k]
                    d[k] = d.get(k, 0) - 1
            if all(v == 0 for v in d.values()):
                res.append((p, q))
    return res
sc = check(S, H, W)
tc = check(T, W, H)
def solve(N, P):
    C = N//2*2
    def dfs(state, c):
        if C == c:
            return 1
        for p, q in P:
            v = (1 << p) | (1 << q)
            if state & v:
                continue
            if dfs(state | v, c+2):
                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:
from collections import Counter, OrderedDict
H, W = map(int, input().split())
S = [input() for _ in range(H)]
T = list(zip(*S))

def ok(S, N):
  ct = Counter([str(sorted(Counter(s).most_common())) for s in S])
  odds = 0
  for k, num in ct.most_common():
    if num % 2 == 1:
      odds += 1
      if N % 2 == 0 or odds >= 2:
        return False
  return True

if ok(S, H) and ok(T, W):
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
from sys import exit
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)

ok = False
for j in range(s):
    if tuple(sorted([a[i][j] for i in range(r)])) == mids:
        for k in dr:
            indeksi = [i for i in range(r) if tuple(sorted(a[i])) == k]
            ok = ok or check([a[i][j] for i in indeksi])
if not ok and mids:
    print('NO')
    exit(0)

ok = False
for i in range(r):
    if tuple(sorted(a[i])) == midr:
        for k in ds:
            indeksi = [j for j in range(s) if tuple(
                sorted([a[q][j] for q in range(r)])) == k]
            ok = ok or check([a[i][j] for j in indeksi])
if not ok and midr:
    print('NO')
    exit(0)

print('YES')

Submission status: Wrong Answer

Solution code:
from collections import Counter, OrderedDict
H, W = map(int, input().split())
S = [input() for _ in range(H)]
T = list(zip(*S))

print(S)
print(list(map(''.join, T)))

def ok(S, N):
  ct = Counter([str(sorted(Counter(s).most_common())) for s in S])
  odds = 0
  print(ct)
  for k, num in ct.most_common():
    if num % 2 == 1:
      odds += 1
      if N % 2 == 0 or odds >= 2:
        return False
  return True

if ok(S, H) and ok(T, W):
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
grid = [input() for i in range(H)]
row = [sum(27**(ord(c) - ord('a')) for c in s) for s in grid]
col = [sum(27**(ord(grid[i][j]) - ord('a')) for i in range(H)) for j in range(W)]
from collections import Counter
cnt = 0
for a, b in Counter(row).items():
  cnt += b % 2
if cnt > 1:
  print('NO')
  exit(0)
cnt = 0
for a, b in Counter(col).items():
  cnt += b % 2
if cnt > 1:
  print('NO')
  exit(0)
print('YES')