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):
def main():
    h, w = map(int, input().split())
    alp = [0]*26
    for _ in range(h):
        s = input()
        for c in s:
            p = ord(c) - ord("a")
            alp[p] += 1
    f = True
    cnt_1, cnt_2, cnt_4 = 0, 0, 0
    if h%2 == 1 and w%2 == 1:
        cnt_1 = 1
        cnt_2 = w-1 + h-1
        cnt_4 = (w-1)*(h-1)
    elif h%2 == 1 and w%2 == 0:
        cnt_2 = w
        cnt_4 = w*(h-1)
    elif h%2 == 0 and w%2 == 1:
        cnt_2 = h
        cnt_4 = (w-1)*h
    else:
        cnt_4 = w*h
    for i in range(26):
        while cnt_4 > 0 and alp[i]>=4:
            alp[i] -= 4
            cnt_4 -= 4
    if cnt_4 > 0:
        f = False
    for i in range(26):
        while cnt_2 > 0 and alp[i]>=2:
            alp[i] -= 2
            cnt_2 -= 2
    if cnt_2 > 0:
        f = False
    for i in range(26):
        while cnt_1 > 0 and alp[i]>=1:
            alp[i] -= 1
            cnt_1 -= 1
    if cnt_1 > 0:
        f = False
    if f:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


H,W = MI()
a = [LS2() for _ in range(H)]

from collections import defaultdict

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[a[i][j]] += 1

if H % 2 == 0 and W % 2 == 0:
    for key in d.keys():
        if d[key] % 4 != 0:
            print('No')
            exit()
    print('Yes')

if H % 2 == 0 and W % 2 != 0:
    H,W = W,H

if H % 2 != 0 and W % 2 == 0:
    a = 0
    for key in d.keys():
        if d[key] % 2 != 0:
            print('No')
            exit()
        elif d[key] % 4 != 0:
            a += 1
    if a <= W//2:
        print('Yes')
        exit()
    else:
        print('No')
        exit()

if H % 2 != 0 and W % 2 != 0:
    a,b = 0,0
    for key in d.keys():
        if d[key] % 4 == 1:
            a += 1
        elif d[key] % 4 == 3:
            a += 1
            b += 1
        elif d[key] % 4 == 2:
            b += 1
    if a <= 1 and b <= H//2 + W//2:
        print('Yes')
        exit()
    else:
        print('No')
        exit()

Solution (Python):
H,W=map(int,input().split())
X=[0 for i in range(26)]
a=[list(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        X[ord(a[i][j])-97]+=1
A=[i%4 for i in X]
B=[i%2 for i in X]
if H%2==0 and W%2==0:
    if sum(A)==0:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==1:
    if sum(B)==1 and sum(A)<=H+W-1:
        print("Yes")
    else:
        print("No")
else:
    if H%2==0:
        H,W=W,H
    
    if sum(B)==0 and sum(A)<=W:
        print("Yes")
    else:
        print("No")

Solution (Python):
H,W = map(int,input().split())
A = [input() for i in range(H)]
from collections import Counter
ctr = Counter()
for row in A:
    ctr.update(row)

if H%2==0 and W%2==0:
    print('Yes' if all(v%4==0 for v in ctr.values()) else 'No')
    exit()

if H%2==0 or W%2==0:
    if H%2: H,W = W,H
    if any(v%2 for v in ctr.values()):
        print('No')
        exit()
    pair = 0
    for v in ctr.values():
        if v%4==2:
            pair += 1
    print('Yes' if pair*2 <= H else 'No')

else:
    odd = 0
    for k in ctr.keys():
        if ctr[k]%2:
            odd += 1
            ctr[k] -= 1
    if odd != 1:
        print('No')
        exit()
    pair = 0
    for v in ctr.values():
        if v%4==2:
            pair += 1
    print('Yes' if pair*2 <= H+W-2 else 'No')

Solution (Python):

from collections import defaultdict
import sys

H, W = (int(x) for x in input().split())

freq = defaultdict(int)

for i in range(H):
    for c in input():
        freq[c] += 1

nDiv4 = 0
nDiv2 = 0
nDiv1 = 0
for c in freq:
    num = freq[c]

    d4 = num // 4
    nDiv4 += d4
    num -= d4 * 4

    d2 = num // 2
    nDiv2 += d2
    num -= d2 * 2

    nDiv1 += num 

canDiv2 = 0
if H % 2 != 0:
    canDiv2 += W // 2
if W % 2 != 0:
    canDiv2 += H // 2

if H % 2 != 0 and W % 2 != 0:
    if nDiv1 != 1:
        print("No")
        sys.exit(0)
else:
    if nDiv1 != 0:
        print("No")
        sys.exit(0)

if nDiv2 <= canDiv2:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())
    a_ac_v.sort(reverse=True)
    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i & 1 for i in a_ac_v])
        if h&1 == 1 and w&1 == 1:
            if a_ac_v_parity_ac[1] == 1:
                print('Yes')
            else:
                print('No')
            sys.exit()
        else:
            if a_ac_v_parity_ac[1] >= 1:
                print('No')
            else:
                print('Yes')
            sys.exit()

    num = []
    num.extend([4] * ((h // 2) * (w // 2)))

    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])
    while sum(num):
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
            a_ac_v.sort(reverse=True)
            num.sort(reverse=True)
        else:
            break
    if num[0] == 0 and a_ac_v[0] == 0:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,input().split())
a = ''

for i in range(h):
    a += input()
from collections import Counter
c = Counter(a)

if h==1 or w==1:
    odd = 0
    for i in c:
        if c[i]%2==1:
            odd += 1
    print('Yes' if odd<=1 else 'No')
    exit()

x = h*(w%2)+w*(h%2)-(h*w)%2
y = 0
z = 0
for i in c:
    y += c[i]%4
    c[i] = c[i]%4
    if c[i]%2==1:
        z += 1

print('Yes' if y<=x and z<=1 else 'No')

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq


import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        res=0
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
            elif cnt[i]%4==2:
                res+=1
        else:
            if res>h//2:
              print("No")
            else:
              print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w//2:
                print("No")
            else:
                print("Yes")
    else:
        res1=0
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res1+=1
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1       
        if res1==1 and res<=(h-1)//2+(w-1)//2:
            print("Yes")
        else:
            print("No")

Solution (Python):
H,W=map(int,input().split())
D={}
for y in range(H):
    S=input()
    for s in S:
        if s in D:
            D[s]+=1
        else:
            D[s]=1

if (H%2)+(W%2)==0:
    F=True
    for s in D:
        if D[s]%4:
            F=False
elif (H%2)+(W%2)==1:
    if H%2:
        H,W=W,H

    F=True
    G=0
    for s in D:
        if D[s]%2:
            F=False
        elif D[s]%4==2:
            G+=1
    if G>H//2:
        F=False
else:
    G=0
    I=0
    for s in D:
        if D[s]%2:
            I+=1
        elif D[s]%4==2:
            G+=1
    F=(I==1) and (G<=(H+W-2)//2)

if F:
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import defaultdict

def main():
    H, W = map(int, input().split())
    A = [list(input()) for _ in range(H)]
    d = defaultdict(int)

    for h in range(H):
        for w in range(W):
            d[A[h][w]] += 1
            d[A[h][w]] %= 4

    e = defaultdict(int)
    for a in d.values():
        e[a] += 1

    if H % 2 == 0 and W % 2 == 0:
        if e[1] or e[2] or e[3]:
            print('No')
        else:
            print('Yes')
    elif H % 2 == 1 and W % 2 == 1:
        if e[1] + e[3] == 1:
            if e[2] <= (H // 2 + W // 2):
                print('Yes')
            else:
                print('No')
        else:
            print('No')
    else:
        if e[1] or e[3]:
            print('No')
        else:
            if H % 2 == 0:
                cnt = H // 2
            else:
                cnt = W // 2
            if e[2] <= cnt:
                print('Yes')
            else:
                print('No')




if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import Counter
input = sys.stdin.readline
h,w,*a = map(int,input().split())
a = []
for i in range(h):
  a += list(input().strip())
m1 = (h%2)*(w%2)
m2 = (h%2)*(w//2)+(h//2)*(w%2)
c = Counter(a)
c1,c2, = 0,0
for i in c.values():
  dc2,dc1 = divmod(i%4,2)
  c1 += dc1
  c2 += dc2
print("Yes" if m1>=c1 and m2>=c2 else "No")

Solution (Python):
h,w=map(int,input().split())

a=[]
for i in range(h):
  a.append(input())

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

for i in range(h):
  for j in range(w):
    alpha[ord(a[i][j])-ord("a")]+=1


l=[0,0,0]
for i in range(26):
  l[0]+=alpha[i]//4
  alpha[i]=alpha[i]%4
  l[1]+=alpha[i]//2
  alpha[i]=alpha[i]%2
  l[2]+=alpha[i]
  alpha[i]=0

if l[1]<=(h//2)*(w%2)+(w//2)*(h%2) and l[2]==(h%2)*(w%2):
  print("Yes")
else:
  print("No")

Solution (Python):
h,w=map(int,input().split())
s=[input() for _ in range(h)]
d=dict(zip([chr(i+97) for i in range(26)],[0]*26))
for i in range(h):
  for j in range(w):
    d[s[i][j]]+=1
a=[i%4 for i in d.values()]
print(['No','Yes'][a.count(1)+a.count(3)==h%2*w%2 and a.count(2)<=(h//2)*(w%2)+(w//2)*(h%2)])

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


from collections import defaultdict

def main():
	h,w=MI()
	d=defaultdict(int)

	for _ in range(h):
		s=RD()
		for i in s:
			d[i]+=1

	dd=defaultdict(int)

	for k,v in d.items():
		q,r=divmod(v,4)
		dd[4]+=q
		qq,rr=divmod(r,2)
		dd[2]+=qq
		dd[1]+=rr
	


	ans="Yes"

	if h%2==0 and w%2==0:
		if dd[2]!=0 or dd[1]!=0:
			ans="No"

	elif h%2==0:
		if dd[1]!=0 or dd[2]>h//2:
			ans="No"

	elif w%2==0:
		if dd[1]!=0 or dd[2]>w//2:
			ans="No"

	else:
		if dd[1]!=1:
			ans="No"
		else:
			if dd[2]>h//2+w//2:
				ans="No"

	print(ans)


















if __name__ == "__main__":
	main()

Solution (Python):
from collections import Counter

H, W = map(int, input().split())
cntr = Counter()
[cntr.update(Counter(input())) for _ in range(H)]

if H % 2 and W % 2:
    has_one = False
    twos = 0
    for v in cntr.values():
        if v % 2:
            if has_one:
                print("No")
                exit()
            else:
                has_one = True
                twos += 2 if v == 3 else 0
        else:
            twos += v % 4
    print("Yes") if twos // 2 <= H // 2 + W // 2 and has_one else print("No")
elif H % 2 or W % 2:
    if H % 2:
        H, W = W, H
    twos = 0
    for v in cntr.values():
        if v % 2:
            print("No")
            exit()
        else:
            twos += v % 4
    print("Yes") if twos <= H else print("No")
else:
    print("Yes") if all(v % 4 == 0 for v in cntr.values()) else print("No")

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())

def solve():
    H, W = MAP()
    a = [input() for _ in range(H)]
    d = defaultdict(int)

    for i in range(H):
        for j in range(W):
            d[a[i][j]] += 1
    
    mod4 = 0
    mod2 = 0
    for x in d.values():
        mod4 += x % 4
        mod2 += x % 2
    
    ans = "No"
    if H % 2 == 0:
        if W % 2 == 0:
            if mod4 == 0:
                ans = "Yes"
        else:
            if mod4 <= H and mod2 == 0:
                ans = "Yes"
    elif H % 2 == 1:
        if W % 2 == 0:
            if mod4 <= W and mod2 == 0:
                ans = "Yes"
        else:
            if mod4 <= W+H-1 and mod2 == 1:
                ans = "Yes"

    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import Counter


from heapq import heappop, heappush, heapify


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [-int(i) for v, i in counts]

    def pop(): return -heappop(cands)
    def push(x): return heappush(cands, -x)

    tasks = [4] * (h // 2) * (w // 2)
    tasks += [2] * ((h % 2) * (w // 2) + (w % 2) * (h // 2))
    tasks += [1] * ((h % 2) * (w % 2))

    flag = True
    for e in tasks:
        c = pop()
        if c < e:
            flag = False
            break
        push(c - e)

    if flag:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Solution (Python):
H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
C = {}
for i in range(H):
    for j in range(W):
        a = A[i][j]
        if a not in C:
            C[a] = 0
        C[a] += 1
if H==1 or W==1:
    if H*W%2==0:
        flag = 0
        for a in C:
            if C[a]%2==1:
                flag = 1
                break
        if flag==0:
            print("Yes")
        else:
            print("No")
    else:
        cnt = 0
        for a in C:
            if C[a]%2==1:
                cnt += 1
        if cnt==1:
            print("Yes")
        else:
            print("No")
elif H%2==0 and W%2==0:
    flag = 0
    for a in C:
        if C[a]%4>0:
            flag = 1
            break
    if flag==0:
        print("Yes")
    else:
        print("No")
elif H%2==0:
    cnt = 0
    flag = 0
    for a in C:
        if C[a]%2==1:
            flag = 1
            break
        if C[a]%4==2:
            cnt += 1
    if flag==1:
        print("No")
    else:
        if cnt<=H//2:
            print("Yes")
        else:
            print("No")
elif W%2==0:
    cnt = 0
    flag = 0
    for a in C:
        if C[a]%2==1:
            flag = 1
            break
        if C[a]%4==2:
            cnt += 1
    if flag==1:
        print("No")
    else:
        if cnt<=W//2:
            print("Yes")
        else:
            print("No")
else:
    cnt1 = 0
    cnt2 = 0
    for a in C:
        if C[a]%2==1:
            cnt1 += 1
        if C[a]%4==2:
            cnt2 += 1
    if cnt1!=1:
        print("No")
    else:
        if cnt2<=(H+W-2)//2:
            print("Yes")
        else:
            print("No")

Solution (Python):
from collections import Counter
h, w, *s = open(0).read().split()
h = int(h)
w = int(w)
s = Counter(list(''.join(s)))

one = two = four = 0

for k in s:
    if s[k] % 4 == 0:
        four += 1
    elif s[k] % 2 == 0:
        two += 1
    else:
        one += 1
limit_one = 0
limit_two = 0


def ans():
    if two <= limit_two and one <= limit_one:
        print("Yes")
    else:
        print("No")


if h & 1 and w & 1:
    limit_two = h // 2 + w // 2
    limit_one = 1
    ans()
elif h & 1:
    limit_two = w // 2
    ans()
elif w & 1:
    limit_two = h // 2
    ans()
else:
    limit_two = 0
    ans()

Solution (Python):
import sys
from collections import Counter
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


def solve():
    h, w = inl()
    counter = Counter()
    for i in range(h):
        for c in ins():
            counter[c] += 1

    p4 = (h // 2) * (w // 2)
    p1 = 1 if h % 2 == 1 and w % 2 == 1 else 0
    p2 = (h * w - p4 * 4 - p1) // 2

    xs = [-x for x in counter.values()]
    hp = []
    for x in xs:
        heapq.heappush(hp, x)

    while hp:
        x = heapq.heappop(hp)
        k = -x
        if p4 > 0:
            if k < 4:
                return False
            m = k // 4
            p = min(m, p4)
            p4 -= p
            k -= p * 4
            if k > 0:
                heapq.heappush(hp, -k)
        elif p2 > 0:
            if k < 2:
                return False
            m = k // 2
            p = min(m, p2)
            p2 -= p
            k -= p * 2
            if k > 0:
                heapq.heappush(hp, -k)
        elif p1 > 0:
            if k == 1:
                return True
            return False
    return True


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

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

n = h*w

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

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

for i in range(h):
    for j in range(w):
        idx1 = i*w+j
        idx2 = i*w+(w-1-j)
        idx3 = (h-1-i)*w+j
        Unite(idx1, idx2, par, rank)
        Unite(idx1, idx3, par, rank)
X = [0]*26
for i in range(h):
    for j in range(w):
        k = ord(A[i][j])-ord('a')
        X[k] += 1
temp = []
for i in range(26):
    if X[i] > 0:
        temp.append(X[i])
S = []
for i in range(h*w):
    if par[i] < 0:
        S.append(-par[i])



S.sort(reverse=True)
import heapq
temp = [-1*t for t in temp]
heapq.heapify(temp)
for s in S:
    if temp:
        t = heapq.heappop(temp)*(-1)
        if t >= s:
            heapq.heappush(temp, (-1)*(t-s))
        else:
            print('No')
            exit()
    else:
        print('No')
        exit()
else:
    print('Yes')

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W = map(int, input().split())
    count = Counter()
    for _ in range(H):
        count += Counter(input())

    four = (H // 2) * (W // 2)
    two = (H // 2) * (W % 2) + (W // 2) * (H % 2)
    one = (H % 2) * (W % 2)

    for value in count.values():
        while value >= 4 and four > 0:
            value -= 4
            four -= 1
        while value >= 2 and two > 0:
            value -= 2
            two -= 1
        while value >= 1 and one > 0:
            value -= 1
            one -= 1

    if four == two == one == 0:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    h, w = map(int, readline().split())
    c = Counter()
    res = [[""] * w for _ in range(h)]

    for _ in range(h):
        s = list(input())
        c.update(s)

    d = Counter()

    if h % 2 == 1 and w % 2 == 1:
        d[1] = 1
        d[2] = (h - 1) // 2 + (w - 1) // 2
    elif h % 2 == 1 and w % 2 == 0:
        d[1] = 0
        d[2] = w // 2
    elif h % 2 == 0 and w % 2 == 1:
        d[1] = 0
        d[2] = h // 2
    d[4] = (h * w - d[1] - 2 * d[2]) // 4

    keys = c.keys()

    for key in keys:
        if c[key] % 2 == 1:
            c[key] -= 1
            d[1] -= 1

    for key in keys:
        if c[key] % 4 == 2:
            c[key] -= 2
            d[2] -= 1

    for key in keys:
        if c[key] % 4 == 0:
            d[4] -= c[key] // 4
            c[key] = 0

    while d[2] > 0:
        d[2] -= 2
        d[4] += 1

    if d[1] == d[2] == d[4] == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Solution (Python):
h, w = map(int, input().split())
a = {}
for i in range(h):
    s = input()
    for i in s:
        a.setdefault(i, 0)
        a[i] += 1
a2 = 0
a1 = 0
for i in a.values():
    if i % 2 == 1:
        a1 += 1
    elif i % 4 == 2:
        a2 += 2
if h % 2 == 1:
    if w % 2 == 1:
        b1 = 1
        b2 = h + w - 2
    else:
        b1 = 0
        b2 = w
else:
    if w % 2 == 1:
        b1 = 0
        b2 = h
    else:
        b1 = 0
        b2 = 0
if a1 > b1 or a2 > b2:
    print("No")
else:
    print("Yes")

Solution (Python):
h, w = map(int, input().split())
a = [[j for j in input()] for i in range(h)]

cnt = [0 for i in range(26)]
for i in range(h):
    for j in range(w):
        cnt[ord(a[i][j]) - ord("a")] += 1

ans1 = 0
ans2 = 0
ans4 = 0
if h % 2 == 1 and w % 2 == 1:
    ans1 = 1
    ans2 = (h - 1) // 2 + (w - 1) // 2
    ans4 = (h * w - ans1 - ans2 * 2) // 4
elif h % 2 == 1:
    ans2 = w // 2
    ans4 = (h * w - ans2 * 2) // 4
elif w % 2 == 1:
    ans2 = h // 2
    ans4 = (h * w - ans2 * 2) // 4
else:
    ans4 = h * w // 4

cnt1 = 0
cnt2 = 0
cnt4 = 0
for i in cnt:
    if i >= 4:
        cnt4 += i // 4
        i %= 4
    if i >= 2:
        cnt2 += i // 2
        i %= 2
    cnt1 += i








if ans4 > cnt4:
    print("No")
else:
    cnt2 += (cnt4 - ans4) * 2
    if ans2 > cnt2:
        print("No")
    else:
        cnt1 += (cnt2 - ans2) * 2
        if ans1 != cnt1:
            print("No")
        else:
            print("Yes")

Solution (Python):

from collections import defaultdict

H,W = map(int,input().split())
C = defaultdict(int)
for _ in range(H):
    row = list(input())
    for i in range(W):
        C[row[i]] += 1
if H%2==0 and W%2==0:
    flag = 0
    for a in C:
        if C[a]%4!=0:
            flag = 1
            break
    if flag==0:
        print("Yes")
    else:
        print("No")
elif (H%2==0 and W%2==1) or (H%2==1 and W%2==0):
    n = (H//2)*(W//2)
    for a in C:
        if C[a]>=4:
            n -= C[a]//4
            C[a] = C[a]%4
    if n>0:
        print("No")
    else:
        flag = 0
        for a in C:
            if C[a]%2==1:
                flag=1
                break
        if flag==0:
            print("Yes")
        else:
            print("No")
else:
    n = (H//2)*(W//2)
    for a in C:
        if C[a]>=4:
            n -= C[a]//4
            C[a] = C[a]%4
    if n>0:
        print("No")
    else:
        cnt = 0
        for a in C:
            cnt += C[a]%2
        if cnt==1:
            print("Yes")
        else:
            print("No")

Solution (Python):

H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
C = {chr(i):0 for i in range(97,123)}
for i in range(H):
    for j in range(W):
        C[A[i][j]] += 1
if H%2==0 and W%2==0:
    flag = 0
    for a in C:
        if C[a]%4!=0:
            flag = 1
            break
    if flag==0:
        print("Yes")
    else:
        print("No")
elif (H%2==0 and W%2==1) or (H%2==1 and W%2==0):
    n = (H//2)*(W//2)
    for a in C:
        if C[a]>=4:
            n -= C[a]//4
            C[a] = C[a]%4
    if n>0:
        print("No")
    else:
        flag = 0
        for a in C:
            if C[a]%2==1:
                flag=1
                break
        if flag==0:
            print("Yes")
        else:
            print("No")
else:
    n = (H//2)*(W//2)
    for a in C:
        if C[a]>=4:
            n -= C[a]//4
            C[a] = C[a]%4
    if n>0:
        print("No")
    else:
        cnt = 0
        for a in C:
            cnt += C[a]%2
        if cnt==1:
            print("Yes")
        else:
            print("No")

Solution (Python):
from collections import Counter

H,W,*n = open(0).read().split()
H = int(H)
W = int(W)
c = Counter([])
for x in n:
    c += Counter(x)
if H % 2 == 0 and W % 2 == 0:
    for x in c.values():
        if x % 4 != 0:
            print('No')
            break
    else:
        print('Yes')
elif H % 2 == 1 and W % 2 == 1:
    center = 0
    cross = H + W - 1
    corner = (H*W) - cross
    corner_filled = 0
    cross_filled = 0
    for x in c.values():
        if x % 2 == 1:
            if center == 0:
                center = 1
                y = x - 1
            else:
                print('No')
                break
        else:
            y = x
        if y % 4 == 0:
            if corner_filled + x < corner:
                corner_filled += x
            elif corner_filled == corner:
                if cross_filled + x > cross:
                    print('No')
                    break
                else:
                    cross_filled += x
            else:
                cross_filled += corner_filled + x - corner
                corner_filled = corner
        else:
            temp = (x//4) * 4
            if corner_filled + temp < corner:
                corner_filled += temp
            elif corner_filled == corner:
                if cross_filled + temp > cross:
                    print('No')
                    break
                else:
                    cross_filled += temp
            else:
                cross_filled += corner_filled + temp - corner
                corner_filled = corner
            if cross_filled + 2 > cross:
                print('No')
                break
            else:
                cross_filled += 2
    else:
        print('Yes')
else:
    if H % 2 == 1:
        center = W
        sides = (H*W) - W
    else:
        center = H
        sides = (H*W) - H
    center_filled = 0
    sides_filled = 0
    for x in c.values():
        if x % 4 == 0:
            if sides_filled + x < sides:
                sides_filled += x
            elif sides_filled == sides:
                if center_filled + x > center:
                    print('No')
                    break
                else:
                    center_filled += x
            else:
                center_filled += sides_filled + x - sides
                sides_filled = sides
        elif x % 2 == 0:
            temp = (x//4) * 4
            if sides_filled + temp < sides:
                sides_filled += temp
            elif sides_filled == sides:
                if center_filled + temp > center:
                    print('No')
                    break
                else:
                    center_filled += temp
            else:
                center_filled += sides_filled + temp - sides
                sides_filled = sides
            if center_filled + 2 > center:
                print('No')
                break
            else:
                center_filled += 2
        else:
            print('No')
            break
    else:
        print('Yes')

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

def solve(h, w, a):
    c = Counter(list("".join(a)))
    g = [0] * 3
    for num in c.values():
        g[0] += int(num % 2 == 1)
        g[1] += 2 * int(num % 4 == 2)
        g[2] += (num // 4) * 4
    if (w % 2 == 0) and (h % 2 == 0):
        return "Yes" if (g[2] == h*w) else "No"
    if (w % 2 == 1) and (h % 2 == 1):
        return "Yes" if (g[0] == 1) and g[1] <= (h + w - 2) else "No"
    if w % 2 == 1:
        return "Yes" if (g[0] == 0) and (g[1] <= h) else "No"
    if h % 2 == 1:
        return "Yes" if (g[0] == 0) and (g[1] <= w) else "No"
    return "Yes"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))