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

Solution (Python):
import collections
h,w = map(int, input().split())
s = ''.join([''.join(input().split()) for _ in range(h)])

ctr = collections.Counter(s)
if h % 2 == 0 and w % 2 == 0:
    four, two = h*w//4, 0
elif h % 2 == 0:
    four, two = (h*w-h)//4, h//2
elif w % 2 == 0:
    four, two = (h*w-w)//4, w//2
else:
    four, two = (h*w-h-w+1)//4, (h+w-1)//2

cf = sum([c//4 for c in ctr.values()])
ct = sum([(c%4)//2 for c in ctr.values()])

if cf >= four and ct + 2*(cf-four) >= two:
    print('Yes')
else:
    print('No')

Solution (Python):
from collections import Counter
h,w=map(int,input().split())
s=""
for i in range(h):
  s+=input()
sc=Counter(s)
d=[0,0,0]
for v in sc.values():
    d[0]+=(v//4)*4
    d[1]+=2 if v%4>=2 else 0
    d[2]+=v%2

if w%2==0 and h%2==0:
  print("Yes" if d[0]==h*w else "No")
elif (w%2)*(h%2)==1:
  print("Yes" if d[1]<=(w-1)+(h-1) and d[2]==1 else "No")
elif w%2==0:
  print("Yes" if d[1]<=w and d[2]==0 else "No")
elif h%2==0:
  print("Yes" if d[1]<=h and d[2]==0 else "No")

Solution (Python):
from collections import Counter
h,w=map(int,input().split())
s=""
for i in range(h):
  s+=input()
sc=Counter(s)
d=[0,0,0]
for v in sc.values():
    d[0]+=(v//4)*4
    d[1]+=2 if v%4>=2 else 0
    d[2]+=v%2

if w%2==0 and h%2==0:
  print("Yes" if d[0]==h*w else "No")
elif (w%2)*(h%2)==1:
  print("Yes" if d[1]<=(w-1)+(h-1) and d[2]==1 else "No")
elif w%2==0:
  print("Yes" if d[1]<=w and d[2]==0 else "No")
elif h%2==0:
  print("Yes" if d[1]<=h and d[2]==0 else "No")

Solution (Python):


from collections import Counter
w = ''
H,W = map(int,input().split())
for _ in range(H):
    w += input()
freq = Counter(list(w))


quartet = 0
pair = 0
if H%2 == 0 and W%2 == 0:
    for v in freq.values():
        if v%4 != 0:
            print('No')
            exit()
    print('Yes')
    exit()

elif H%2 == 0 and W%2 != 0:

    for k,v in freq.items():
        
        if freq[k]>=4:
            quartet += v//4
            freq[k] -= 4*(v//4)
    if quartet < (H//2)*(W//2): 
        print('No')
        exit()
    else:
        pair += 2*(quartet-(H//2)*(W//2)) 

    for k,v in freq.items():
        if freq[k] >= 2:
            pair += v//2
            freq[k] -= 2*(v//2)
    if pair != H//2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()


elif H%2 != 0 and W%2 == 0:
    for k, v in freq.items():
        
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  

    for k, v in freq.items():
        if freq[k] >= 2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != W // 2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()

else:
    for k, v in freq.items():
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  

    for k, v in freq.items():
        
        if freq[k] >=2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != (W//2 + H//2):
        print('No')
        exit()
    else:
        print('Yes')
        exit()

Solution (Python):
from collections import defaultdict

def f(n):
    a,b,c = 0,0,0
    a = n//4
    r = n%4
    b = r//2
    c = r%2
    return (a,b,c)

h,w = map(int,input().split())
a = defaultdict(lambda:0)
for _ in range(h):
    for i in input():
        a[i] += 1
need = []
need.append((h//2)*(w//2))
need.append((h%2)*(w//2)+(w%2)*(h//2))
need.append((h%2)*(w%2))
data = [0]*3
for i in a.values():
    j,k,l = f(i)
    data[0] += j
    data[1] += k
    data[2] += l
if data[0] >= need[0]:
    data[1] += (data[0]-need[0])*2
    if data[1] == need[1] and data[2] == need[2]:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
h,w=map(int,input().split())
cnt=[0]*26
for i in range(h):
	a=input()
	for j in range(w):
		cnt[ord(a[j])-ord("a")]+=1
if h%2==1 and w%2==1:
	f=0
	for i in range(26):
		if cnt[i]%2==1:
			cnt[i]-=1
			break
	four=0
	for i in range(26):
		if cnt[i]>=4:
			four+=cnt[i]//4*4
		elif cnt[i]%2==1:
			f=-1
			break
	if four>=h*w-(h//2+w//2)*2-1 and f==0:
		f=1
	else:
		f=0
elif h%2==0 and w%2==0:
	f=1
	for i in range(26):
		if cnt[i]%4!=0:
			f=0
			break
else:
	if h%2==0:
		h,w=w,h
	two=0;f=0
	for i in range(26):
		if cnt[i]%4==2:
			two+=2
		elif cnt[i]%2==1:
			f=-1
			break
	if two<=w and f==0:
		f=1
	else:
		f=0
print("Yes" if f else "No")

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
D = {}
center_of_center = (H % 2) * (W % 2)
center_of_H = (H % 2) * (W >> 1)
center_of_W = (W % 2) * (H >> 1)
for i in range(H):
  for j in range(W):
    if S[i][j] in D:
      D[S[i][j]] += 1
    else:
      D[S[i][j]] = 1
odd, half_odd, four = 0, 0, 0
odds = []
half_odds = []
fours = []
for i in D:
  if D[i] % 2:
    odd += 1
    odds.append(i)
  else:
    if (D[i] >> 1) % 2:
      half_odds.append(i)
      half_odd += 1
    else:
      four += 1
      fours.append(i)
if odd != center_of_center:
  print("No")
else:
  if odd != 0:
    D[odds[0]] -= 1
    if D[odds[0]] % 4 == 2:
      half_odd += 1
    else:
      four += 1
  if (center_of_H + center_of_W) % 2 != half_odd % 2 or center_of_H + center_of_W < half_odd:
    print("No")
  else:
    print("Yes")

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



H, W = MAP()

a = ""
for _ in range(H):
	a += input()

A = Counter(a).values()
cnt_1 = 0
cnt_2 = 0
cnt_4 = 0

for x in A:
	if x%4 == 0:
		cnt_4 += 1
	elif x%2 == 0:
		cnt_2 += 1
	else:
		cnt_1 += 1

if H%2 and W%2 and cnt_1 == 1 and cnt_2 <= H//2 + W//2:
	print("Yes")
elif H%2 + W%2 == 0 and cnt_1 + cnt_2 == 0:
	print("Yes")
elif H%2 and cnt_1 == 0 and cnt_2 <= W//2:
	print("Yes")
elif W%2 and cnt_1 == 0 and cnt_2 <= H//2:
	print("Yes")
else:
	print("No")

Solution (Python):
from collections import Counter
 
h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))
 
center = h * w % 2 
side = (h//2) * (w%2) + (w//2) * (h%2)
corner = (h//2) * (w//2)

for _ in range(center):
    for i in a:
        if a[i] % 2 == 1:
            a[i] -= 1
            break
for _ in range(side):
    for j in a:
        if a[j] % 4 == 2:
            a[j] -= 2
            break

print('Yes' if sum(a[i]%4 for i in a) == 0 else 'No')

Solution (Python):
import sys
from collections import defaultdict
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

H, W = map(int, readline().split())
A = [readline().decode().rstrip() for _ in range(H)]

dicts = defaultdict(int)
for a in A:
    for c in a:
        dicts[c] += 1

g1 = H * W % 2
g2 = (H // 2) * (W % 2) + (W // 2) * (H % 2)
g4 = (H // 2) * (W // 2)

for _ in range(g1):
    for k, v in dicts.items():
        if (v % 4 == 1) | (v % 4 == 3):
            dicts[k] -= 1
            break

for _ in range(g2):
    for k, v in dicts.items():
        if v % 4 == 2:
            dicts[k] -= 2
            break
"""for _ in range(g4):
    for k, v in dicts.items():
        if (v % 4 == 0) & (v != 0):
            dicts[k] -= 4
            break
"""
for k, v in dicts.items():
    if v % 4 != 0:
        print('No')
        sys.exit()

print("Yes")

Solution (Python):
h,w = map(int,input().split())
a = [input() for _ in range(h)]
alpha = [0]*26
for i in range(h):
    for j in range(w):
        alpha[ord(a[i][j])-ord("a")] += 1

ans = "Yes"
odd = 0
cnt = 0

for i in range(26):
    if alpha[i]%2 != 0:
        odd += 1
        alpha[i] -= 1
    if alpha[i]%4 != 0: cnt += 1

if odd > 1: ans = "No"
elif h%2 == 1 and w%2 == 1:
    if cnt > h//2+w//2: ans = "No"
elif h%2 == 1:
    if cnt > w//2: ans = "No"
elif w%2 == 1:
    if cnt > h/2: ans = "No"
else:
    if cnt > 0: ans = "No"
print(ans)

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    H, W = map(int, input().split())
    a = ''
    for _ in range(H):
        a += input()
    
    nums1 = 0
    nums2 = 0
    if H % 2 != W % 2:
        nums2 -= 1
    for l in Counter(a).values():
        if l & 1:
            nums1 += 1
        if l % 4 > 1:
            nums2 += 1

    if nums1 > H*W % 2 or nums2 > (H%2) * ((W-1)//2) + (W%2) * ((H-1)//2):
        print('No')
    else:
        print('Yes')

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline

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

def main():
    mod=10**9+7
    H,W=MI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(H):
        a=input()
        for j in range(W):
            dd[a[j]]+=1
            
    L=[]
    for k,v in dd.items():
        L.append(v)
    
    four=0
    two=0
    one=0
    
    for aa in L:
        four+=aa//4
        bb=aa%4
        if bb==2 or bb==3:
            two+=1
        if bb==1 or bb==3:
            one+=1
        
    flag=0    
    if H%2==0 and W%2==0:
        if two==0 and one==0:
            flag=1
    elif H%2==0 and W%2!=0:
        if one==0 and two<=(H//2):
            flag=1
    elif H%2!=0 and W%2==0:
        if one==0 and two<=(W//2):
            flag=1
    else:
        if one<=1 and two<=((H+W)//2):
            flag=1
            
    if flag:
        print("Yes")
    else:
        print("No")
        
            
    
        

main()

Solution (Python):
from collections import defaultdict


def main():
    counter = defaultdict(int)
    H, W = list(map(int, input().split(' ')))
    for _ in range(H):
        A = input()
        for a in A:
            counter[a] += 1
    if H % 2 == W % 2 == 0:
        
        for cnt in counter.values():
            if cnt % 4 != 0:
                print('No')
                return
        print('Yes')
    elif H % 2 == 0 and W % 2 == 1:
        non_quad_chars = 0
        for cnt in counter.values():
            if cnt % 4 == 1 or cnt % 4 == 3:
                print('No')
                return
            elif cnt % 4 == 2:
                non_quad_chars += 1
        if 2 * non_quad_chars <= H and (H - 2 * non_quad_chars) % 4 == 0:
            print('Yes')
        else:
            print('No')
    elif H % 2 == 1 and W % 2 == 0:
        non_quad_chars = 0
        for cnt in counter.values():
            if cnt % 4 == 1 or cnt % 4 == 3:
                print('No')
                return
            elif cnt % 4 == 2:
                non_quad_chars += 1
        if 2 * non_quad_chars <= W and (W - 2 * non_quad_chars) % 4 == 0:
            print('Yes')
        else:
            print('No')
    else:
        center_filled = False
        non_quad_chars = 0
        for cnt in counter.values():
            if cnt % 4 == 1 or cnt % 4 == 3:
                if center_filled:
                    print('No')
                    return
                center_filled = True
                if (cnt - 1) % 4 == 2:
                    non_quad_chars += 1
            elif cnt % 4 == 2:
                non_quad_chars += 1
        if 2 * non_quad_chars <= H + W - 2 and (H + W - 2 - 2 * non_quad_chars) % 4 == 0:
            print('Yes')
        else:
            print('No')


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

if sum(a[i] % 2 for i in a) != center:
    print('No')
    exit()
a = {i: a[i] - (a[i] % 2) for i in a}
cnt = sum(a[i] % 4 == 2 for i in a)
print('Yes' if cnt <= side else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

if sum(a[i] % 2 for i in a) != center:
    print('No')
    exit()
a = {i: a[i] - (a[i] % 2) for i in a}
print('Yes' if sum(a[i] % 4 == 2 for i in a) <= side else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

cnt = 0
for i in a:
    if a[i] % 2 == 1:
        a[i] -= 1
        cnt += 1
if cnt != center:
    print('No')
    exit()

print('Yes' if sum(a[i] % 4 == 2 for i in a) <= side else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

cnt = 0
for i in a:
    if a[i] % 2 == 1:
        a[i] -= 1
        cnt += 1
if cnt != center:
    print('No')
    exit()

cnt = 0
for i in a:
    if a[i] % 4 == 2:
        a[i] -= 2
        cnt += 1
if cnt > side:
    print('No')
    exit()

print('Yes' if sum(a[i] % 4 for i in a) == 0 else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

cnt = 0
for i in a:
    if a[i] % 2 == 1:
        a[i] -= 1
        cnt += 1
if cnt != center:
    print('No')
    exit()
cnt = 0
for i in a:
    if cnt == side:
        break
    if a[i] % 4 == 2:
        a[i] -= 2
        cnt += 1

print('Yes' if sum(a[i] % 4 for i in a) == 0 else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)

cnt = 0
for i in a:
    if cnt == center:
        break
    if a[i] % 2 == 1:
        a[i] -= 1
        cnt += 1
cnt = 0
for i in a:
    if cnt == side:
        break
    if a[i] % 4 == 2:
        a[i] -= 2
        cnt += 1

print('Yes' if sum(a[i] % 4 for i in a) == 0 else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)
corner = (h // 2) * (w // 2)

cnt = 0
for i in a:
    if cnt == center:
        break
    if a[i] % 2 == 1:
        a[i] -= 1
        cnt += 1
cnt = 0
for i in a:
    if cnt == side:
        break
    if a[i] % 4 == 2:
        a[i] -= 2
        cnt += 1

print('Yes' if sum(a[i] % 4 for i in a) == 0 else 'No')

Solution (Python):
from collections import Counter

h, w = map(int, input().split())
a = Counter(''.join(input() for _ in range(h)))

center = h * w % 2
side = (h // 2) * (w % 2) + (w // 2) * (h % 2)
corner = (h // 2) * (w // 2)

for i in range(center):
    for i in a:
        if a[i] % 2 == 1:
            a[i] -= 1
            break
for i in range(side):
    for i in a:
        if a[i] % 4 == 2:
            a[i] -= 2
            break

print('Yes' if sum(a[i] % 4 for i in a) == 0 else 'No')

Solution (Python):
import sys
from collections import defaultdict, Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    H, W = in_nn()
    a = []
    for i in range(H):
        a += list(in_s())

    mH = -(-H // 2)
    mW = -(-W // 2)
    nums = defaultdict(int)
    for y in range(mH):
        for x in range(mW):
            if x == W - x - 1 and y == H - y - 1:
                n = 1
            elif x == W - x - 1:
                n = 2
            elif y == H - y - 1:
                n = 2
            else:
                n = 4
            nums[n] += 1

    count = Counter(a)
    td = defaultdict(int)
    for v in count.values():
        for n in (4, 2, 1):
            if v >= n:
                if td[n] < nums[n]:
                    c = min(v // n, nums[n] - td[n])
                    td[n] += c
                    v -= n * c

    
    

    ans = 'Yes'
    for n in (4, 2, 1):
        if td[n] != nums[n]:
            ans = 'No'
            break

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

def main():
    H, W = map(int, input().split())
    all_c = Counter([])
    for _ in range(H):
        line = list(input())
        line_c = Counter(line)
        all_c += line_c
    
    if H % 2 == 0 and W % 2 == 0:
        if all(val%4 == 0 for val in all_c.values()):
            print("Yes")
        else:
            print("No")

    elif H % 2 ^ W % 2:
        flag = all(val%2 == 0 for val in all_c.values())
        two_count = sum(val%4 == 2 for val in all_c.values())

        if H % 2 == 0:
            threshould = H//2
        else:
            threshould = W//2

        if flag and two_count <= threshould:
            print("Yes")
        else:
            print("No")

    elif H % 2 == 1 and W % 2 == 1:
        one_count = sum(val%2 == 1 for val in all_c.values())
        
        three_count = sum(val%4 == 3 for val in all_c.values())
        two_count = sum(val%4 == 2 for val in all_c.values())

        threshould = H//2 + W//2
        if one_count == 1 and three_count + two_count <= threshould:
            print("Yes")
        else:
            print("No")

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


h,w = list(map(int, input().split()))
a = []
for i in range(h):
    a.extend(list(input()))
    
from collections import Counter
c = Counter(a)

must = {1:0, 2:0, 4:0}
must[4] += (h//2) * (w//2)
if h%2==1:
    must[2] += (w//2)
if w%2==1:
    must[2] += (h//2)
if w%2==1 and h%2==1:
    must[1] += 1
    
for base in (4,2,1):
    val = 0
    for k,v in c.items():
        if val + v//base >= must[base]:
            c[k] -= (must[base] - val) * base
            val += (must[base] - val)

            break
        val += v//base
        c[k] -= (v//base)*base
    if val<must[base]:
        ans = "No"
        break
else:
    ans = "Yes"
print(ans)

Solution (Python):
from collections import Counter

h, w = (int(x) for x in input().split())
A = []
for _ in range(h):
    A += list(input())

B = [0, 0, 0]
if h % 2 == 0 and w % 2 == 0:
    B[2] += h * w
elif h % 2 == 0 and w % 2 == 1:
    B[2] += h * (w - 1)
    B[1] += h
elif h % 2 == 1 and w % 2 == 0:
    B[2] += w * (h - 1)
    B[1] += w
else:
    B[2] += (w - 1) * (h - 1)
    B[1] += w + h - 2
    B[0] += 1

i = 0
for key, value in Counter(A).most_common():
    while value >= 4 and B[2] > 0:
        B[2] -= 4
        value -= 4
    while value >= 2 and B[1] > 0:
        B[1] -= 2
        value -= 2
    while value >= 1 and B[0] > 0:
        B[0] -= 1
        value -= 1
    if value != 0:
        print("No")
        exit()
else:
    print("Yes")
    exit()

Solution (Python):
from collections import Counter


def solve():
    YES, NO = "Yes", "No"
    if H % 2 and W % 2:
        if rems[1] == 0 and rems[3] == 1:
            rems[1] += 1
            rems[2] += 1
            rems[3] = 0
        if rems[1] != 1 or rems[3] or rems[2] > H // 2 + W // 2:
            return NO
        else:
            return YES
    elif H % 2 or W % 2:
        even_edge = H if W % 2 else W
        if rems[1] + rems[3] or rems[2] > even_edge // 2:
            return NO
        else:
            return YES
    else:
        if rems[1] + rems[2] + rems[3]:
            return NO
        else:
            return YES


H, W = map(int, input().split())
c = Counter()
for _ in range(H):
    c += Counter(input())
rems = Counter(v % 4 for v in c.values())
print(solve())

Solution (Python):
h,w=map(int,input().split())
a,l=[list(input())for i in range(h)],[0]*26
for i in a:
 for j in i:l[ord(j)-97]+=1
o,t,f=(h//2)*(w//2),(h//2)*(w%2)+(w//2)*(h%2),(h%2)*(w%2)
for i in l:
 while o and i>3:o-=1;i-=4
 while t and i>1:t-=1;i-=2
 f-=i
print("Yes"if o==t==f==0else"No")

Solution (Python):
h,w=map(int,input().split())
a=[input() for _ in range(h)]
d=dict()
for i in range(h):
    for j in range(w):
        if a[i][j] not in d:
            d[a[i][j]]=1
        else:
            d[a[i][j]]+=1
num=[]
for c in d:
    num.append(d[c])
num.sort(reverse=True)
ans=[[0]*w for _ in range(h)]
lis=[]
for i in range(h):
    for j in range(w):
        if ans[i][j]==0:
            if h-1-i==i and w-j-1!=j:
                ans[i][j]=1
                ans[i][w-1-j]=1
                lis.append(2)
            elif h-i-1!=i and w-j-1==j:
                ans[i][j]=1
                ans[h-1-i][j]=1
                lis.append(2)
            elif h-1-i==i and w-j-1==j:
                ans[i][j]=1
                lis.append(1)
            else:
                ans[i][j]=1
                ans[i][w-1-j]=1
                ans[h-1-i][j]=1
                ans[h-1-i][w-1-j]=1
                lis.append(4)
lis.sort(reverse=True)
for i in range(len(num)):
    for j in range(len(lis)):
        if num[i]>=lis[j]:
            num[i]-=lis[j]
            lis[j]=0
if sum(num)!=0 or sum(lis)!=0:
    print("No")
else:
    print("Yes")

Solution (Python):
h,w=map(int,input().split())
a=[0]*h
book={}
for _ in range(h):
    a=input()
    for i in a:
        if i in book:
            book[i]+=1
        else:
            book[i]=1


if h%2==0 and w%2==0:
    for i in book:
        if book[i]%4!=0:
            print("No")
            exit()
    print("Yes")

elif h%2==1 and w%2==1:
    cnt=0
    odd=0
    for i in book:
        if book[i]%2==1:
            odd+=1
            if odd==2:
                print("No")
                exit()
        cnt+=book[i]//4
    if odd==0:
        print("No")
    else:
        if cnt>=(h-1)*(w-1)//4:
            print("Yes")
        else:
            print("No")
else:
    cnt=0
    for i in book:
        if book[i]%2==1:
            print("No")
            exit()
        cnt+=book[i]//4
    if h%2==1:
        if cnt>=(h-1)*w//4:
            print("Yes")
        else:
            print("No")
    else:
        if cnt>=(w-1)*h//4:
            print("Yes")
        else:
            print("No")

Solution (Python):
h, w = map(int, input().split())
s = ""
cnt = [0] * 26
for _ in range(h):
    s = input()
    for i in range(len(s)):
        cnt[ord(s[i]) - ord("a")] += 1

c4=0
c2=0
c1=0
if h&1 == 1:
    c2 += w // 2
if w&1 == 1:
    c2 += h // 2
if (h&1 == 1) and (w&1 == 1):
    c1 = 1
c4 += (h//2) * (w//2)


for i in range(26):
    if c4 <= 0:
        continue
    while cnt[i] >= 4:
        cnt[i] -= 4
        c4 -= 1
for i in range(26):
    if c2 <= 0:
        continue
    while cnt[i] >= 2:
        cnt[i] -= 2
        c2 -= 1
for i in range(26):
    if c1 <= 0:
        continue
    while cnt[i] % 4 == 1 or cnt[i] % 4 == 3:
        cnt[i] -= 1
        c1 -= 1


if c1==0 and c2==0 and c4==0:
    print("Yes")
else:
    print("No")

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

h,w=MI()
lis=[]
for i in range(h):
    lis+=list(SI())
c=Counter(lis)
count1,count2=0,0
if h%2==0 and w%2==0:
    for x in c.keys():
        if c[x]%4!=0:
            print("No")
            sys.exit()
    print("Yes")
    
if h%2==0 and w%2==1:
    for x in c.keys():
        if c[x]%4==2:
            count2+=1
        elif c[x]%2==1:
            count1+=1
    if count2>h//2 or count1>0:
        print("No")
    else:
        print("Yes")
if w%2==0 and h%2==1:
    for x in c.keys():
        if c[x]%4==2:
            count2+=1
        elif c[x]%2==1:
            count1+=1
    
    if count2>w//2 or count1>0:
        print("No")
    else:
        print("Yes")
if w%2==1 and h%2==1:
    for x in c.keys():
        if c[x]%4==2:
            count2+=1
        elif c[x]%2==1:
            count1+=1
    
    if count1>1 or count2>h//2+w//2:
        print("No")
    else:
        print("Yes")

Solution (Python):
from collections import Counter
def check():
  H, W = map(int, input().split())
  S = [input() for _ in range(H)]
  s = ''.join(S)
  c = Counter(s)
  d = Counter(c.values())
  two = 0
  one = 0
  for k,v in d.items():
    if k%4==2 or k%4==3:
      two += 2*v
    if k%2==1:
      one += v
  if H%2==0 and W%2==0:
    if two>0 or one>0:
        return 'No'
  elif H%2==1 and W%2==1:
    if two>(H+W-2) or one>1:
      return 'No'
  elif H%2==1:
    if two>W or one>0:
      return 'No'
  elif W%2==1:
    if two>H or one>0:
      return 'No'
  return 'Yes'
print(check())

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
h,w = readInts()
S = ""
for i in range(h):
    S += input()
LIST = Counter(S)

if h%2 == 0 and w%2 == 0:
    for k,v in LIST.items():
        if v%4:
            print('No')
            exit()
elif (h%2 == 0 and w%2) or (h%2 and w%2 == 0):
    two = 0
    for k,v in LIST.items():
        if v%2 or v%4 == 3:
            print('No')
            exit()
        if v%4 == 2:
            two += 1
    if h%2 == 0:
        if two > h//2:
            print('No')
            exit()
    else:
        if two > w//2:
            print('No')
            exit()
else:
    odd = 0
    two = 0
    for k,v in LIST.items():
        if v%4 == 1:
            odd += 1
        if v%4 == 2:
            two += 1
        if v%4 == 3:
            print('No')
            exit()
    if odd != 1:
        print('No')
        exit()
    if two > ((h-1)//2 + (w-1)//2):
        print('No')
        exit()
print('Yes')

Solution (Python):
h, w = map(int, input().split())
A = [list(str(input())) for _ in range(h)]



idx = 0
d = {}
C = [0]*26
for i in range(h):
    for j in range(w):
        C[ord(A[i][j])-ord('a')] += 1
        d[(i, j)] = idx
        idx += 1

def Find(x, par):
    if par[x] < 0:
        return x
    else:
        par[x] = Find(par[x], par)
        return par[x]

def Unite(x, y, par, rank):
    x = Find(x, par)
    y = Find(y, par)

    if x != y:
        if rank[x] < rank[y]:
            par[y] += par[x]
            par[x] = y
        else:
            par[x] += par[y]
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

def Same(x, y, par):
    return Find(x, par) == Find(y, par)

def Size(x, par):
    return -par[Find(x, par)]

n = h*w
par = [-1]*n
rank = [0]*n

for i in range(h):
    for j in range(w):
        p = d[(i, j)]
        q = d[(i, w-1-j)]
        Unite(p, q, par, rank)
        q = d[(h-1-i, j)]
        Unite(p, q, par, rank)

S = []
for i in range(n):
    if par[i] < 0:
        S.append(-par[i])
S.sort(reverse=True)




C = [-c for c in C]
import heapq
heapq.heapify(C)
for s in S:
    c = -heapq.heappop(C)
    if c >= s:
        c -= s
        heapq.heappush(C, -c)
    else:
        print('No')
        exit()
else:
    print('Yes')