Problem:


Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    H, W = MI()
    alpha = [0] * len(l_alp)

    for i in range(H):
        s = S()
        for j in range(W):
            alpha[ord(s[j])-97] += 1

    for i in range((H+1)//2):
        for j in range((W+1)//2):
            V = 4
            if H % 2 != 0 and i == (H+1)//2-1:
                V //= 2
            if W % 2 != 0 and j == (W+1)//2-1:
                V //= 2
            f = False
            for k in range(len(alpha)):
                if alpha[k] >= V:
                    f = True
                    alpha[k] -= V
                    break
            # print(i, j, V, alpha)
            if not f:
                print('No')
                return
    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    H, W = MI()
    alpha = [0] * len(l_alp)

    for i in range(H):
        s = S()
        for j in range(W):
            alpha[ord(s[j])-97] += 1

    if H == 2 or W == 2:
        for i in range((max(H, W)+1)//2):
            V = 2
            if (H % 2 != 0 or W % 2 != 0) and i == (max(H, W)+1)//2-1:
                V //= 2

            f = []
            for j in range(2):
                for k in range(len(alpha)):
                    if alpha[k] >= V:
                        f.append(True)
                        alpha[k] -= V
                        break
            if len(f) != 2 or not all(f):
                print('No')
                return
        print('Yes')
    else:
        for i in range((H+1)//2):
            for j in range((W+1)//2):
                V = 4
                if H % 2 != 0 and i == (H+1)//2-1:
                    V //= 2
                if W % 2 != 0 and j == (W+1)//2-1:
                    V //= 2
                f = False
                for k in range(len(alpha)):
                    if alpha[k] >= V:
                        f = True
                        alpha[k] -= V
                        break
                # print(i, j, V, alpha)
                if not f:
                    print('No')
                    return
        print('Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    H, W = MI()
    alpha = [0] * len(l_alp)

    for i in range(H):
        s = S()
        for j in range(W):
            alpha[ord(s[j])-97] += 1

    if H <= 2 and W <= 2:
        print('Yes')
        return

    for i in range((H+1)//2):
        for j in range((W+1)//2):
            V = 4
            if H % 2 != 0 and i == (H+1)//2-1:
                V //= 2
            if W % 2 != 0 and j == (W+1)//2-1:
                V //= 2
            f = False
            for k in range(len(alpha)):
                if alpha[k] >= V:
                    f = True
                    alpha[k] -= V
                    break
            # print(i, j, V, alpha)
            if not f:
                print('No')
                return

    print('No 'if all(alpha) else 'Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
lis = [0 for i in range(26)]
s = [str(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    lis[ord(s[i][j])-ord("a")] += 1
lis.sort()
cnt1 = 0
cnt4 = 0
for num in lis:
  if num % 2 == 1:cnt1 += 1
  cnt4 += num//4
if h % 2 == 0:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 >= (h//2)*(w//2):
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 0 and cnt4 >= max(h,w)//2:
      print("Yes")
    else:
      print("No")
else:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 >= max(h,w)//2:
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 1 and cnt4 >= (h//2)*(w//2):
      print("Yes")
    else:
      print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
lis = [0 for i in range(26)]
s = [str(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    lis[ord(s[i][j])-ord("a")] += 1
lis.sort()
cnt1 = 0
cnt4 = 0
for num in lis:
  if num % 2 == 1:cnt1 += 1
  cnt4 += num//4
if h % 2 == 0:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 == (h//2)*(w//2):
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 0 and cnt4 >= max(h,w)//2:
      print("Yes")
    else:
      print("No")
else:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 >= max(h,w)//2:
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 1 and cnt4 >= (h//2)*(w//2):
      print("Yes")
    else:
      print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
lis = [0 for i in range(26)]
s = [str(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    lis[ord(s[i][j])-ord("a")] += 1
lis.sort()
cnt1 = 0
cnt4 = 0
for num in lis:
  if num % 2 == 1:cnt1 += 1
  cnt4 += num//4
if h % 2 == 0:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 == (h//2)*(w//2):
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 0 and cnt4 >= h//2:
      print("Yes")
    else:
      print("No")
else:
  if w % 2 == 0:
    if cnt1 == 0 and cnt4 >= w//2:
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 1 and cnt4 >= (h//2)*(w//2):
      print("Yes")
    else:
      print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
lis = [0 for i in range(26)]
s = [str(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    lis[ord(s[i][j])-ord("a")] += 1
lis.sort()
cnt1 = 0
cnt4 = 0
for num in lis:
  if num % 2 == 1:cnt1 += 1
  cnt4 += num//4
if h % 2 == 0:
  if w % 2 == 0:
    if cnt4 == (h//2)*(w//2):
      print("Yes")
    else:
      print("No")
  else:
    if cnt4 >= h//2:
      print("Yes")
    else:
      print("No")
else:
  if w % 2 == 0:
    if cnt4 >= w//2:
      print("Yes")
    else:
      print("No")
  else:
    if cnt1 == 1 and cnt4 >= (h//2)*(w//2):
      print("Yes")
    else:
      print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
mat = [input() for _ in range(H)]
cnts = Counter(c for r in mat for c in r)
odd = 0
if H % 2:
    odd += 1
if W % 2:
    odd += 1


def solve():
    v1 = list(cnts.values())
    rest = sum(v1)
    assert rest == H * W
    v2, v3 = [], []
    for v in v1:
        rest -= v - v % 4
        if v % 4:
            v2.append(v % 4)
    if odd == 0:
        return rest == 0
    if H % 2 and W % 2 and rest > H + W - 1:
        return False
    if H % 2 and W % 2 == 0 and rest > H:
        return False
    if H % 2 == 0 and W % 2 and rest > W:
        return False
    for v in v2:
        rest -= v - v % 2
        if v % 2:
            v3.append(v % 2)
    if odd == 1:
        return rest == 0
    return rest == 1 and len(v3) == 1


print("Yes" if solve() else "No")

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
mat = [input() for _ in range(H)]
cnts = Counter(c for r in mat for c in r)
odd = 0
if H % 2:
    odd += 1
if W % 2:
    odd += 1


def solve():
    v1 = list(cnts.values())
    rest = sum(v1)
    assert rest == H * W
    v2, v3 = [], []
    for v in v1:
        rest -= v - v % 4
        if v % 4:
            v2.append(v % 4)
    if odd == 0:
        return rest == 0
    for v in v2:
        rest -= v - v % 2
        if v % 2:
            v3.append(v % 2)
    if odd == 1:
        return rest == 0
    return len(v3) == 1 and rest == 1


print("Yes" if solve() else "No")

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
mat = [input() for _ in range(H)]
cnts = Counter(c for r in mat for c in r)
odd = 0
if H % 2:
    odd += 1
if W % 2:
    odd += 1


def solve():
    v1 = list(cnts.values())
    rest = sum(v1)
    assert rest == H * W
    v2, v3 = [], []
    for v in v1:
        rest -= v - v % 4
        if v % 4:
            v2.append(v % 4)
    if odd == 0:
        return rest == 0
    for v in v2:
        rest -= v - v % 2
        if v % 2:
            v3.append(v % 2)
    if odd == 1:
        return rest == 0
    return len(v3) == 1


print("Yes" if solve() else "No")

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A=[]
for i in range(H):
    a=list(input())
    A.append(a)

x=dict()
for i in range(H):
    for j in range(W):
        s=A[i][j]
        if s in x:
            x[s]+=1
        else:
            x[s]=1

for idx in x:
    if x[idx]%4==1 or x[idx]%4==3:
        x[idx]-=1
        break
g2=(H//2)+(W//2)
g3=(H//2)*(W//2)
#print(x)
if g2>0:
    flag=False
    for idx in x:
        if x[idx]!=0:
            flag=True
    if flag:
        pass
    else:
        print("No")
        exit()
for i in range(g2):
    #print(i)
    for idx in x:
        if x[idx]%4==2:
            x[idx]-=2
            break
#print(x)
if g3>0:
    flag=False
    for idx in x:
        if x[idx]!=0:
            flag=True
    if flag:
        pass
    else:
        print("No")
        exit()

for i in range(g3):
    #print(i)
    for idx in x:
        if x[idx]==0:
            continue
        if x[idx]%4==0:
            x[idx]-=4
            break
#print(x)
flag=True
for idx in x:
    if x[idx]!=0:
        flag=False
if flag:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A=[]
for i in range(H):
    a=list(input())
    A.append(a)

x=dict()
for i in range(H):
    for j in range(W):
        s=A[i][j]
        if s in x:
            x[s]+=1
        else:
            x[s]=1

for idx in x:
    if x[idx]%4==1 or x[idx]%4==3:
        x[idx]-=1
        break
g2=(H//2)+(W//2)
g3=(H//2)*(W//2)
#print(x)
for i in range(g2):
    #print(i)
    for idx in x:
        if x[idx]%4==2:
            x[idx]-=2
            break
#print(x)
if g3>0:
    flag=False
    for idx in x:
        if x[idx]!=0:
            flag=True
    if flag:
        pass
    else:
        print("No")
        exit()

for i in range(g3):
    #print(i)
    for idx in x:
        if x[idx]==0:
            continue
        if x[idx]%4==0:
            x[idx]-=4
            break
#print(x)
flag=True
for idx in x:
    if x[idx]!=0:
        flag=False
if flag:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    from collections import Counter
    c = Counter(A[0])
    for a in A[1:]:
        c += Counter(a)
    if H % 2 == 0 and W % 2 == 0:
        if all(v % 4 == 0 for v in c.values()):
            return print("Yes")
        else:
            return print("No")
    elif H % 2 == 1 and W % 2 == 1:
        c_odd = sum(1 if v % 2 == 1 else 0 for k, v in c.items())
        if c_odd == 1:
            return print("Yes")
        else:
            return print("No")
    else:
        if all(v % 2 == 0 for v in c.values()):
            return print("Yes")
        else:
            return print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter

h, w = map(int, readline().split())
a = []
for i in range(h):
    a += list(readline().rstrip().decode())
counter = Counter(a).values()
check = [1, h // 2 + w // 2, (h // 2) * (w // 2)]
if h % 2 != 1 or w % 2 != 1:
    check[0] -= 1
    check[1] -= 1
for v in counter:
    if check[2] < v // 4:
        v -= 4 * check[2]
        check[2] = 0
    else:
        check[2] -= v // 4
        v %= 4
    if check[1] < v // 2:
        v -= 2 * check[1]
        check[1] = 0
    else:
        check[1] -= v // 2
        v %= 2
    check[0] -= v
print('Yes' if all(check[i] == 0 for i in range(3)) else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w = map(int, input().split())
l = []
for i in range(h):
    l += list(input())
c = list(Counter(l).values())
if h%2 and w%2:
    g1 = 1
    g2 = (w//2) + (h//2)
    g4 = (w//2)*(h//2)
    for j in range(len(c)):
        if c[j]%4 == 1 or c[j]%4 == 3:
            c[j] -= 1
            break
    for k in range(g2):
        for m in range(len(c)):
            if c[m]%4 == 2:
                c[m] -= 2
                break
    for n in range(g4):
        for o in range(len(c)):
            if c[o]%4 == 0 and c[o]//4 >0:
                c[o] -= 4
                break

elif h%2 or w%2:
    g2 = (h//2 if h%2==0 else w//2)
    g4 = (w//2)*(h//2)
    for k in range(g2):
        for m in range(len(c)):
            if c[m]%4 == 2:
                c[m] -= 2
                break
    for n in range(g4):
        for o in range(len(c)):
            if c[o]%4 == 0 and c[o]//4 >0:
                c[o] -= 4
                break

else:
    g4 = (w//2)*(h//2)
    for n in range(g4):
        for o in range(len(c)):
            if c[o]%4 == 0 and c[o]//4 >0:
                c[o] -= 4
                break

if all(i==0 for i in c):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

H,W = map(int, input().split())
cnt = defaultdict(int)
for _ in range(H):
    for a in list(input()):
        cnt[a] += 1
cnt = [c for c in cnt.values()]



for i in range((H + 2 - 1) // 2):
    for j in range((W + 2 - 1) // 2):
        need = [(i, j), (H - 1 - i, j), (i, W - 1 - j), (H - 1 - i, W - 1 - j)]
        need = set(need)

        cnt.sort()

        flag = False
        for i, c in enumerate(cnt):
            if len(need) <= c:
                flag = True
                cnt[i] -= len(need)
                if cnt[i] == 0:
                    cnt.pop(i)
                break
                    
        if not flag:
            print("No")
            exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
from math import ceil
H,W=map(int, input().split())
if H==2 or W==2:
  print("No")
  exit()
D={"a":0,"b":0,"c":0,"d":0,"e":0,"f":0,"g":0,
   "h":0,"i":0,"j":0,"k":0,"l":0,"m":0,"n":0,
   "o":0,"p":0,"q":0,"r":0,"s":0,"t":0,"u":0,
   "v":0,"w":0,"x":0,"y":0,"z":0,}  
for i in range(H):
  S=input()
  for s in S:
    D[s]+=1
    
odds=0
for d in D.values():
  if d%2==1:
    odds+=1
if H%2==1 and W%2==0:
  res=ceil(W/2)
elif H%2==0 and W%2==1:
  res=ceil(H/2)
elif H%2==1 and W%2==1:
  res=ceil(H)+ceil(W)-1
else:
  res=0
if odds<=res:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
H,W=map(int, input().split())
if H==2 or W==2:
  print("No")
  exit()
D={"a":0,"b":0,"c":0,"d":0,"e":0,"f":0,"g":0,
   "h":0,"i":0,"j":0,"k":0,"l":0,"m":0,"n":0,
   "o":0,"p":0,"q":0,"r":0,"s":0,"t":0,"u":0,
   "v":0,"w":0,"x":0,"y":0,"z":0,}  
for i in range(H):
  S=input()
  for s in S:
    D[s]+=1
    
odds=0
for d in D.values():
  if d%2==1:
    odds+=1
if H%2==1 and W%2==0:
  res=W
elif H%2==0 and W%2==1:
  res=H
elif H%2==1 and W%2==1:
  res=H+W-1
else:
  res=0
if odds<=res:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = {}
for h in range(H):
    for s in sr():
        if not A.get(s, 0):
            A[s] = 1
            continue
        A[s] += 1
if H == W == 1:
    print("Yes")
    exit(0)
if H == 1 or W == 1:
    guki = max(H, W) % 2
    for k, v in A.items():
        if v % 2 == 1:
            if guki == 0:
                print("No")
                exit(0)
            if guki == 1:
                guki = 0
                continue
        if v % 2 == 0:
            continue
    print("Yes")
elif H % 2 == 1 and W % 2 == 1:
    kisu = 1
    two = H + W - 2
    for k, v in A.items():
        if v % 2 == 1:
            if kisu == 0:
                print("No")
                exit(0)
            kisu = 0
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 2
    print("Yes")
elif H % 2 == 0 and W % 2 == 0:
    for k, v in A.items():
        if v % 4 != 0:
            print("No")
            exit(0)
    print("Yes")
else:
    two = W if H % 2 else H
    kisu = 1
    for k, v in A.items():
        if v % 2 == 1:
            if kisu == 0:
                print("No")
                exit(0)
            kisu = 0
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 2
    print("Yes")

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = {}
for h in range(H):
    for s in sr():
        if not A.get(s, 0):
            A[s] = 1
            continue
        A[s] += 1
if H == W == 1:
    print("Yes")
    exit(0)
if H == 1 or W == 1:
    guki = max(H, W) % 2
    for k, v in A.items():
        if v % 2 == 1:
            if guki == 0:
                print("No")
                exit(0)
            if guki == 1:
                guki = 0
                continue
        if v % 2 == 0:
            continue
    print("Yes")
elif H % 2 == 1 and W % 2 == 1:
    kisu = 1
    two = H + W - 2
    for k, v in A.items():
        if v % 2 == 1:
            if kisu == 0:
                print("No")
                exit(0)
            kisu = 0
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 1
    print("Yes")
elif H % 2 == 0 and W % 2 == 0:
    for k, v in A.items():
        if v % 4 != 0:
            print("No")
            exit(0)
    print("Yes")
else:
    two = W if H % 2 else H
    kisu = 1
    for k, v in A.items():
        if v % 2 == 1:
            if kisu == 0:
                print("No")
                exit(0)
            kisu = 0
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 1
    print("Yes")

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = {}
for h in range(H):
    for s in sr():
        if not A.get(s, 0):
            A[s] = 1
            continue
        A[s] += 1
if H == W == 1:
    print("Yes")
    exit(0)
if H == 1 or W == 1:
    guki = max(H, W) % 2
    for k, v in A.items():
        if v % 2 == 1:
            if guki == 0:
                print("No")
                exit(0)
            if guki == 1:
                guki = 0
                continue
        if v % 2 == 0:
            continue
    print("Yes")
elif H % 2 == 1 and W % 2 == 1:
    kisu = 1
    two = H + W - 2
    for k, v in A.items():
        if v % 2 == 1:
            if kisu == 0:
                print("No")
                exit(0)
            kisu = 0
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 1
    print("Yes")
elif H % 2 == 0 and W % 2 == 0:
    for k, v in A.items():
        if v % 4 != 0:
            print("No")
            exit(0)
    print("Yes")
else:
    two = W if H % 2 else H
    for k, v in A.items():
        if v % 2 == 1:
            print("No")
            exit(0)
        if v % 4 != 0:
            if two == 0:
                print("No")
                exit(0)
            two -= 1
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int,input().split())
a = ""
for k in range(H):
    a += input()
C = Counter(a)
if H%2 == 0 and W%2 == 0:
    for e in C:
        if C[e]%4 != 0:
            print("No")
            exit(0)
    print("Yes")
else:
    t = []
    s = 0
    for e in C:
        if C[e]%4 != 0:
            t.append(C[e]%4)
        else:
            s += C[e]//4
    f = 0
    for e in t:
        if e%2 == 1:
            f += 1
    if H%2 == 1 and W%2 == 1:
        if f != 1:
            print("No")
        elif len(t) < H//2 + W//2 - 4*s:
            print("No")
        else:
            print("Yes")
    else:
        if f != 0:
            print("No")
        elif len(t) < H//2 + W//2 - 4*s:
            print("No")
        else:
            print("Yes")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
alp = [0] * 26
for i in range(h):
        for c in input(): alp[ord(c)-ord('a')] += 1
x = min(h, 2+h%2) * min(w, 2+w%2)

m2 = h * (w%2) + w * (h%2)
one = h%2 and w%2
cant = False
for i in alp:
        if i%4!=0:
                if i%4==2:
                        if m2>=2: m2 -= 2
                        else: cant = True
                if i%2==1:
                        if one: one = False
                        else: cant = True
print("YNeos"[cant::2])

Submission status: Wrong Answer

Solution code:
def examA():
    N, M, A, B = LI()
    C = [I()for _ in range(M)]
    ans = 0
    for i,c in enumerate(C):
        if N<=A:
            N += B
        N -= c
        if N<0:
            ans = i+1
            break
    if ans==0:
        ans = "complete"
    print(ans)
    return

def examB():
    N = LI()
    P = LI()
    return

def examC():
    S = LSI()
    N = I()
    NG = [SI()for _ in range(N)]
    ans = []
    for s in S:
        for ng in NG:
            if len(s)!=len(ng):
                ans.append(s)
                continue
            flag = True
            for i,t in ng:
                if s[i]!=t and t!="*":
                    flag = False
                    break
            if flag:
                ans.append("*"*len(s))
            else:
                ans.append(s)
    print(" ".join(map(str,ans)))
    return

def examD():
    H, W = LI()
    S = [SI()for _ in range(H)]
    D = defaultdict(int)
    for s in S:
        for d in s:
            D[d] += 1
    odd = 0
    two = 0
    for d in D.values():
        if d%2==1:
            odd += 1
        elif d%4==2:
            two += 1
    if odd>=2 or two>=3:
        print("No")
        return
    if H%2==1 and W%2==1:
        print("Yes")
        return
    if H%2==1 or W%2==1:
        if odd==0 and two<=1:
            print("Yes")
            return
    if odd==0 and two==0:
        print("Yes")
        return
    print("No")
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    N, K = LI()
    S = SI()
    D = [defaultdict(int) for _ in range(N+1)]
    for i in range(N):
        D[i+1] = deepcopy(D[i])
        D[i+1][S[i]] += 1
    ans = "No"
    L = [defaultdict(int) for _ in range(N-K+1)]
    for i in range(N-K+1):
        cur = deepcopy(D[i+K])
        for s,j in D[i].items():
            cur[s] -= j
        L[i] = cur



    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int,input().split())
a = ""
for k in range(H):
    a += input()
C = Counter(a)
if H%2 == 0 and W%2 == 0:
    for e in C:
        if C[e]%4 != 0:
            print("No")
            exit(0)
    print("Yes")
else:
    t = []
    for e in C:
        if C[e]%4 != 0:
            t.append(C[e]%4)
    f = 0
    for e in t:
        if e%2 == 1:
            f += 1
    if H%2 == 1 and W%2 == 1:
        if f != 1:
            print("No")
        else:
            print("Yes")
    else:
        if f != 0:
            print("No")
        else:
            print("Yes")

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    h,w = LI()
    a = [input() for i in range(h)]
    d = defaultdict(lambda : 0)
    for i in a:
        for j in i:
            d[j] += 1
    d = list(d.values())
    n = (h>>1)*(w>>1)
    f = [i for i in d if i >= 4]
    if len(f) < n:
        print("No")
        return
    fs = [i for i in d if i&1]
    if len(fs) > 1:
        print("No")
        return
    if (h*w)&1 and not fs:
        print("No")
        return
    if (not (h*w)&1) and fs:
        print("No")
        return
    print("Yes")
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
cntA = Counter()

for _ in range(H):
    cntA += Counter(list(input()))

four = (H // 2) * (W // 2)
for a, c in cntA.items():
    if four <= 0:
        break
    if c >= 4:
        cntA[a] -= 4
        four -= 1

if four > 0:
    print('No')
    exit()

if H % 2 == W % 2 == 1:
    for a, c in cntA.items():
        if c % 2 == 1:
            cntA[a] -= 1
            break
    else:
        print('No')
        exit()

print('Yes' if all(c % 2 == 0 for c in cntA.values()) else 'No')

Submission status: Wrong Answer

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

alpha=[[0 for i in range(26)] for j in range(H)]

for h in range(H):
    temp=input()
    for w in range(W):
        num=ord(temp[w])-97
        alpha[h][num]+=1


root=[i for i in range(H)]
for h in range(H-1):
    for i in range(h+1,H):
        if alpha[h]==alpha[i]:
            root[i]=min(root[i],h)

tate=[0]*H
for item in root:
    tate[item]+=1

tate=list(map(lambda x:x%2, tate))

for h in range(H):
    alpha[h]=list(map(lambda x:x%2, alpha[h]))

if tate.count(1)>1:
    print("No")
    exit()

root=list(set(root))

for item in root:
    if alpha[item].count(1)>1:
        print("No")
        exit()

print("Yes")