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 Counter

H, W = map(int, input().split())
a = ''
for i in range(H):
    a += input()

c = Counter(a)
n = c.values()
T = [0,
     sum(ni // 4 for ni in n),
     sum((ni % 4) // 2 for ni in n),
     sum(ni % 2 for ni in n)]
E = [(H // 2) * (W // 2),
     (H // 2) * (W % 2) + (H % 2) * (W // 2),
     (H % 2) * (W % 2)]

ans = True
for i in range(3):
    T[i + 1] += T[i] * 2
    T[i + 1] -= E[i]
    if T[i + 1] < 0:
        ans = False
        break
print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
import bisect

h, w = map(int, input().split())
figure_dic = {}
def add_dic(figure):
    if not figure in figure_dic:
        figure_dic[figure] = 0
    figure_dic[figure] += 1
for i in range(h):
    line = list(input())
    for j in line:
        add_dic(j)
figures = []
for key, items in figure_dic.items():
    figures.append(items)
figures.sort()
same_boxes = []
if h % 2 == 0:
    if w % 2 == 0:
        same_boxes += [4 for i in range(h*w//4)]
    else:
        same_boxes += [4 for i in range((w//2)*(h//2))]
        same_boxes += [2 for i in range(h//2)]
else:
    if w % 2 == 0:
        same_boxes += [4 for i in range((h//2)*(w//2))]
        same_boxes += [2 for i in range(w//2)]
    else:
        same_boxes += [4 for i in range((h//2)*(w//2))]
        same_boxes += [2 for i in range(h//2 + w//2)]
        same_boxes += [1]
same_boxes.sort(reverse=True)
l = len(figures)
flag = 0
for box in same_boxes:
    left = bisect.bisect_left(figures, box)
    if left == l:
        flag = 1
        break
    figures[left] -= box
    figures.sort()
if flag:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
import collections
H,W=map(int,input().split())
table=[list(input()) for _ in range(H)]
ch=[]
for i in range(H):
    for j in range(W):
        ch.append(table[i][j])
c=collections.Counter(ch)
values=list(c.values())	#aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key=list(c.keys())	#先のvalue値に相当する要素のリスト(要素1,要素2,…)
count4=0
count2=0
count1=0
for i in values:
    if i%4==0:
        count4=count4+1
    elif i%2==0:
        count2=count2+1
    else:
        count1=count1+1
if (H%2==0)and(W%2==0):
    if count2+count1==0:
        print("Yes")
    else:
        print("No")
elif (H%2==1)and(W%2==1):
    if (count2<=((H+W-2)//2))and(count1==1):
        print("Yes")
    else:
        print("No")
else:
    if H%2==0:
        nu=H//2
    else:
        nu=W//2
    if (count2<=nu)and(count1==0):
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa
from collections import Counter


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


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

    A.sort()
    B = Counter(A).most_common()
    # print(A)
    # print(B)
    success = False
    if H % 2 == 0 and W % 2 == 0:
        if all([val[1] % 4 == 0 for val in B]):
            success = True
    elif H % 2 == 1 and W % 2 == 0:
        if all([val[1] % 2 == 0 for val in B]):
            four = 0
            two = 0
            for el in B:
                x = el[1]
                if x % 4 == 0:
                    four += 1
                elif x % 2 == 0:
                    two += 1
            if two <= W // 2 and (W // 2 - two) % 2 == 0:
                success = True
    elif H % 2 == 0 and W % 2 == 1:
        if all([val[1] % 2 == 0 for val in B]):
            four = 0
            two = 0
            for el in B:
                x = el[1]
                if x % 4 == 0:
                    four += 1
                elif x % 2 == 0:
                    two += 1
            if two <= H // 2 and (H // 2 - two) % 2 == 0:
                success = True
    else:
        odd = 0
        two = 0
        four = 0
        for el in B:
            x = el[1]
            if x % 2 == 1:
                odd += 1
                x -= 1
            if x % 4 == 0:
                four += 1
            elif x % 2 == 0:
                two += 1
        if odd == 1:
            if two <= H // 2 + W // 2 and (H // 2 + W // 2 - two) % 2 == 0:
                success = True
    if success:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections

H, W = map(int, input().split())
a_frq_dict = collections.defaultdict(int)
for _ in range(H):
    for a in input():
        a_frq_dict[a] += 1


n = H%2 + W%2
frq1, frq2, frq4 = 0,0,0

if n == 2:
    frq1 = 1
    frq2 = (H-1)//2 + (W-1)//2
    frq4 = ((H*W) - (frq2*2 + frq1)) // 4

elif n == 1:
    if H%2:
        frq2 = W//2
    else:
        frq2 = H//2

    frq4 = ((H*W) - frq2*2) // 4

else:
    frq4 = (H * W) // 4


# print(frq1, frq2, frq4)


for frq in a_frq_dict.values():
    n = min(frq4, frq//4)
    frq4 -= n
    frq -= n*4

    n = min(frq2, frq//2)
    frq2 -= n
    frq -= n*2

    n = min(frq1, frq)
    frq1 -= n
    frq -= n

    # print(frq)

    if frq:
        print('No')
        exit()


# print(frq1, frq2, frq4)

if all(map(lambda x: x == 0, [frq1, frq2, frq4])):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
# ノートに考察メモあり。
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'

        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 2
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H:
                return 'Yes'
            else:
                return 'No'

        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 2
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W:
                return 'Yes'
            else:
                return 'No'

        else:
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 2
                elif i % 4 == 3:
                    cnt2 += 2
                    cnt1 += 1
            if cnt1 == 1 and cnt2 <= (H + W - 2):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Accepted

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

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

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

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

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

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



    print(ans)
    return

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

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

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

Submission status: Accepted

Solution code:
import collections

H, W  = map(int, input().split())
A = []
for _ in range(H):
    for c in input():
        A.append(c)
A.sort()
B = collections.Counter(A).most_common()
# print(A)
# print(B)
success = False
if H % 2 == 0 and W % 2 == 0:
    if all([val[1] % 4 == 0 for val in B]):
        success = True
elif H % 2 == 1 and W % 2 == 0:
    if all([val[1] % 2 == 0 for val in B]):
        four = 0
        two = 0
        for el in B:
            x = el[1]
            if x % 4 == 0:
                four += 1
            elif x % 2 == 0:
                two += 1
        if two <= W // 2 and (W // 2 - two) % 2 == 0:
            success = True
elif H % 2 == 0 and W % 2 == 1:
    if all([val[1] % 2 == 0 for val in B]):
        four = 0
        two = 0
        for el in B:
            x = el[1]
            if x % 4 == 0:
                four += 1
            elif x % 2 == 0:
                two += 1
        if two <= H // 2 and (H // 2 - two) % 2 == 0:
            success = True
else:
    odd = 0
    two = 0
    four = 0
    for el in B:
        x = el[1]
        if x % 2 == 1:
            odd += 1
            x -= 1
        if x % 4 == 0:
            four += 1
        elif x % 2 == 0:
            two += 1
    if odd == 1:
        if two <= H // 2 + W // 2 and (H // 2 + W // 2- two) % 2 == 0:
            success = True
if success:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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


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


def S(): return input()


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


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


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


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


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


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


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


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


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

show_flg = False
# show_flg = True


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

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

    for i in range((H+1)//2):
        for j in range((W+1)//2):
            V = 4
            if H % 2 != 0 and i == (H+1)//2-1:
                V //= 2
            if W % 2 != 0 and j == (W+1)//2-1:
                V //= 2
            f = False

            if V == 2:
                for k in range(len(alpha)):
                    if alpha[k] % 2 == 0 and alpha[k] % 4 == 2:
                        f = True
                        alpha[k] -= V
                        break

            if not f:
                for k in range(len(alpha)):
                    if alpha[k] >= V:
                        f = True
                        alpha[k] -= V
                        break
            # print(i+1, j+1, V, alpha)
            if not f:
                print('No')
                return
    # print(alpha)
    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

H, W = MAP()
a = [input() for _ in range(H)]
cnt = defaultdict(int)
for l in a:
	for s in l:
		cnt[s] += 1

l = sorted(list(cnt.values()), reverse=True)
if H%2==0 and W%2==0:
	for v in cnt.values():
		if v%4 != 0:
			print("No")
			break
	else:
		print("Yes")
elif H%2==0 and W%2==1:
	tmp = (W-1)*H//4
	if tmp:
		for i in range(len(l)):
			if l[i] >= 4:
				take = l[i]//4
				if tmp - take > 0:
					l[i] -= 4*take
					tmp -= take
				else:  # 取りきった
					l[i] -= 4*tmp
					tmp = 0
					break	
		else:
			print("No")
			exit()
	for v in l:
		if v%2 != 0:
			print("No")
			break
	else:
		print("Yes")
elif H%2==1 and W%2==0:
	tmp = (H-1)*W//4
	if tmp:
		for i in range(len(l)):
			if l[i] >= 4:
				take = l[i]//4
				if tmp - take > 0:
					l[i] -= 4*take
					tmp -= take
				else:  # 取りきった
					l[i] -= 4*tmp
					tmp = 0
					break	
		else:
			print("No")
			exit()
	for v in l:
		if v%2 != 0:
			print("No")
			break
	else:
		print("Yes")
else:
	tmp = (H-1)*(W-1)//4
	if tmp:
		for i in range(len(l)):
			if l[i] >= 4:
				take = l[i]//4
				if tmp - take > 0:
					l[i] -= 4*take
					tmp -= take
				else:  # 取りきった
					l[i] -= 4*tmp
					tmp = 0
					break	
		else:
			print("No")
			exit()
	odd = 0
	for v in l:
		if v%2 == 1:
			odd += 1
	print("Yes" if odd == 1 else "No")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split(" "))
four, two, one = 0, 0, 0

four += ((H // 2)) * (W // 2)
if W % 2 == 1:
    two += (H // 2)
if H % 2 == 1:
    two += (W // 2)

one += (H * W) % 2

counter = {}
for _ in range(H):
    A = input()
    for a in A:
        if a in counter:
            counter[a] += 1
        else:
            counter[a] = 1

flag = True
for c in counter:
    if counter[c] >= 4 and four > 0:
        mk_four = min(four, counter[c] // 4)
        four -= mk_four
        counter[c] -= mk_four * 4
    if counter[c] >= 2 and two > 0:
        mk_two = min(two, counter[c] // 2)
        two -= mk_two
        counter[c] -= mk_two * 2
    if counter[c] >= 1 and one > 0:
        mk_one = min(one, counter[c])
        one -= mk_one
        counter[c] -= mk_one

    if counter[c] != 0:
        flag = False


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

Submission status: Accepted

Solution code:
from collections import Counter

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


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


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

Submission status: Accepted

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

Submission status: Accepted

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

chars=dict()
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=0
        chars[c]+=1

def judge(H,W,chars):
    #両方奇数
    if H%2+W%2==2:
        ones=1
        twos=H//2 + W//2
    #両方偶数
    elif H%2+W%2==0:
        ones=0
        twos=0
    elif H%2==1:
        ones=0
        twos=W//2
    else:
        ones=0
        twos=H//2
    for c in chars:
        chars[c]%=4
        if chars[c]==0:
            continue
        if chars[c]==2:
            twos-=1
        elif chars[c]==3:
            twos-=1
            ones-=1
        else:
            ones-=1
    if ones<0 or twos<0:
        return False
    return True

if judge(H,W,chars):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

chars=dict()
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=0
        chars[c]+=1

def judge(H,W,chars):
    #両方奇数
    if H%2+W%2==2:
        ones=1
        twos=H//2 + W//2
    #両方偶数
    elif H%2+W%2==0:
        ones=0
        twos=0
    elif H%2==1:
        ones=0
        twos=W//2
    else:
        ones=0
        twos=H//2
    for c in chars:
        if ones<0 or twos<0:
            return False
        chars[c]%=4
        if chars[c]==0:
            continue
        if chars[c]==2:
            twos-=1
        elif chars[c]==3:
            twos-=1
            ones-=1
        else:
            ones-=1
    if ones<0 or twos<0:
        return False
    return True

if judge(H,W,chars):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

chars=dict()
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=0
        chars[c]+=1

def judge(H,W,chars):
    #両方奇数
    if H%2+W%2==2:
        ones=1
        twos=H//2 + W//2
    #両方偶数
    elif H%2+W%2==0:
        ones=0
        twos=0
    elif H%2==1:
        ones=0
        twos=W//2
    else:
        ones=0
        twos=H//2
    for c in chars:
        if ones<0 or twos<0:
            return False
        chars[c]%=4
        if chars[c]==0:
            continue
        if chars[c]==2:
            twos-=1
        elif chars[c]==3:
            twos-=1
            ones-=1
        else:
            ones-=1
    return True

if judge(H,W,chars):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

chars=dict()
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=0
        chars[c]+=1

def judge(H,W,chars):
    #両方奇数
    if H%2+W%2==2:
        ones=1
        twos=(H+W-2)//2
    #両方偶数
    elif H%2+W%2==0:
        ones=0
        twos=0
    elif H%2==1:
        ones=0
        twos=W//2
    else:
        ones=0
        twos=H//2
    for c in chars:
        if ones<0 or twos<0:
            return False
        chars[c]%=4
        if chars[c]==0:
            continue
        if chars[c]==2:
            twos-=1
        elif chars[c]==3:
            twos-=1
            ones-=1
        else:
            ones-=1
    return True

if judge(H,W,chars):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict
    H, W = map(int, input().split())
    counter = defaultdict(int)
    for _ in range(H):
        for s in input().rstrip():
            counter[s] += 1
    if H % 2 == 0 and W % 2 == 0:
        for cnt in counter.values():
            if cnt % 4 != 0:
                print("No")
                sys.exit()
        print("Yes")
        sys.exit()
    
    if H % 2 == 1 and W % 2 == 1:
        one_cnt = 0
        two_cnt = 0
        for cnt in counter.values():
            if cnt % 4 != 0:
                if cnt % 2 == 0:
                    two_cnt += 1
                else:
                    one_cnt += 1
        if two_cnt <= H // 2 + W // 2 and one_cnt <= 1:
            print("Yes")
        else:
            print("No")
        sys.exit()
    
    if H % 2 == 1 and W % 2 == 0:
        two_cnt = 0
        for cnt in counter.values():
            if cnt % 4 != 0:
                if cnt % 2 == 0:
                    two_cnt += 1
                else:
                    print("No")
                    sys.exit()
        if two_cnt <= W // 2:
            print("Yes")
        else:
            print("No")
        sys.exit() 

    if H % 2 == 0 and W % 2 == 1:
        two_cnt = 0
        for cnt in counter.values():
            if cnt % 4 != 0:
                if cnt % 2 == 0:
                    two_cnt += 1
                else:
                    print("No")
                    sys.exit()
        if two_cnt <= H // 2:
            print("Yes")
        else:
            print("No")
        sys.exit() 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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



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

        cnt.sort()

        flag = False
        for 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:
from collections import Counter
H, W = map(int,input().split())
a = ""
for k in range(H):
    a += input()
C = Counter(a)
s = 0
for e in C:
    s += C[e]//4
if s < (H//2)*(W//2):
    print("No")
    exit(0)

a = s-(H//2)*(W//2)
t = 0
u = 0
for e in C:
    t += C[e]%4
    u += C[e]%2

if 2*t + 4*a < (H%2)*(H//2) + (W%2)*(W//2):
    print("No")
    exit(0)
if u != (H%2)*(W%2):
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

h, w = lr()
alp = [0] * 26
for i in range(h):
        for c in sr(): alp[ord(c)-ord('a')] += 1

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

Submission status: Accepted

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

sys.setrecursionlimit(1000000)
mod = 1000000007

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

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

Submission status: Accepted

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

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