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: Accepted

Solution code:
from collections import defaultdict
from sys import exit

H, W = map(int, input().split())
char_counter = defaultdict(int)

for _ in range(H):
    for s in input():
        char_counter[s] += 1

if H % 2 == 0 and W % 2 == 0:
    for k, v in char_counter.items():
        if v % 4 != 0:
            print("No")
            exit()
    print("Yes")
elif H % 2 == 0 and W % 2 == 1:
    holder = defaultdict(int)
    counter = 0
    for k, v in char_counter.items():
        if v % 4 != 0:
            for _ in range(v % 4):
                holder[k] += 1
                counter += 1
    if counter <= H:
        for kk, vv in holder.items():
            if vv % 2 != 0:
                print("No")
                exit()
        print("Yes")
    else:
        print("No")
elif H % 2 == 1 and W % 2 == 0:
    holder = defaultdict(int)
    counter = 0
    for k, v in char_counter.items():
        if v % 4 != 0:
            for _ in range(v % 4):
                holder[k] += 1
                counter += 1
    if counter <= W:
        for kk, vv in holder.items():
            if vv % 2 != 0:
                print("No")
                exit()
        print("Yes")
    else:
        print("No")
elif H % 2 == 1 and W % 2 == 1:
    holder = defaultdict(int)
    counter = 0
    for k, v in char_counter.items():
        if v % 4 != 0:
            for _ in range(v % 4):
                holder[k] += 1
                counter += 1
    if counter <= W + H - 1:
        no_odd = False
        for kk, vv in holder.items():
            if vv % 2 != 0:
                if no_odd:
                    print("No")
                    exit()
                else:
                    no_odd = True
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W=map(int, input().split())
    A=[list(input().rstrip()) for _ in range(H)]
    from itertools import chain
    from collections import Counter
    C=Counter(chain.from_iterable(A))
    t4=[(v//4)*4 for v in C.values()]
    t2=[(v//2)*2 for v in C.values()]
    if sum(t4) >= (H-(H&1))*(W-(W&1)):
        if sum(t2) >= H*W-(H&W&1):
            print('Yes')
            return
    print('No')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
from collections import defaultdict
cnt = defaultdict(int)
for _ in range(H):
    A = list(input())
    for a in A:
        cnt[a] += 1
cnt = [c for c in cnt.values()]
cnt.sort()

for i in range((H + 2 - 1) // 2):
    for j in range((W + 2 - 1) // 2):
        need = set([(i,j),(H-i-1,j),(i,W-j-1),(H-i-1,W-j-1)])
        flag = False
        for k in range(len(cnt)):
            if len(need) <= cnt[k]:
                flag = True
                cnt[k] -= len(need)
                if cnt[k] == 0:
                    cnt.pop(k)
                break
        
        if not flag:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
from collections import Counter
c=Counter()
for i in range(h):
    s=list(input())
    c+=Counter(s)

if h==1 and w==1:
    print("Yes")
elif h==1:
    s2=w//2
    for k,v in c.items():
        c[k]-=2*(v//2)
        s2-=v//2
    if s2==0:
        print("Yes")
    else:
        print("No")
elif w==1:
    s2=h//2
    for k,v in c.items():
        c[k]-=2*(v//2)
        s2-=v//2
    if s2==0:
        print("Yes")
    else:
        print("No")
else:
    s4=((w//2)*(h//2))
    s2=0
    if w%2 and h%2:
        s2=(h+w-2)//2
    elif w%2:
        s2=(h//2)
    elif h%2:
        s2=w//2
    for k,v in c.items():
        s4-=v//4
        c[k]-=4*(v//4)
        s2-=c[k]//2
        c[k]-=2*(c[k]//2)
    if s4==0 and s2==0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
from collections import Counter
H, W = map(int, input().split())
cntA = Counter()
for _ in range(H):
    cntA += Counter(list(input()))

if H % 2 + W % 2 == 0:
    if all(c % 4 == 0 for c in cntA.values()):
        print('Yes')
    else:
        print('No')
    exit()

if H % 2 + W % 2 == 1:
    two = (H if H % 2 == 0 else W) // 2
    cntTwo = 0
    for c in cntA.values():
        if c % 4 == 0:
            continue
        if c % 2 == 0:
            cntTwo += 1
            continue
        break
    else:
        if cntTwo <= two:
            print('Yes')
            exit()
    print('No')
    exit()

cntTwo = 0
cntOdd = 0
for c in cntA.values():
    if c % 4 == 0:
        continue
    if c % 2 == 0:
        cntTwo += 1
        continue
    cntOdd += 1

if cntOdd == 1 and cntTwo <= (H + W - 2) // 2:
    print('Yes')
    exit()

print('No')

Submission status: Accepted

Solution code:
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')
elif H%2 and W%2:
    odd = 0
    for k,v in ctr.items():
        if v%2:
            odd += 1
            ctr[k] -= 1
    if odd != 1:
        print('No')
        exit()
    four = 0
    for v in ctr.values():
        four += (v//4)*4
    want = (H-H%2)*(W-W%2)
    print('Yes' if four >= want else 'No')
else:
    if any(v%2 for v in ctr.values()):
        print('No')
        exit()
    four = 0
    for v in ctr.values():
        four += (v//4)*4
    want = (H-H%2)*(W-W%2)
    print('Yes' if four >= want else 'No')

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

h, w = MAP()
a = ''
for _ in range(h):
    a += STR()
a = list(a)
b = Counter(a)
c = list(set(a))
n4 = 0
n2 = 0
n1 = 0
for i in c:
    if b[i] % 2:
        n1 += 1
    elif b[i] % 4 == 0:
        n4 += 1
    elif b[i] % 2 == 0:
        n2 += 1
if h % 2 and w % 2:#奇数が一個あって良い。2の倍数が5//2*2コあってもよい
    if n1 == 1 and n2 <= h // 2 + w // 2:
        print('Yes')
    else:
        print('No')
elif h % 2:
    if n1 == 0 and n2 <= w // 2:
        print('Yes')
    else:
        print('No')
elif w % 2:
    if n1 == 0 and n2 <= h // 2:
        print('Yes')
    else:
        print('No')
else:
    if n1 == 0 and n2 == 0:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

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

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

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

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes" if P[0]==0 else "No")

Submission status: Accepted

Solution code:
from math import ceil
h,w=map(int,input().split())

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

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

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes")

Submission status: Accepted

Solution code:
from math import ceil
h,w=map(int,input().split())

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

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

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes" if P[0]==0 else "No")

Submission status: Accepted

Solution code:
from collections import Counter
h,w = map(int,input().split())
A = [input() for _ in range(h)]
s = []
for a in A:
    for c in a:
        s.append(c)
c = Counter(s)
if h==1 and w==1:
    print('Yes')
else:
    x,y,z = 0,0,0
    for v in c.values():
        if v%4==0:
            x += 1
        elif v%4==2:
            y += 1
        else:
            z += 1
    if h%2==0 and w%2==0:
        print('Yes' if (y==0 and z==0) else 'No')
    elif h%2==0 or w%2==0:
        if z!=0:
            print('No')
        else:
            if h%2==0 and y<=h//2:
                print('Yes')
            elif w%2==0 and y<=w//2:
                print('Yes')
            else:
                print('No')
    else:
        if z!=1:
            print('No')
        else:
            if y<=(h-1)//2 + (w-1)//2:
                print('Yes')
            else:
                print('No')

Submission status: Accepted

Solution code:
from collections import defaultdict

H, W = map(int, input().split())
cnt = defaultdict(int)

for _ in range(H):
    a = list(input())
    
    for ai in a:
        cnt[ai] += 1

four = 0
odd = 0

for v in cnt.values():
    four += v//4
    odd += v%2
    
if four>=H//2*(W//2) and odd<=1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
from collections import Counter

h, w = map(int, input().split())
a = []
for _ in range(h):
    aa = list(input())
    a += aa

c = Counter(a)

hq, hr = divmod(h, 2)
wq, wr = divmod(w, 2)
d = {1: hr * wr,
     2: hq * wr + hr * wq,
     4: hq * wq}

for v in c.values():
    q4, r4 = divmod(v, 4)
    if q4 > d[4]:
        r4 += 4 * (q4 - d[4])
        q4 = d[4]
    d[4] -= q4
    q2, r2 = divmod(r4, 2)
    d[2] -= q2
    d[1] -= r2

if any(d.values()):
    ans = "No"
else:
    ans = "Yes"

print(ans)

Submission status: Accepted

Solution code:
alpha_small_dict = {chr(i):i-97 for i in range(97, 97+26)} #辞書ver

h,w = map(int, input().split())
data = [0]*26
for i in range(h):
    a = input()
    for x in a:
        data[alpha_small_dict[x]] += 1

if h%2 == 0 and w%2 == 0:
    check = True
    for x in data:
        if x%4:
            check = False
            break
    print("Yes" if check else "No")
elif w%2 == 0:
    check = 0

    for x in data:
        if x%4:
            if x%2:
                print("No")
                exit()
            else:
                check += 2

    print("Yes" if check <= w else "No")
elif h%2 == 0:
    check = 0
    for x in data:
        if x%4:
            if x%2:
                print("No")
                exit()
            else:
                check += 2
    print("Yes" if check <= h else "No")
else:
    check1 = 0
    check2 = 0
    for x in data:
        if x%4:
            if x%2 and check2:
                print("No")
                exit()
            elif x%2:
                check2 = 1
            else:
                check1 += 2

    print("Yes" if check1 <= h + w - 2 else "No")

Submission status: Accepted

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

alphabetlist=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

count=[0 for i in range(0,26)]

for i in range(0,H):
    for j in range(0,W):
        count[alphabetlist.index(A[i][j])]+=1

data=[0 for i in range(0,4)]

for i in range(0,26):
    data[count[i]%4]+=1

if H%2==0 and W%2==0:
    if data[1]==0 and data[2]==0 and data[3]==0:
        print('Yes')
    else:
        print('No')
elif H%2==0 and W%2==1:
    h=H//2
    if data[1]==0 and data[3]==0 and h>=data[2]:
        print('Yes')
    else:
        print('No')
elif H%2==1 and W%2==0:
    w=W//2
    if data[1]==0 and data[3]==0 and w>=data[2]:
        print('Yes')
    else:
        print('No')
else:
    if data[1]+data[3]>1:
        print('No')
    elif data[1]==1:
        h=(H-1)//2
        w=(W-1)//2
        if h+w>=data[2]:
            print('Yes')
        else:
            print('No')
    else:
        h=(H-1)//2
        w=(W-1)//2
        if h+w>=data[2]+data[3]:
            print('Yes')
        else:
            print('No')

Submission status: Accepted

Solution code:
from collections import Counter

H, W = map(int, input().split())

raw = ""
for i in range(H):
    raw = raw + input()
words = Counter(list(raw))

if H*W % 2 == 1:
    flag = 0
    two_sum = (H-1) + (W-1)
elif H%2 == 1:
    flag = 1
    two_sum = W
elif W%2 == 1:
    flag = 1
    two_sum = H
else:
    flag = 1
    two_sum = 0

for value in words.values():
    if value % 2 == 1:
        flag += 1
    if flag > 1:
        print("No")
        break

if flag < 2 and two_sum > -1:
    for key, value in words.items():
        N_key = value
        while N_key > 0:
            if 1 < N_key < 4:
                N_key -= 2
                two_sum -= 2
            else:
                N_key -= 4

if flag < 2 and two_sum > -1:
    print("Yes")
elif two_sum < 0:
    print("No")

Submission status: Accepted

Solution code:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from math import sin,pi

def main():
    h,w = map(int,input().split())
    dic = {}
    for _ in range(h):
        a = list(input())
        for s in a:
            if s in dic:
                dic[s] += 1
            else:
                dic[s] = 1
    
    ans = 'Yes'
    if h%2 == 0 and w%2 == 0:
        for v in dic.values():
            if v%4 != 0:
                ans = 'No'
                cnt = 0
                break
    
    elif h%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += cnt//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                ans = 'No'
                cnt = 0
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    elif w%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                ans = 'No'
                cnt = 0
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    else:
        cnt = 0
        flag = False
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                if flag:
                    ans = 'No'
                    cnt = 0
                    break
                flag = True
                v -= 1
                if v%4 == 0:
                    cnt += v//4
                else:
                    cnt += (v - 2)//4
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    print(ans)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
al = [chr(ord('a') + i) for i in range(26)]

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

cnt = [0] * 26
for h in range(H):
    for w in range(W):
        cnt[al.index(A[h][w])] += 1
cnt = [i for i in cnt if i != 0]
cnt.sort(reverse=True)

if H % 2 == 1 and W % 2 == 1:
    count = (H//2) * (W//2)
    while count > 0:
        count -= 1
        cnt[0] -= 4
        cnt.sort(reverse=True)
    cnt = [i for i in cnt if i != 0]
    count = (H//2) + (W//2)
    while count > 0:
        count -= 1
        cnt[0] -= 2
        cnt.sort(reverse=True)
    cnt = [i for i in cnt if i != 0]
    if sum(cnt) == 1 and len(cnt) == 1:
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 0 and W % 2 == 0:
    count = (H//2) * (W//2)
    while count > 0:
        count -= 1
        cnt[0] -= 4
        cnt.sort(reverse=True)
    cnt = list(set(cnt))
    if cnt[0] == 0 and len(cnt) == 1:
        ans = "Yes"
    else:
        ans = "No"
else:
    count = (H//2) * (W//2)
    while count > 0:
        count -= 1
        cnt[0] -= 4
        cnt.sort(reverse=True)
    cnt = [i for i in cnt if i != 0]
    cnt.sort(reverse=True)
    count = (H%2) * (W//2) + (H//2) * (W%2)
    while count > 0:
        count -= 1
        cnt[0] -= 2
        cnt.sort(reverse=True)
    cnt =list(set(cnt))
    if cnt[0] == 0 and len(cnt) == 1:
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

H,W=map(int,input().split())
S=""
for i in range(H):
    S+=input()
S=Counter(S)
c=0
b=0
a=0

for i in S:

    z=S[i]
    c+=z-z%4
    z%=4
    b+=z-z%2
    z%=2
    a+=z
C=(H-H%2)*(W-W%2)
c-=C
if c<0:
    print("No")
    exit()
b+=c
b-=(H*W//2*2-C)
if b!=0:
    print("No")
    exit()
if a-H*W%2!=0:
    print("No")
    exit()

print("Yes")

Submission status: Accepted

Solution code:
import string

h, w = map(int, input().split())
Alpha = [x for x in string.ascii_lowercase]
Alpha_count = {x:0 for x in Alpha}

for _ in range(h):
    A = str(input())
    for a in A:
        Alpha_count[a] += 1

odd_count = 0
two_count = 0
four_count = 0

for a in Alpha:
    if Alpha_count[a]%2 == 1:
        odd_count += 1
    if Alpha_count[a] == 2:
        two_count += 1
    else:
        four_count += int(Alpha_count[a] // 4)
        if Alpha_count[a] >= 6 and Alpha_count[a]% 4 == 2:
            two_count += 1

if odd_count >= 2:
    ans = 'No'
elif h%2 == 0 and w%2 == 0:
    if four_count == int(h/2)*int(w/2):
        ans = 'Yes'
    else:
        ans = 'No'
elif h%2 == 0 and w%2 != 0:
    need_four_count = int(h/2)*int((w-1)/2)
    need_two_count = int(h/2)
    if four_count < need_four_count:
        ans = 'No'
    elif four_count == need_four_count:
        if two_count == need_two_count:
            ans = 'Yes'
        else:
            ans = 'No'
    elif four_count > need_four_count:
        if need_two_count == 2*(four_count - need_four_count) + two_count:
            ans = 'Yes'
        else:
            ans = 'No'
elif h%2 != 0 and w%2 == 0:
    need_four_count = int(w/2)*int((h-1)/2)
    need_two_count = int(w/2)
    if four_count < need_four_count:
        ans = 'No'
    elif four_count == need_four_count:
        if two_count == need_two_count:
            ans = 'Yes'
        else:
            ans = 'No'
    elif four_count > need_four_count:
        if need_two_count == 2*(four_count - need_four_count) + two_count:
            ans = 'Yes'
        else:
            ans = 'No'
else:
    if odd_count != 1:
        ans = 'No'
    else:
        need_four_count = int((h-1)/2)*int((w-1)/2)
        need_two_count = int((h-1)/2)+int((w-1)/2)
        if four_count < need_four_count:
            ans = 'No'
        elif four_count == need_four_count:
            if two_count == need_two_count:
                ans = 'Yes'
            else:
                ans = 'No'
        elif four_count > need_four_count:
            if need_two_count == 2*(four_count - need_four_count) + two_count:
                ans = 'Yes'
            else:
                ans = 'No'

print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
D={chr(code): 0 for code in range(ord('a'), ord('z') + 1)}
for i in range(H):
  A=list(input())
  for i in A:
    D[i]+=1
if H%2==0 and W%2==0:
  for i in D.values():
    if i%4!=0:
      print('No')
      exit()
  print('Yes')
elif H%2==1 and W%2==1:
  E=[]
  for i in D.values():
    if i%4!=0:
      E.append(i%4)
  E=sorted(E)
  a=H+W-1
  if H==1 or W==1:
    for i in range(a//2+1):
      if E==[1]+[2]*i:
        print('Yes')
        exit()
    print('No')
    exit()    
  if (a+1)//2 <len(E):
    print('No')
    exit()
  for i in range(a//2+2):
    if E==[1]+[2]*i:
      print('Yes')
      exit()
  print('No')
else:
  E=[]
  for i in D.values():
    if i%4!=0:
      E.append(i%4)
  E=sorted(E)
  if H%2==0:
    a=H
  else:
    a=W
  if a//2 <len(E):
    print('No')
    exit()
  for i in range(a//2+1):
    if E==[2]*i:
      print('Yes')
      exit()
  print('No')

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    a = [0]*26
    for i in range(h):
        s = input().rstrip()
        for c in s:
            a[ord(c) - ord("a")] += 1
    f = True
    for i in range(h//2):
        for j in range(w//2):
            sf = True
            for k in range(26):
                if a[k] >= 4:
                    a[k] -= 4
                    sf = False
                    break
            if sf:
                f = False
    if h%2 == 1:
        for j in range(w//2):
            sf = True
            for k in range(26):
                if a[k] >= 2:
                    a[k] -= 2
                    sf = False
                    break
            if sf:
                f = False
    if w%2 == 1:
        for i in range(h//2):
            sf = True
            for k in range(26):
                if a[k] >= 2:
                    a[k] -= 2
                    sf = False
                    break
            if sf:
                f = False
    if h%2 == 1 and w&2 == 1:
        sf = True
        for k in range(26):
            if a[k] >= 1:
                a[k] -= 1
                sf = False
                break
        if sf:
            f = False
    if f:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

h,w = inm()
c = [0]*26
for i in range(h):
    s = ins()
    for j in range(w):
        c[ord(s[j])-ord('a')] += 1
n1 = len([x for x in c if x%2==1])
n2 = len([x for x in c if x%4==2])

ddprint(c)
ddprint(n1)
ddprint(n2)

if h%2==0 and w%2==0:
    print('No' if n1>0 or n2>0 else 'Yes')
elif h%2==1 and w%2==1:
    k = w//2 + h//2
    print('No' if n1!=1 or n2>k else 'Yes')
else:
    k = 0
    if h%2==1:
        k = w//2
    if w%2==1:
        k += h//2
    print('No' if n1!=0 or n2>k else 'Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [input() for _ in range(h)]
dic = {}
for i in a:
    for j in range(w):
        po = i[j]
        if po in dic.keys():
            dic[po] += 1
        else: dic[po] = 1
odd = 0
mod4 = 0
for i in dic:
    if dic[i] % 2:
        odd += 1
    if dic[i] % 4:
        mod4 += 1
if h % 2 == 0 and w % 2 == 0:
    if not mod4:
        print('Yes')
    else: print('No')
elif h % 2 and w % 2:
    if odd == 1 and mod4 <= (h//2+w//2+1):
        print('Yes')
    else: print('No')
else:
    if h % 2: k = w//2
    else: k = h//2
    if odd == 0 and mod4 <= k:
        print('Yes')
    else: print('No')

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import defaultdict

def main():
    H,W = map(int,input().split())
    d = defaultdict(int)
    for _ in range(H):
        s = input()
        for st in s:
            num = ord(st)-ord("a")
            d[num] += 1
    
    use = list(d.values())
    m = [0,0,0]
    for num in use:
        m[0] += num//4
        num -= num//4*4
        m[1] += num//2
        m[2] += num%2

    if (H%2 == 0 and W%2 == 0):
        if H*W == 4*m[0]:
            print("Yes")
        else:
            print("No")
    elif (H*W)%2 == 1:
        if m[2] != 1:
            print("No")
        else:
            if m[0] >= ((H-1)*(W-1))//4:
                print("Yes")
            else:
                print("No")
    else:
        if m[2] != 0:
            print("No")
        else:
            if H%2 == 1:
                if m[1] <= W//2:
                    print("Yes")
                else:
                    print("No")
            else:
                if m[1] <= H//2:
                    print("Yes")
                else:
                    print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
h, w = map(int, input().split())
s = ''
for i in range(h):
    s += input()
c = list(Counter(s).values())
for i in range(len(c)):
    c[i] %= 4
a, b, c, d = c.count(0), c.count(1), c.count(2), c.count(3)
if b + d >= 2:
    print('No')
else:
    x = c + d
    if h % 2:
        x -= w // 2
    if w % 2:
        x -= h // 2
    if x <= 0:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
Cr = []
for _ in range(H):
  Cr += list(input())
  
from collections import Counter
Cr=Counter(Cr)

cntod=0
cnt4=0

for cr in Cr.most_common():
  c=cr[1]
  if c%2==1:
    cntod+=1
    cnt4+=(c-1)//4
  else:
    cnt4+=c//4
ret = 'No'
if H%2==0 and W%2==0:
  if cntod==0 and cnt4==(H*W)//4:
    ret = 'Yes'
if H%2==0 and W%2==1:
  if cntod==0 and cnt4>=(H*(W-1))//4:
    ret = 'Yes'
if H%2==1 and W%2==0:
  if cntod==0 and cnt4>=((H-1)*W)//4:
    ret = 'Yes'
if H%2==1 and W%2==1:
  if cntod==1 and cnt4>=((H-1)*(W-1))//4:
    ret = 'Yes'
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter

h, w = map(int, input().split())
A = []
for _ in range(h):
  A += list(input())
C = Counter(A)
X = [0]*2
for i in C.values():
  if i%2:
    X[0] += 1
  if i%4>=2:
    X[1] += 1
if X[0] <= (h%2)*(w%2) and X[1] <= (h%2) * (w//2) + (w%2) * (h//2):
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
from collections import Counter
import sys
h,w=map(int,input().split())
a=list(sys.stdin.read())
c=Counter(a)
c["\n"]=0
n1=0
n2=0
if h*w%2:
  n1=1
if h%2:
  n2+=w//2
if w%2:
  n2+=h//2
n4=(h//2)*(w//2)
n1r,n2r,n4r=0,0,0
for i in c.values():
  n1r += i%2
  n2r += (i//2)%2
  n4r += i//4
if n1==n1r and n4r>=n4:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
from collections import Counter
H,W=map(int, input().split())
A="".join(input() for _ in range(H))
C=Counter(A)
R=[0]*5
for v in C.values():
    R[4]+=v//4
    v-=v//4*4
    R[2]+=v//2
    v-=v//2*2
    R[1]+=v
R[4]-=(H//2)*(W//2)
x2=(H&1)*(W//2)+(W&1)*(H//2)
if R[4]>=0:
    R[2]+=R[4]*2
    R[4]=0
R[2]-=x2
b=R[4]==0 and R[2]==0 and R[1]==(H&1 and W&1)
print("YNeos"[not b::2])

Submission status: Accepted

Solution code:
from sys import stdin
import string
import heapq
h,w = map(int,stdin.readline().rstrip().split())
s = ""
for i in range(h):
    s += stdin.readline().rstrip()
task_4 = (h//2)*(w//2)
task_2 = (h%2)*(w//2)+(w%2)*(h//2)
task_1 = (h%2)*(w%2)
alpha = string.ascii_lowercase
li = [-s.count(i) for i in alpha if s.count(i) != 0]
heapq.heapify(li)
for i in range(task_4):
    if li[0] <= -4:
        k = heapq.heappop(li)
        if k+4 == 0:
            continue
        else:
            heapq.heappush(li,k+4)
    else:
        print("No")
        exit()
for i in range(task_2):
    if li[0] <= -2:
        k = heapq.heappop(li)
        if k+2 == 0:
            continue
        else:
            heapq.heappush(li,k+2)
    else:
        print("No")
        exit()
for i in range(task_1):
    if li[0] <= -1:
        k = heapq.heappop(li)
        if k+1 == 0:
            continue
        else:
            heapq.heappush(li,k+1)
    else:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
from collections import Counter
H, W = map(int, input().split())
counter = Counter()
for h in range(H):
    row = input()
    for a in row:
        counter[a] += 1
ans = True
if (H * W) % 2 == 0:
    for k, v in counter.items():
        if v % 2 == 0:
            continue
        ans = False
        break
    
else:
    odd_count = 0
    for k, v in counter.items():
        if v % 2 == 0:
            continue
        odd_count += 1
    if odd_count != 1:
        ans = False
if ans:
    g3 = (H // 2) * (W // 2)
    for k, v in counter.items():
        if v >= 4:
            g3 -= v // 4
    if g3 <= 0:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
a = [input() for _ in range(h)]

a_set = set()
for i in range(h):
    for j in range(w):
        a_set.add(a[i][j])

odd = 0
mul_2 = 0
for s in a_set:
    tmp = 0
    for i in range(h):
        tmp += a[i].count(s)
    odd += (tmp%2 == 1) * 1
    mul_2 += ((tmp%2 == 0) & (tmp%4 == 2)) * 1


limit_odd = ( h*w % 2 == 1) * 1
limit_mul_2 = (h%2 == 1) * (w//2) + (w%2 == 1) * (h//2)
if((odd <= limit_odd) & (mul_2 <= limit_mul_2)):
    print('Yes')
else:
    print('No')