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:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif (H*W)%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and y <= H//2 and z == 0 else 'No')
        else:
            a = b = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
                b += D[c]//4
            print('Yes' if a == 1 and b >= (H//2)*(W//2) and len(D.keys()) <= ((H+1)//2)*((W+1)//2) else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
H, W = map(int, input().split(' '))
d = defaultdict(int)
for i in range(H):
    for k, v in Counter(input()).items():
        d[k] += v
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 == 0 or W % 2 == 0:
    cnt = H//2 if not H % 2 else W//2
    for i in d.values():
        if i % 4 == 0:
            continue
        elif i % 2 == 0:
            cnt -= 1
            if cnt >= 0:
                continue
        print('No')
        exit()
    print('Yes')
else:
    odd = 1
    cnt = (H+W-2)//2
    for i in d.values():
        if i % 2 != 0 and odd:
            odd -= 1
            continue
        elif i % 4 == 0:
            continue
        elif i % 2 == 0 and cnt:
            cnt -= 1
            continue
        else:
            print('No')
            exit()
    print('Yes')

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce
from operator import add

h, w = map(int, input().split())
a_counts = [Counter(input()) for _ in range(h)]
counts = reduce(add, a_counts)

if h%2==0 and w%2==0:
    if all([x%4==0 for x in counts.values()]):
        print('Yes')
        exit()
    else:
        print('No')
        exit()
elif h%2==0 and w%2==1:
    mod4 = [x%4 for x in counts.values()]
    mod2 = [x%2 for x in mod4 if x!=0]

    if all([x==0 for x in mod2]) and len(mod2) <= h//2:
        print('Yes')
        exit()
    else:
        print('No')
        exit()
elif h%2==1 and w%2==0:
    mod4 = [x%4 for x in counts.values()]
    mod2 = [x%2 for x in mod4 if x!=0]

    if all([x==0 for x in mod2]) and len(mod2) <= w//2:
        print('Yes')
        exit()
    else:
        print('No')
        exit()
else:
    mod4 = [x%4 for x in counts.values()]
    mod2 = [x%2 for x in mod4 if x!=0]
    mod2_0 = [x for x in mod2 if x==0]
    mod2_1 = [x for x in mod2 if x==1]

    if len(mod2_0) <= w//2 + h//2 and len(mod2_1) <= 1:
        print('Yes')
        exit()
    else:
        print('No')
        exit()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    h,w=map(int,input().split())
    s=""
    for _ in range(h):
        s+=input()
    sc=list(Counter(s).values())
    cunt_1=(h%2)*(w%2)
    cunt_2=(h%2)*(w//2)+(w%2)*(h//2)
    cunt_4=(h//2)*(w//2)
    #print(sc)
    for i,scc in enumerate(sc):
        if scc>=4:
            x=min(scc//4,cunt_4)
            sc[i]-=x*4
            cunt_4-=x
        if cunt_4==0:break
    else:
        print("No")
        sys.exit()
    for i,scc in enumerate(sc):
        if scc>=2:
            x=min(scc//2,cunt_2)
            sc[i]-=x*2
            cunt_2-=x
        if cunt_2==0:break
    else:
        print("No") 
        sys.exit()
    for i,scc in enumerate(sc):
        if scc>=1:
            x=min(scc,cunt_1)
            sc[i]-=x
            cunt_1-=x
        if cunt_1==0:break
    else:
        print("No")   
        sys.exit()  
    print("Yes")

        

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    h,w=map(int,input().split())
    s=""
    for _ in range(h):
        s+=input()
    sc=list(Counter(s).values())
    cunt_1=(h%2)*(w%2)
    cunt_2=(h%2)*(w//2)+(w%2)*(h//2)
    cunt_4=(h//2)*(w//2)
    #print(sc)
    for i,scc in enumerate(sc):
        if scc>=4:
            x=min(scc//4,cunt_4)
            sc[i]-=x*4
            cunt_4-=x
    for i,scc in enumerate(sc):
        if scc>=2:
            x=min(scc//2,cunt_2)
            sc[i]-=x*2
            cunt_2-=x
    for i,scc in enumerate(sc):
        if scc>=1:
            x=min(scc,cunt_1)
            sc[i]-=x
            cunt_1-=x
    print("Yes" if all(scc==0 for scc in sc) else "No")

        

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a={}
for i in range(26):
  a[chr(i+97)]=0
for i in range(h):
  s=input()
  for j in s:
    a[j]+=1
if h==1 and w==1:print("Yes")
elif h%2==0 and w%2==0:
  for i in a.values():
    if i%4!=0:print("No");exit()
  print("Yes")
elif h%2==1 and w%2==0:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>w//2:
    print("No")
  else:print("Yes")
elif h%2==0 and w%2==1:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt3=0;cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
    elif i%4==3:cnt3+=1
  if cnt1+cnt3!=1:print("No")
  elif cnt2+cnt3>(h-1)//2+(w-1)//2:print("No")
  else:
    print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict
h, w = [int(i) for i in input().split()]
num_count = defaultdict(int)
for i in range(h):
    for j in input():
        num_count[j] += 1
#finish counting

n1 = (h%2) * (w%2)
n2 = h%2 * (w//2)*2 + w%2 * (h//2)*2 # I would have exactly e
n4 = h * w - n1 - n2

for i in num_count:
    while num_count[i] % 4 in [1,3]:
        num_count[i] -= 1
        n1 -= 1
if n1 != 0:
    print("No")
else:
    for i in num_count:
        while num_count[i] % 4  == 2:
            num_count[i] -= 2
            n2 -= 2
    if n2 < 0: #it's ok to have leftover as it can be covered by 4
        print("No")
    else:
        n4 += n2 # those that are not covered yet
        for i in num_count:
            while num_count[i] % 4 == 0 and num_count[i] > 0:
                num_count[i] -= 4
                n4 -= 4
        if n4 != 0:
            print("No")
        else:
            print("Yes")

Submission status: Accepted

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

d_al = dict()

for ai in a:
    for aij in ai:
        if aij in d_al:
            d_al[aij] += 1
        else:
            d_al[aij] = 1

od = 0
a2 = 0

for i in d_al.values():
    if i%2 == 1:
        od += 1
    elif i%4 == 2:
        a2 += 1

if h%2==1 and w%2==1:
    if od == 1 and a2 <= (h+w-1)//2:
        print("Yes")
    else:
        print("No")
elif h%2==1:
    if od == 0 and a2 <= w//2:
        print("Yes")
    else:
        print("No")
elif w%2==1:
    if od == 0 and a2 <= h//2:
        print("Yes")
    else:
        print("No")
else:
    if od == 0 and a2 == 0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f=o=0
for i in Counter(''.join(t)).values():f+=i//4;o+=i&1
print('YNeos'[o-h%2>h*w%2or f-h//4<h//2*(w//2)::2])

Submission status: Accepted

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o-h%2>h*w%2or f-h//4<=h//2*(w//2)::2])

Submission status: Accepted

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o-h%2>h%2*w%2or f-h//4<=h//2*(w//2)::2])

Submission status: Accepted

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t[:-1]for t in t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o>h%2*w%2or h//2*(w//2)>=f::2])

Submission status: Accepted

Solution code:
import collections
h,w=map(int,input().split())
a=""
for i in range(h):
    a+=input()
f,o=1,0
for i in collections.Counter(a).values():
    f+=i//4
    o+=i&1
print(["No","Yes"][f>(h//2)*(w//2) and o<=h%2*w%2])

Submission status: Accepted

Solution code:
import collections
h,w=map(int,input().split())
a=""
for i in range(h):
    a+=input()
f,o=1,0
for i in collections.Counter(a).values():
    f+=i//4
    o+=i%4%2
print(["No","Yes"][f>(h//2)*(w//2) and o<=h%2*w%2])

Submission status: Accepted

Solution code:
import collections
h,w=map(int,input().split())
a=""
for i in range(h):
    a+=input()
f,o=1,0
for i in collections.Counter(a).values():
    f+=i//4
    o+=i%4%2
print("Yes" if f>(h//2)*(w//2) and o<=h%2*w%2 else "No")

Submission status: Accepted

Solution code:
from collections import Counter
h,w=map(int,input().split())
a=""
for i in range(h):
    a+=input()
f,o=0,0
for i in Counter(a).values():
    f+=i//4
    o+=i%4%2
print("Yes" if f>=(h//2)*(w//2) and o<=(h%2)*(w%2) else "No")

Submission status: Accepted

Solution code:
from collections import Counter

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

ctr = Counter()
for s in X:
    for c in s:
        ctr[c] += 1

if H % 2 == 0 and W % 2 == 0:
    flg1 = all(v % 4 == 0 for v in ctr.values())
    flg2 = True
elif H % 2 == 1 and W % 2 == 1:
    flg1 = sum(v % 2 == 1 for v in ctr.values()) == 1
    flg2 = sum(v % 4 == 2 for v in ctr.values()) <= H // 2 + W // 2
else:
    flg1 = all(v % 2 == 0 for v in ctr.values())
    flg2 = sum(v % 4 == 2 for v in ctr.values()) <= (W // 2 if W % 2 == 0 else H // 2)

if flg1 and flg2:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[input() for _ in range(h)]
from collections import defaultdict
d=defaultdict(lambda:0)
for ai in a:
    for aij in ai:
        d[aij]+=1
if h%2==0 and w%2==0:
    print('Yes' if all(v%4==0 for v in d.values()) else 'No')
elif h%2==0 and w%2==1:
    c=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            c+=1
        else:
            print('No')
            exit()
    print('Yes' if c<=h//2 else 'No')
elif h%2==1 and w%2==0:
    c=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            c+=1
        else:
            print('No')
            exit()
    print('Yes' if c<=w//2 else 'No')
else:
    ce=0
    co=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            ce+=1
        else:
            co+=1
    print('Yes' if ce<=h//2+w//2 and co==1 else 'No')

Submission status: Accepted

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

def moji_num(s):
    return ord(s) - ord('a')

if h % 2 == 0:
    if w % 2 == 0:
        pat = 1
    else:
        pat = 2
else:
    if w % 2 == 0:
        pat = 3
    else:
        pat = 4

moji = [0]*(26)

for i in range(h):
    for j in range(w):
        moji[moji_num(aij[i][j])] += 1
        
#print(moji)

b1 = 0
b2 = 0

for i in range(26):
    if moji[i] % 2 == 1:
        tmp = moji[i]
        if tmp % 4 == 3:
            b2 += 1
        #b1_li.append(moji_num(moji[i]))
        b1 += 1
        #print(i)
    elif moji[i] % 2 == 0 and moji[i] % 4 != 0:
        b2 += 1


#print(pat,b1,b2)        
        
if b1 > 1:
    print('No')
    exit()
elif pat == 1:
    if b2 > 0:
        print('No')
        exit()
elif pat == 2:
    if b2 > h//2:
        print('No')
        exit()
elif pat == 3:
    if b2 > w//2:
        print('No')
        exit()
elif pat == 4:
    if b2 > (h+w-2)//2:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c

from collections import Counter, defaultdict

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
c = defaultdict(int)
for row in A:
    for ch in row:
        c[ch] += 1

# 両方偶数 = 全部4回ずつ必要
if H % 2 == 0 and W % 2 == 0:
    if all(v % 4 == 0 for v in c.values()):
        print("Yes")
    else:
        print("No")
    exit()

# 両方奇数 (縦・横の中心がある)
elif H % 2 == 1 and W % 2 == 1:
    c_odd = sum(1 if v % 2 == 1 else 0 for v in c.values())
    if c_odd == 1 and sum(1 if v % 4 == 2 else 0 for v in c.values()) <= (W // 2) + (H // 2):
        print("Yes")
    else:
        print("No")
    exit()

# 片方が奇数
else:
    if all(v % 2 == 0 for v in c.values()) and \
        sum(1 if v % 4 == 2 else 0 for v in c.values()) <= \
            (W // 2 if W % 2 == 0 else H // 2):
        print("Yes")
    else:
        print("No")
    exit()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())
    A = []
    for i in range(H):
        a = list(input())
        A.extend(a)

    D = Counter(A)
    B = [["" for _ in range(W)] for _ in range(H)]
    if H > 1 and W > 1:
        for h in range(H // 2):
            for w in range(W // 2):
                for k, v in D.items():
                    if v >= 4:
                        B[h][w] = k
                        B[h][-(w + 1)] = k
                        B[-(h + 1)][w] = k
                        B[-(h + 1)][-(w + 1)] = k
                        D[k] -= 4
                        break

        if H % 2 != 0:
            for w in range(W // 2):
                for k, v in D.items():
                    if v >= 2:
                        B[H // 2][w] = k
                        B[H // 2][-(w + 1)] = k
                        D[k] -= 2
                        break
        if W % 2 != 0:
            for h in range(H // 2):
                for k, v in D.items():
                    if v >= 2:
                        B[h][W // 2] = k
                        B[-(h + 1)][W // 2] = k
                        D[k] -= 2
                        break
        if H % 2 != 0 and W % 2 != 0:
            for k, v in D.items():
                if v >= 1:
                    B[H // 2][W // 2] = k
                    D[k] -= 1
                    break
    elif W == 1 and H > 1:
        for h in range(H // 2):
            for k, v in D.items():
                if v >= 2:
                    B[h][0] = k
                    B[-(h + 1)][0] = k
                    D[k] -= 2
                    break
        if H % 2 != 0:
            for k, v in D.items():
                if v >= 1:
                    B[H // 2][0] = k
                    D[k] -= 1
                    break
    elif H == 1 and W > 1:
        for w in range(W // 2):
            for k, v in D.items():
                if v >= 2:
                    B[0][w] = k
                    B[0][-(w + 1)] = k
                    D[k] -= 2
                    break
        if W % 2 != 0:
            for k, v in D.items():
                if v >= 1:
                    B[0][W // 2] = k
                    D[k] -= 1
                    break
    else:
        print("Yes")
        exit()

    for i in B:
        if "" in set(i):
            print("No")
            break
    else:
        print("Yes")


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

ans = 0
for i in range(h-1):
    a = a+ input()

from collections import Counter

c = Counter(a)

cm = c.most_common()


if h*w%2==1:
    cnt_odd = 0
    cnt_2 =0
    limit_2 = h//2 + w//2

    for k,v in cm:
        if v%2==1:
            cnt_odd+=1
            if cnt_odd >1:
                print('No')
                exit()
        elif v%4!=0:
            cnt_2+=1
            if cnt_2 >limit_2:
                print('No')
                exit()
    if cnt_odd == 0:
        print('No')
    else:
        print('Yes')

else:
    cnt_odd = 0
    cnt_2 =0
    if h%2!=0:
        limit_2 = w//2
    elif w%2!=0:
        limit_2 = h//2
    else:
        limit_2 = 0

    for k,v in cm:
        if v%2==1:
            cnt_odd+=1
            if cnt_odd >0:
                print('No')
                exit()
        elif v%4!=0:
            cnt_2+=1
            if cnt_2 >limit_2:
                print('No')
                exit()
    print('Yes')

Submission status: Accepted

Solution code:
from sys import stdin
from collections import Counter
H,W = [int(x) for x in stdin.readline().rstrip().split()]
c = Counter()

for i in range(H):
    c += Counter(list(input()))

rem0 = 0
rem2 = 0

for i in c.values():
    if i % 4 == 0:
        rem0 += 1
    elif i % 4 == 2:
        rem2 += 1

num_v = len(c.values())

f = True
if H % 2 == 0 and W % 2 == 0:
    if rem0 != num_v:
        f = False

elif H % 2 != 0 and W % 2 != 0:
    
    if len([i for i in c.values() if i % 2 != 0]) != 1:
        f = False
        
    if rem2 > (H-1)/2 + (W-1)/2:
        f = False
    
    if num_v-1 != rem0 + rem2:
        f = False
    
else:
    if H % 2 == 0:
        if rem2 > H/2:
            f = False
        if rem2 + rem0 != num_v:
            f = False
            
    if W % 2 == 0:
        if rem2 > W/2:
            f = False
        if rem2 + rem0 != num_v:
            f = False
    
if f:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

c=Counter(sum(a,[]))

if h%2==0 and w%2==0:
    g1=0
    g2=0
    g4=(h//2)*(w//2)
elif h%2==1 and w%2==0:
    g1=0
    g2=w//2
    g4=(h//2)*(w//2)
elif h%2==0 and w%2==1:
    g1=0
    g2=h//2
    g4=(h//2)*(w//2)
else:
    g1=1
    g2=(h//2)+(w//2)
    g4=(h//2)*(w//2)

g1_cnt=0
g2_cnt=0
g4_cnt=0

for key in c:
    if c[key]%4==1:
        g1_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g2_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g1_cnt+=1
        g2_cnt+=1
        g4_cnt+=c[key]//4
    else:
        g4_cnt+=c[key]//4

# print(g1,g2,g4)

# print(g1_cnt,g2_cnt,g4_cnt)

flag=False
if g1==g1_cnt:
    if g2==g2_cnt and g4==g4_cnt:
        flag=True
    elif g2_cnt<g4_cnt and g2>g4 and g2*2+g4*4==g2_cnt*2+g4_cnt*4:
        flag=True

print('Yes' if flag else 'No')

Submission status: Accepted

Solution code:
import sys
from string import ascii_lowercase as AL

input = sys.stdin.readline


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

    alpha = {}
    for al in AL:
        alpha[al] = 0
    for i in range(H):
        a = input().rstrip()
        for s in a:
            alpha[s] += 1

    values = list(alpha.values())

    q_h, r_h, = divmod(H, 2)
    q_w, r_w, = divmod(W, 2)
    g_4 = q_h * q_w
    g_2 = q_h * r_w + q_w * r_h
    g_1 = r_h * r_w

    for i in range(len(values)):
        while g_4 > 0:
            if values[i] >= 4:
                g_4 -= 1
                values[i] -= 4
            else:
                break
    for i in range(len(values)):
        while g_2 > 0:
            if values[i] >= 2:
                g_2 -= 1
                values[i] -= 2
            else:
                break
    for i in range(len(values)):
        while g_1 > 0:
            if values[i] == 1:
                g_1 -= 1
                values[i] = 0
            else:
                break

    if g_4 == g_2 == g_1 == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

a=[]
for _ in range(h):
    a+=list(input())

four=(h//2)*(w//2)
two=h%2*w//2+w%2*h//2
one=(h%2)*(w%2)

import collections
c = collections.Counter(a)

#print(four,two,one)
d=list(c.values())
d.sort()

for item in d:
    if item==1:
        if one==1:
            one-=1
        else:
            print("No")
            exit()
    elif item==2:
        if two>0:
            two-=1
        else:
            print("No")
            exit()
    elif item==3:
        if one==1 and two>0:
            one-=1
            two-=1
        else:
            print("No")
            exit()
    else:
        if item%2==1:
            if one==0:
                print("No")
                exit()
            else:
                item-=1
                one-=1
        #print(item,four,two,one)
        while item>=4 and four>0:
            item-=4
            four-=1
        #print(item,four,two,one)
        
        while item>=2:
            item-=2
            two-=1
        #print(item,four,two,one)
    
if one==0 and two==0 and four==0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]
    A = [input().strip() for _ in range(H)]

    c = collections.Counter()
    for a in A:
        for aa in a:
            c[aa] += 1

    four_cnt = (H // 2) * (W // 2)
    two_cnt = (H % 2) * (W // 2) + (W % 2) * (H // 2)
    one_cnt = (H % 2) * (W % 2)

    for k in c.keys():
        if c[k] >= 4:
            q, r = divmod(c[k], 4)
            mi = min(four_cnt, q)
            c[k] -= mi * 4
            four_cnt -= mi

    for k in c.keys():
        if c[k] >= 2:
            q, r = divmod(c[k], 2)
            mi = min(two_cnt, q)
            c[k] -= mi * 2
            two_cnt -= mi

    for k in c.keys():
        if c[k] >= 1:
            q, r = divmod(c[k], 1)
            mi = min(one_cnt, q)
            c[k] -= mi * 1
            one_cnt -= mi

    if sum([four_cnt, two_cnt, one_cnt]) == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
h,w = map(int,input().split())
chk = []
for i in range(h):
  a = list(input().strip())
  for j in a:
    chk.append(j)
    
from collections import Counter
c = Counter(chk)
ans = 'Yes'
cnt = 0
q = 0

if h%2 == 0 and w%2 == 0:
  for i in c.values():
    if i%4 != 0:
      ans = 'No'
      break
      
elif h%2 == 0 and w%2 == 1:
  for i in c.values():
    if i%2 != 0:
      ans = 'No'
      break
    if i%4 == 2:
      cnt += 1
    if cnt > h//2:
      ans = 'No'
      break
    
elif w%2 == 0 and h%2 == 1:
  for i in c.values():
    if i%2 != 0:
      ans = 'No'
      break
    if i%4 == 2:
      cnt += 1
    if cnt > w//2:
      ans = 'No'
      break
      
elif h%2 == 1 and w%2 == 1:
  for i in c.values():
    if i%2 != 0:
      q += 1
    if q > 1:
      ans = 'No'
      break
    if i%4 == 2:
      cnt += 1
    if cnt > h//2+w//2:
      ans = 'No'
      break
      
print(ans)

Submission status: Accepted

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

if h % 2 == 0 and w % 2 == 0:
    for k , v in c.items():
        if v % 4 != 0:
            print("No")
            exit()
    print("Yes")
    
elif h % 2 != 0 and w % 2 != 0:
    m = 0
    n = 0
    for k , v in c.items():
        if v % 4 == 1:
            n += 1
            if n > 1:
                print("No")
                exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2 + h//2:
                print("No")
                exit()
        elif v % 4 == 3:
            n += 1
            m += 1
            if n > 1:
                print("No")
                exit()
            if m > w//2 + h//2:
                print("No")
                exit()
        
    print("Yes")
elif h % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1 or v % 4 == 3:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2:
                print("No")
                exit()
    print("Yes")
elif w % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1 or v % 4 == 3:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > h//2:
                print("No")
                exit()
    print("Yes")

Submission status: Accepted

Solution code:
from collections import Counter
H,W=map(int,input().split())
li=[]
for i in range(H):
    a=list(input())
    li+=a
c=Counter(li)
t,o=0,0
for x,y in c.items():
    l=y%4
    t+=l//2
    o+=l%2
isok=True
if(H%2==0 and W%2==0):
    if t!=0 or o!=0:
        isok=False
elif(H%2==0 and W%2==1):
    if(t>H//2 or o>0):
        isok=False
elif(H%2==1 and W%2==0):
    if(t>W//2 or o>0):
        isok=False
else:
    if(t>W//2+H//2 or o>1):
        isok=False
print('Yes' if isok else 'No')

Submission status: Accepted

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len(list((set(D))))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = (H*W-(H//2)*(W//2)*4)//2+1
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m <= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        seigen = (H*W)//4
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s <= seigen)and(q == 0)):
            print("Yes")
        else:
            print("No")

    else:
        q = 0
        e = 0
        if ((H%2) == 1):
            seigen = (H//2+1)*W
            seigen2 = W//2
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
            seigen2 = H//2
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                e += 1
        if (q == 0)and(s<=seigen)and(e<=seigen2):
            print("Yes")
        else:
            print("No")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=''
for _ in range(h):
  s+=input()
from collections import Counter as c
v=list(c(s).values())
ki=0;ni=0;
for i in v:
  if i%2:
    ki+=1
  elif i%4:
    ni+=1
x='No'
if (h%2)*(w%2):
  if ki==1 and ni<=(h+w-2)//2:
    x='Yes'
elif h%2:
  if ki==0 and ni<=w//2:
    x='Yes'
elif w%2:
  if ki==0 and ni<=h//2:
    x='Yes'
else:
  if ki==0 and ni==0:
    x='Yes'
print(x)

Submission status: Accepted

Solution code:
from collections import Counter

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

num = [0]*5

for h in range(-(-H//2)):
    for w in range(-(-W//2)):
        tmp = 1
        if h < H//2:
            tmp *= 2
        if w < W//2:
            tmp *= 2
        num[tmp] += 1

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

c = list(Counter(s).values())
c.sort(reverse = True)

four = 0
for i in range(len(c)):
    if four == num[4]:
        break
    while c[i] >= 4:
        c[i] -= 4
        four += 1
        if four == num[4]:
            break

two = 0
for i in range(len(c)):
    if two == num[2]:
        break
    while c[i] >= 2:
        c[i] -= 2
        two += 1
        if two == num[2]:
            break

print("Yes" if sum(c) == num[1] and four == num[4] and two == num[2] else "No")

Submission status: Accepted

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

ss = []
for i in range(h):
    for j in range(w):
        ss.append(s[i][j])
            
b = Counter(ss)
odd = 0
    
if h % 2 == 0 and w % 2 == 0:
    fl = 1
    for i in b:
        if b[i] % 4 != 0:
            fl = 0
            
    if fl == 1:
        print("Yes")
    else:
        print("No")
        
elif h % 2 == 1 and w % 2 == 0:
    flsum = 0
    for i in b:
        flsum += b[i] % 4
        if b[i] % 2 == 1:
            odd += 1
        
    if odd != 0:
        print("No")
    elif flsum >= 0 and flsum <= w:
        print("Yes")
    else:
        print("No")
        
elif h % 2 == 0 and w % 2 == 1:
    flsum = 0
    for i in b:
        flsum += b[i] % 4
        if b[i] % 2 == 1:
            odd += 1
        
    if odd != 0:
        print("No")
    elif flsum >= 0 and flsum <= h:
        print("Yes")
    else:
        print("No")
        
else:
    flsum = 0
    for i in b:
        flsum += b[i] % 4
        if b[i] % 2 == 1:
            odd += 1
        
    if odd != 1:
        print("No")
    elif flsum >= 0 and flsum <= h+w-1:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

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

f = (h-h%2)*(w-w%2)//4
t = 0
one = 0
if h%2==1:
    t += w//2
if w%2==1:
    t += h//2
if h%2==1 and w%2==1:
    one = 1

dic = {}
for i in range(h):
    a = list(input())
    for j in a:
        if j in dic:
            dic[j] += 1
        else:
            dic[j] = 1

result = True
for i, j in dic.items():
    if j%2==1:
        if one == 1:
            j -= 1
            one -= 1
        else:
            result = False
    if j==0:
        continue
    while f > 0 and j>=4:
        j -= 4
        f -= 1
        if j==0:
            break
    if j==0:
        continue
    while t > 0 and j>=2:
        j -= 2
        t -= 1
        if j==0:
            break
    if j==0:
        continue
    else:
        result = False

    if result == False:
        break

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