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
import sys

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

arr = sum(arr, [])

c = Counter(arr)
c = list(c.values())

if h % 2 == 0 and w % 2 == 0:
    ans = all([x % 4 == 0 for x in c])  # 全て4個セットならOK

elif h % 2 == 1 and w % 2 == 1:
    even = (h + w) // 2 - 1  # 2個セットを許容できる個数
    odd_cnt = len([x for x in c if x % 2 == 1])
    if odd_cnt != 1:
        print("No")
        sys.exit()
    odd = sum([x for x in c if x % 2 == 1])
    if odd % 4 == 3:
        even -= 1
    c.remove(odd)
    # 2個セットが許容数以下か
    ans = True if sum([x % 4 == 2 for x in c]) <= even else False
    ans &= all([x % 4 == 0 for x in c if x % 4 != 2])  # 残り全て4個セットか

else:
    even = h // 2 if h % 2 == 0 else w // 2  # 2個セットを許容できる個数
    # 2個セットが許容数以下か
    ans = True if sum([x % 4 == 2 for x in c]) <= even else False
    ans &= all([x % 4 == 0 for x in c if x % 4 != 2])  # 残り全て4個セットか

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[list(input()) for _ in range(H)]
D={}

for h in range(H):
    for w in range(W):
        if s[h][w] in D:
            D[s[h][w]]+=1
        else:
            D[s[h][w]]=1

odd=0
semi=0
for d in D:
    if D[d]%4==1:
        odd+=1
    elif D[d]%4==2:
        semi+=2
    elif D[d]%4==3:
        odd+=1
        semi+=2

if H%2==0 and W%2==0:
    if odd==semi==0:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==1:
    if odd==1 and semi<=H+W-2:
        print("Yes")
    else:
        print("No")
else:
    if H%2==1:
        if odd>0 or semi>W:
            print("No")
        else:
            print("Yes")
    else:
        if odd>0 or semi>H:
            print("No")
        else:
            print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

c4,c2 = 0,0
for k in d.keys():
    c4 += d[k]//4
    c2 += (d[k]%4)//2

if H%2 + W%2 == 0:
    if c4 == H*W//4:
        print("Yes")
    else:
        print("No")
elif H%2 + W%2 == 1:
    if H%2 == 1:
        if c4 < (H-1)*W//4:
            print("No")
        else:
            c4 -= (H-1)*W//4
            c2 += c4*2
            if c2 == W//2:
                print("Yes")
            else:
                print("No")
    if W%2 == 1:
        if c4 < H*(W-1)//4:
            print("No")
        else:
            c4 -= H*(W-1)//4
            c2 += c4*2
            if c2 == H//2:
                print("Yes")
            else:
                print("No")
else:
    if c4 < (H-1)*(W-1)//4:
        print("No")
    else:
        c4 -= (H-1)*(W-1)//4
        c2 += c4*2
        if c2 == (H-1)//2+(W-1)//2:
            print("Yes")
        else:
            print("No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [list(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:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

# print(lst)

heapq.heapify(lst)
dic=defaultdict(int)
if h%2==0:
    if w%2==0:
        dic[4]=h*w//4
    else:
        dic[4]=(w-1)*h//4
        dic[2]=h//2
else:
    if w%2==0:
        dic[4]=(h-1)*w//4
        dic[2]=w//2
    else:
        dic[4]=(h-1)*(w-1)//4
        dic[2]=h//2+w//2
        dic[1]=1
# print(dic)
# print(lst)
for i in range(dic[4]):
    tmp=-heapq.heappop(lst)
    if tmp>=4:
        tmp-=4
        heapq.heappush(lst,-tmp)
    else:
        print("No")
        quit()

for i in range(dic[2]):
    tmp=-heapq.heappop(lst)
    if tmp>=2:
        tmp-=2
        heapq.heappush(lst,-tmp)
    else:
        print("No")
        quit()

for i in range(dic[1]):
    tmp=-heapq.heappop(lst)
    if tmp>=1:
        tmp-=1
        heapq.heappush(lst,-tmp)
    else:
        print("No")
        quit()
# print(lst)
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[list(input()) for _ in range(h)]
b=[0]*26
for aa in a:
  for ai in aa:
    b[ord(ai)-97]+=1

x=(h//2)*(w//2)
y=(h%2)*(w//2)+(h//2)*(w%2)
z=(h%2)*(w%2)

for bb in b:
  t=bb
  while x and t>3:
    x-=1
    t-=4
  while y and t>1:
    y-=1
    t-=2
  z-=t
if not(x==y==z==0):
  print("No")
else:
  print("Yes")

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict
h, w = map(int, input().split())
cnt = defaultdict(int)
for _ in range(h):
    for a in input():
        cnt[a] -= 1
blocks = list(cnt.values())
heapq.heapify(blocks)

tasks = []
tasks.extend([4]*((h//2)*(w//2)))
tasks.extend([2]*((h % 2)*(w//2)+(w % 2)*(h//2)))
if h & 1 and w & 1:
    tasks.append(1)


for k in tasks:
    v = -heapq.heappop(blocks)
    if v < k:
        break
    x = -(v-k)
    if x == 0:
        continue
    heapq.heappush(blocks, x)
print('Yes' if not blocks else 'No')

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 input():
        cnt[a] += 1

quartet = (h//2) * (w//2)
doublet = (h % 2)*(w//2)+(w % 2)*(h//2)
singlet = 1 if h & 1 and w & 1 else 0

for k, v in cnt.items():
    if not quartet:
        break
    if v < 4:
        continue
    x = min(quartet, v//4)
    quartet -= x
    cnt[k] -= 4*x

for k, v in cnt.items():
    if v < 2:
        continue
    doublet -= v//2
    cnt[k] = v % 2

singlet -= sum(v for v in cnt.values())
if singlet == doublet == quartet == 0:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
from collections import Counter

H, W = map(int, input().split())
four, two, any = 0, 0, 0
if H % 2:
    if W % 2:
        two = H // 2 + W // 2
        any = 1
    else:
        two = W // 2
else:
    if W % 2:
        two = H // 2
four = (H * W - two * 2 - any) // 4
s = ''
for _ in range(H):
    s += str(input())
c = Counter(s)
d = {'four': 0, 'two': 0}
for v in c.values():
    d['four'] += v // 4
    v -= 4 * (v // 4)
    d['two'] += v // 2
if d['four'] >= four:
    d['two'] += (d['four'] - four) * 2
    if d['two'] == two:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
H,W = IL()
dic = DD(int)
for _ in range(H):
  s = S()
  for x in s:
    dic[x] += 1
data = []
for a in AtoZ:
  if dic[a]%4 != 0:
    data.append(dic[a]%4)
c = DD(int)
for d in data:
  c[d] += 1
  
if H%2 and W%2:
  if c[1] == 1 and c[2]<=H//2+W//2 and c[3] == 0:
      print("Yes")
  elif c[3] == 1 and c[2]<=H//2+W//2-1 and c[1] == 0:
      print("Yes")
  else:
    print("No")
elif H%2:
  if c[1] == c[3] == 0 and c[2]<=W//2:
      print("Yes")
  else:
    print("No")
elif W%2:
  if c[1] == c[3] == 0 and c[2]<=H//2:
      print("Yes")
  else:
    print("No")
else:
  if data == []:
    print("Yes")
  else:
    print("No")

Submission status: Accepted

Solution code:
from collections import Counter
import math
h, w = map(int, input().split())
data = ''
for _ in range(h):
    a = input()
    data += a
cnt = Counter(data)
data_list = list(cnt.values())
cnt_list = []
for hh in range(math.ceil(h / 2)):
    for ww in range(math.ceil(w / 2)):
        if (hh == math.floor(h / 2)) & (ww == math.floor(w / 2)):
            cnt_list.append(1)
        elif (hh == math.floor(h / 2)) | (ww == math.floor(w / 2)):
            cnt_list.append(2)
        else:
            cnt_list.append(4)
cnt_count = Counter(cnt_list)
ans = 'Yes'

cnt_4 = 0
while cnt_4 < cnt_count[4]:
    for c in range(len(data_list)):
        if ((data_list[c]) >= 4) & (data_list[c] > 0):
            data_list[c] -= 4
            cnt_4 += 1
        if cnt_4 == cnt_count[4]:
            break
    if cnt_4 == cnt_count[4]:
        ans = 'Yes'
        break
    if len([d for d in data_list if d >= 4]) == 0:
        ans = 'No'
        break

cnt_2 = 0
while cnt_2 < cnt_count[2]:
    for c in range(len(data_list)):
        if ((data_list[c] >= 2) & (data_list[c] > 0)):
            data_list[c] -= 2
            cnt_2 += 1
        if cnt_2 == cnt_count[2]:
            break
    if cnt_2 == cnt_count[2]:
        ans = 'Yes'
        break
    if len([d for d in data_list if d >= 2]) == 0:
        ans = 'No'
        break
    
cnt_1 = 0
while cnt_1 < cnt_count[1]:
    for c in range(len(data_list)):
        if (data_list[c] > 0):
            data_list[c] -= 1
            cnt_1 += 1
        if cnt_1 == cnt_count[1]:
            break
    if cnt_1 == cnt_count[1]:
        ans = 'Yes'
        break
    if len([d for d in data_list if d >= 1]) == 0:
        ans = 'No'
        break

if (sum(data_list) == 0) & (ans != 'No'):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

lst = [0]*26

for _ in range(H):
  s = input()
  for c in s:
    lst[ord(c)-ord('a')] += 1
  
cnt1 = 0
cnt2 = 0
for i in lst:
  if i%2 != 0:
    cnt1 += 1
  elif i%4 != 0:
    cnt2 += 1
    
if cnt1 > 1:
  print('No')
  exit()
  
if H%2 == 0 and W%2 == 0:
  if cnt1 > 0 or cnt2 > 0:
    print('No')
  else:
    print('Yes')
elif H%2 == 1 and W%2 == 0:
  if cnt1 > 0 or cnt2 > W//2:
    print('No')
  else:
    print('Yes')
elif H%2 == 0 and W%2 == 1:
  if cnt1 > 0 or cnt2 > H//2:
    print('No')
  else:
    print('Yes')
else:
  if cnt2 > (H//2 + W//2):
    print('No')
  else:
    print('Yes')

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = defaultdict(int)
for i in range(h):
    for j in range(w):
        cnt[a[i][j]] += 1

flag1 = w % 2
flag2 = h % 2
even = h * flag1 + w * flag2 - flag1 * flag2 * 2
odd = flag1 * flag2

ans = True
for k, v in cnt.items():
    even -= ((v % 4) // 2) * 2
    odd -= v % 2
    if even < 0 or odd < 0:
        ans = False
        break

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

Submission status: Accepted

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

lis = [0] * 26
for i in range(H):
    for j in range(W):
        lis[ord(a[i][j]) - ord('a')] += 1

one = 0
two = 0
for i in lis:
    if i % 4 == 2:
        two += 1
    elif i % 4 == 1:
        one += 1
    elif i % 4 == 3:
        two += 1
        one += 1
ans = 'Yes'
if H % 2 == 0 and W % 2 == 0:
    if two > 0 or one > 0:
        ans = 'No'
elif H % 2 == 0 and W % 2 == 1:
    if one > 0 or two > H//2:
        ans = 'No'
elif H % 2 == 1 and W % 2 == 0:
    if one > 0 or two > W//2:
        ans = 'No'
else:
    if one != 1 or two > (H-1 + W-1)//2:
        ans = 'No'
print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
l = []
for i in range(h):
    l.extend(input())
import collections
c = collections.Counter(l)
num4=(h//2) * (w//2)
if h%2!=0 and w%2!=0:
    num1=1
else:
    num1=0
num2 = ((h*w) - (4*num4 + num1))//2
cnts = list(c.values())

for i in range(num4):
    for j in range(len(cnts)):
        if cnts[j] >= 4:
            cnts[j] -= 4
            break
    else: # not broken -> error
        print("No")
        exit()

for i in range(num2):
    for j in range(len(cnts)):
        if cnts[j] >= 2:
            cnts[j] -= 2
            break
    else: # not broken -> error
        print("No")
        exit()

for i in range(num1):
    for j in range(len(cnts)):
        if cnts[j] >= 1:
            cnts[j] -= 1
            break
    else: # not broken -> error
        print("No")
        exit()

print("Yes")

Submission status: Accepted

Solution code:
from collections import Counter as co
n,m=map(int,input().split())
l=co("".join([input()for i in range(n)])).values()
if n%2 and m%2:
    l=list(l)
    f=1
    for v in range(len(l)):
        if l[v]%2:
            if f:f=0;l[v]-=1
            else:print("No");exit()
    if f==1:print("No");exit()
    print("Yes"if(n+m)//2-1>=len(tuple(1 for j in l if j%4==2)) else "No")
elif (n+m)%2:
    k=(n,m)[n%2]
    print("Yes"if k//2>=len(tuple(1 for j in l if j%4==2)) and 0==len(tuple(1 for j in l if j%2))else"No")
else:print("No"if len(tuple(1 for j in l if j%4))else "Yes")

Submission status: Accepted

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

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
even_4_sum=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%2==0:
            k= d[char]//4
            even_4 += k
            even_4_sum += k*4
            even_2 += (d[char]-4*k)//2
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd !=1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd !=1:
            ans="No"
        else:
            s=d[odd_list[0]]
            u = s//4
            v = (s-u)//2  # s=4*u+2*v+1
            #print("even_4_sum",even_4_sum)
            #print(H,W,(H-1)*(W-1))
                  
            if even_4_sum+4*u< (H-1)*(W-1):
                ans="No"
            #else:
            #    even_4_amari = even_4_sum- (H-1)*(W-1)
            #    must_4_maxi = 4*(min(H,W)//2)
            #    print(even_4_amari, must_4_maxi)
            #    #must_4_mini = 4*(min(H,W)//2)
            #    if even_4_amari > must_4_maxi:
            #        ans="No"
                
    else:
        if odd >=1:
            ans="No"
        else:
            gu = H if H%2==0 else W
            if 2*even_2>gu:
                ans="No"

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

H, W = map(int, input().split())
s = ''
for _ in range(H):
    s += input()
jdg = True
cnt = Counter(s)
lst = list(cnt.values())
lst4 = [l%4 for l in lst if l%4!=0]
lst42 = []
if len(lst4) > ((H%2) * W//2 + (W%2) * H//2 + (H%2)*(W%2)): jdg = False
elif len(lst4) == 0 and (H%2 == 1 and W%2 == 1):jdg = False 
else:
    lst42 = [l%2 for l in lst4 if l%2!=0]
    if len(lst42) != (H%2) * (W%2): jdg = False

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

Submission status: Accepted

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

char = [0]*26
for i in range(h):
    a = input()
    for j in range(w):
        char[ord(a[j])-97] -= 1

heapify(char)

q = []
if h % 2 == 1 and w % 2 == 1:
    for i in range((h//2)*(w//2)):
        q.append(4)
    for i in range((h+w-2)//2):
        q.append(2)
    q.append(1)

elif h % 2 == 1:
    for i in range((h//2)*(w//2)):
        q.append(4)
    for i in range(w//2):
        q.append(2)

elif w % 2 == 1:
    for i in range((h//2)*(w//2)):
        q.append(4)
    for i in range(h//2):
        q.append(2)

else:
    for i in range((h//2)*(w//2)):
        q.append(4)

flag = 1
for i in q:
    a = heappop(char)
    if -a >= i:
        a += i
        heappush(char, a)
    else:
        flag = 0
        break

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

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# a〜z -- START --
alf=['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']
# a〜z --- END ---

def main():
  h,w=LI()
  l=[0]*len(alf)

  for _ in range(h):
    a=S()
    for x in a:
      l[alf.index(x)]+=1

  count4=count2=count1=0
  for i in range(len(alf)):
    count4+=l[i]//4
    l[i]%=4
    count2+=l[i]//2
    l[i]%=2
    count1+=l[i]

  # print(count1,count2,count4)

  if h%2==1 and w%2==1:
    if count1!=1:
      return 'No'

    x=count4-(h-1)*(w-1)//4
    if x<0:
      return 'No'
    count2+=x*2

    if count2!=w//2+h//2:
      return 'No'

    return 'Yes'
  elif h%2==1 and w%2==0:
    if count1!=0:
      return 'No'

    x=count4-(h-1)*w//4
    if x<0:
      return 'No'
    count2+=x*2

    if count2!=w//2:
      return 'No'

    return 'Yes'
  elif h%2==0 and w%2==1:
    if count1!=0:
      return 'No'

    x=count4-h*(w-1)//4
    if x<0:
      return 'No'
    count2+=x*2

    if count2!=h//2:
      return 'No'

    return 'Yes'
  elif h%2==0 and w%2==0:
    if count1!=0:
      return 'No'

    x=count4-h*w//4
    if x<0:
      return 'No'
    count2+=x*2

    if count2!=0:
      return 'No'

    return 'Yes'

# main()
print(main())

Submission status: Accepted

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

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

f_num = 0
t_num = 0
for i in cnt:
    while i >= 4:
        i -= 4
        f_num += 1
    while i >= 2:
        i -=2
        t_num += 1

req_f = (h//2)*(w//2)
req_t = (h%2)*(w//2) + (w%2)*(h//2)

if req_f > f_num:
    print('No')
    exit()
f_num -= req_f
t_num += f_num*2
if req_t > t_num:
    print("No")
    exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

H,W = LI()
a  = [list(map(str,input())) for _ in range(H)]

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

if H%2 == 0 and W%2 == 0:
    for k in d.keys():
        if d[k]%4 != 0:
            print('No')
            exit()
    print('Yes')
elif H%2 == 0 and W%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (H//2)%2 and H//2 >= count:
        print('Yes')
    else:
        print('No')
elif W%2 == 0 and H%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (W//2)%2 and W//2 >= count:
        print('Yes')
    else:
        print('No')
else:
    odd_flag = False
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                if not odd_flag:
                    odd_flag = True
                    if (d[k]-1)%4 != 0:
                        count += 1
                else:
                    print('No')
                    exit()
            else:
                count += 1
    if count%2 == ((H-1)//2+(W-1)//2)%2 and (H-1)//2+(W-1)//2 >= count:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

H,W = LI()
a  = [list(map(str,input())) for _ in range(H)]

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

if H%2 == 0 and W%2 == 0:
    for k in d.keys():
        if d[k]%4 != 0:
            print('No')
            exit()
    print('Yes')
elif H%2 == 0 and W%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (H//2)%2 and H//2 >= count:
        print('Yes')
    else:
        print('No')
elif W%2 == 0 and H%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (W//2)%2 and W//2 >= count:
        print('Yes')
    else:
        print('No')
else:
    odd_flag = False
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                if not odd_flag:
                    odd_flag = True
                else:
                    print('No')
                    exit()
            else:
                count += 1
    if count%2 == ((H-1)//2+(W-1)//2)%2 and (H-1)//2+(W-1)//2 >= count:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
import sys
H,W=map(int,input().split())
a = [[c for c in l.strip()] for l in sys.stdin]
#print(a)
dic={}
for i in range(H):
  for j in range(W):
    if(a[i][j] not in dic):
      dic[a[i][j]]=1
    else:
      dic[a[i][j]]+=1
#print(dic)
flag=True
if(H%2==0 and W%2==0):
  for i in dic.keys():
    if(dic[i]%4!=0):
      flag=False
elif((H%2==1 and W%2==0)):
  count=0
  for i in dic.keys():
    if(dic[i]%2!=0):
      flag=False
    if(dic[i]%4==2):
      count+=1
  if(count>W//2):
    flag=False
elif((H%2==0 and W%2==1)): 
  count=0
  for i in dic.keys():
    if(dic[i]%2!=0):
      flag=False
    if(dic[i]%4==2):
      count+=1
  if(count>H//2):
    flag=False
else:
  count2=0
  count1=0
  for i in dic.keys():
    if(dic[i]%4==1):
      count1+=1
    elif(dic[i]%4==2):
      count2+=1
    elif(dic[i]%4==3):
      count1+=1
      count2+=1
  if(count1>2 or count2>(W+H)//2-1):
    flag=False
if flag:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
alp = "qwertyuiopasdfghjklzxcvbnm"
c = [0]*26
for i in range(h):
    s = input()
    for i in range(26):
        c[i] += s.count(alp[i])
for i in range(26):
    c[i] %= 4
c2 = c.count(2)
c2 += c.count(3)
for i in range(26):
    c[i] %= 2
c1 = c.count(1)
a2 = 0
a1 = 0
if h%2 == 1:
    a2 += w//2
if w%2 == 1:
    a2 += h//2
if h%2 == 1 and w%2 == 1:
    a1 = 1
if c2 <= a2 and c1 <= a1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

if h%2==0 and w%2==0:
    for i in cnt:
        if i%4!=0:
            print('No')
            break
    else:
        print('Yes')
elif h%2==0 or w%2==0:
    mod2 = 0
    for i in cnt:
        if i%2!=0:
            print('No')
            break
        elif i%4!=0:
            mod2 += i%4
    else:
        tmp = h if h%2==0 else w
        if mod2<=tmp:
            print('Yes')
        else:
            print('No')
else:
    mod2 = 0
    alone = False
    for i in cnt:
        if i%2!=0:
            if alone:
                print('No')
                break
            alone = True
            mod2 += (i%4)//2
        elif i%4!=0:
            mod2 += i%4
    else:
        if mod2<=h+w-2:
            print('Yes')
        else:
            print('No')

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w = ns()
    d = []
    for _ in range(h):
        tmp = list(input())
        d.extend(tmp)

    c = collections.Counter(d)

    flg1 = w % 2
    flg2 = h % 2
    even_pocket = h * flg1 + w * flg2 - flg1 * flg2 * 2
    odd_pocket = flg1 * flg2

    ans = True
    for k, v in c.items():
        even_pocket -= ((v % 4) // 2) * 2
        odd_pocket -= v % 2
        if even_pocket < 0 or odd_pocket < 0:
            ans = False
            break

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


if __name__ == '__main__':
    main()