Problem:


Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Accepted

Solution code:
from collections import defaultdict
def getlist():
  return list(map(int, input().split()))

D = defaultdict(int)

H, W = getlist()
for i in range(H):
  a = input()
  for j in a:
    D[j] += 1

if H % 2 == 0 and W % 2 == 0:
  judge = "Yes"
  for i in D:
    if D[i] % 4 != 0:
      judge = "No"
      break
  print(judge)

elif H % 2 == 0:
  judge = "Yes"
  c2 = 0
  for i in D:
    if D[i] % 4 != 0:
      if D[i] % 2 == 0:
        c2 += 1
      else:
        judge = "No"
        break
  if judge == "No":
      print(judge)
  else:
    if c2 <= int(H // 2):
      print("Yes")
    else:
      print("No")

elif W % 2 == 0:
  judge = "Yes"
  c2 = 0
  for i in D:
    if D[i] % 4 != 0:
      if D[i] % 2 == 0:
        c2 += 1
      else:
        judge = "No"
        break
  if judge == "No":
      print(judge)
  else:
    if c2 <= int(W // 2):
      print("Yes")
    else:
      print("No")

else:
  c1 = 0
  c2 = 0
  for i in D:
    if D[i] % 4 != 0:
      if D[i] % 4 == 2:
        c2 += 1
      else:
        c1 += 1

  if c1 <= 1 and c2 <= int((H - 1) // 2 + (W - 1) // 2):
    print("Yes")
  else:
    print("No")

Submission status: Accepted

Solution code:
from collections import Counter

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

for _ in range(H):
    cnt.update(Counter(input()))

H_r = H % 2
W_r = W % 2

if H_r == 0 and W_r == 0:
    cnt1 = cnt2 = 0
    cnt4 = (H * W) // 4
elif H_r == 1 and W_r == 0:
    cnt1 = 0
    cnt2 = W // 2
    cnt4 = (W // 2) * (H // 2)
elif H_r == 0 and W_r == 1:
    cnt1 = 0
    cnt2 = H // 2
    cnt4 = (W // 2) * (H // 2)
else:
    cnt1 = 1
    cnt4 = (W // 2) * (H // 2)
    cnt2 = ((H * W) - cnt4 * 4 - cnt1 * 1) // 2

odd = 0
for c in cnt.values():
    t = min(c // 4, cnt4)
    c -= 4 * t
    cnt4 -= t

    t = min(c // 2, cnt2)
    c -= 2 * t
    cnt2 -= t

    t = min(c, cnt1)
    c -= t
    cnt1 -= t

    if c > 0: break
print('Yes' if cnt1 == cnt2 == cnt4 == 0 else 'No')

Submission status: Accepted

Solution code:
from collections import Counter

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

for _ in range(H):
    cnt.update(Counter(input()))

H_r = H % 2
W_r = W % 2

if H_r == 0 and W_r == 0:
    cnt1 = cnt2 = 0
    cnt4 = (H * W) // 4
elif H_r == 1 and W_r == 0:
    cnt1 = 0
    cnt2 = W // 2
    cnt4 = (W // 2) * (H // 2)
elif H_r == 0 and W_r == 1:
    cnt1 = 0
    cnt2 = H // 2
    cnt4 = (W // 2) * (H // 2)
else:
    cnt1 = 1
    cnt4 = (W // 2) * (H // 2)
    cnt2 = ((H * W) - cnt4 * 4 - cnt1 * 1) // 2

odd = 0
for c in cnt.values():
    while c >= 4 and cnt4 > 0:
        c -= 4
        cnt4 -= 1
    while c >= 2 and cnt2 > 0:
        c -= 2
        cnt2 -= 1
    while c >= 1 and cnt1 > 0:
        c -= 1
        cnt1 -= 1
print('Yes' if cnt1 == cnt2 == cnt4 == 0 else 'No')

Submission status: Accepted

Solution code:
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)


H,W = map(int,input().split())
A = ""
for i in range(H):
    s = input()
    A += s
C = Counter(A)

c4,c2=0,0

for _,x in C.most_common():
    c4 += x//4
    c2 += (x%4)//2

g4 = (H//2)*(W//2)
g2 = (H//2)*(W%2)+(W//2)*(H%2)

if c4-g4 < 0:
    print("No")
elif (c2+(c4-g4)*2)  < g2:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

for i in range(H):
    li = list(input())
    for j in li:
        if j not in d:
            d[j] = 1
        else:
            d[j] += 1

norm = (H//2) * (W//2)

unit = 0
for i in d:
    unit += d[i] // 4

if unit < norm:
    print("No")
    exit()
else:
    for i in d:
        if norm == 0:
            continue
        if d[i] >= 4:
            num = min(norm, d[i] // 4)
            d[i] -= 4 * num
            norm -= num

if (H % 2 == 0) ^ (W % 2 == 0):
    for i in d:
        if d[i] % 2 == 1:
            print("No")
            exit()
    print("Yes")
    exit()
else:
    odd = 0
    for i in d:
        if d[i] % 2 == 1:
            odd += 1
            if odd >= 2:
                print("No")
                exit()
    print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict
cnt=defaultdict(int)
H,W=map(int,input().split())
for i in range(H):
    for a in input():
        cnt[a]+=1
one=0
two=0
for x in cnt.values():
    if x%2==1:
        one+=1
        x-=1
    x//=2
    if x%2==1:
        two+=2
canone=0
cantwo=0
if H%2==1 and W%2==1:
    canone=1
    cantwo=H+W-2
if H%2==0 and W%2==1:
    H,W=W,H
if H%2==1 and W%2==0:
    canone=0
    cantwo=W
if one<=canone and two<=cantwo:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
d=[0]*26
for i in range(h):
    b=list(input())
    for j in range(w):
        d[ord(b[j])-ord("a")]+=1
s=0
t=0
r=0
for i in range(26):
    if d[i]%4==0:
        s+=d[i]//4
    elif d[i]%2==0:
        s+=d[i]//4
        t+=(d[i]%4)//2
    else:
        s+=d[i]//4
        t+=(d[i]%4)//2
        r+=1
if h%2==0 and w%2==0:
    if t==0 and r==0:
        print("Yes")
    else:
        print("No")
elif h%2==1 and w%2==1:
    if r==1 and(w+h-2-2*t)>=0 and (w+h-2-2*t)%4==0:
        print("Yes")
    else:
        print("No")
else:
    q=(w if h%2==1 else h)
    if q-2*t>=0  and (q-2*t)%4==0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
c = [0] * 26
for _ in range(H):
	for i in input():
		c[ord(i) - ord('a')] += 1
a = 0
b = 0
for i in c:
	a += i % 4 // 2
	b += i % 2
if (H % 2) * (W % 2) < b:
	print("No")
elif (H % 2) * (W // 2) + (H // 2) * (W % 2) < a:
	print("No")
else:
	print("Yes")

Submission status: Accepted

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()
S=''
for i in range(H):
    S+=input()

# 各文字を4つ組と余りにまとめる
C=Counter(S)
cnt=Counter()
for v in C.values():
    cnt[v%4]+=1
# 3つ組は1と2に割り振る
cnt[1]+=cnt[3]
cnt[2]+=cnt[3]
cnt[3]=0

# 縦横とも偶数
if H%2==0 and W%2==0:
    # 全て4つ組
    if cnt[1]==0 and cnt[2]==0:
        Yes()
    else:
        No()

# 縦偶数
elif H%2==0:
    # 2つ組がH/2以下で残りが4つ組
    if cnt[2]<=H//2 and cnt[1]==0:
        Yes()
    else:
        No()

# 横偶数
elif W%2==0:
    # 2つ組がW/2以下で残りが4つ組
    if cnt[2]<=W//2 and cnt[1]==0:
        Yes()
    else:
        No()

# 両方奇数
else:
    # 1つ組1つ、2つ組が(H+W)/2-1以下で残りが4つ組
    if cnt[1]==1 and cnt[2]<=(H+W)//2-1:
        Yes()
    else:
        No()

Submission status: Accepted

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()
S=''
for i in range(H):
    S+=input()

C=Counter(S)
cnt1=cnt2=0
# 縦横とも偶数
if H%2==0 and W%2==0:
    for k, v in C.items():
        if v%4!=0:
            No()
            exit()
    # 全て4つ組
    Yes()
# 縦偶数
elif H%2==0:
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                cnt2+=1
            else:
                No()
                exit()
    # 2つ組がH/2以下で残りが4つ組
    if cnt2<=H//2:
        Yes()
    else:
        No()
# 横偶数
elif W%2==0:
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                cnt2+=1
            else:
                No()
                exit()
    # 2つ組がW/2以下で残りが4つ組
    if cnt2<=W//2:
        Yes()
    else:
        No()
# 両方奇数
else:
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                cnt2+=1
            elif v%3==0:
                cnt1+=1
                cnt2+=1
            else:
                cnt1+=1
    # 1つ組1つ、2つ組が(H+W)/2-1以下で残りが4つ組
    if cnt1==1 and cnt2<=(H+W)//2-1:
        Yes()
    else:
        No()

Submission status: Accepted

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()
S=''
for i in range(H):
    S+=input()

C=Counter(S)
if H%2==0 and W%2==0:
    for k, v in C.items():
        if v%4!=0:
            No()
            exit()
    Yes()
elif H%2==0:
    mod2=0
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                mod2+=1
            else:
                No()
                exit()
    if mod2<=H//2:
        Yes()
    else:
        No()
elif W%2==0:
    mod2=0
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                mod2+=1
            else:
                No()
                exit()
    if mod2<=W//2:
        Yes()
    else:
        No()
else:
    mod2=mod1=0
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                mod2+=1
            elif v%3==0:
                mod2+=1
                mod1+=1
            else:
                mod1+=1
    if mod1==1 and mod2<=(H+W)//2-1:
        Yes()
    else:
        No()

Submission status: Accepted

Solution code:
from collections import Counter

if __name__ == '__main__':
    h, w = map(int, input().split())
    counts = Counter(''.join(input() for _ in range(h))).values()
    c1, c2 = 0, 0
    for x in counts:
        if x % 2:
            c1 += 1
            x -= 1
        if x % 4:
            c2 += 1
    if c1 == 1 and (h * w) & 1 == 0 or c1 > 1:
        print('No')
        quit(0)
    lim2 = 0
    if h & 1:
        lim2 += w // 2
    if w & 1:
        lim2 += h // 2
    if lim2 >= c2:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [H, W] = [int(input_raw[0]), int(input_raw[1])]
    dic = {}
    for i in range(H):
        input_raw = read_func().strip()
        for j in range(W):
            if input_raw[j] not in dic:
                dic[input_raw[j]] = 1
            else:
                dic[input_raw[j]] += 1

    max_count = 0
    odd_count = 0
    even_count = 0

    if H % 2 == 0 and W % 2 == 0:
        for s in dic:
            if dic[s] % 4 != 0:
                print "No"
                return
        print "Yes"
    else:
        four_count = (H / 2) * (W / 2)
        count = 0
        odd_count = 0
        for s in dic:
            count += dic[s]/4
            dic[s] %= 4
            if dic[s] %2 == 1:
                odd_count += 1
        if count < four_count:
            print "No"
            return
        if H % 2 == 1 and W % 2 == 1:
            if odd_count == 1:
                print "Yes"
            else:
                print "No"
            return
        else:
            if odd_count == 0:
                print "Yes"
            else:
                print "No"



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W = list(map(int,input().split()))
def atoi(a):
    alphabet = ['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']
    return alphabet.index(a)
a_list = [0 for i in range(26)]

for i in range(H):
    S=input()
    for a in range(len(S)):
        a_list[atoi(S[a])]+=1

if H%2==0 and W%2==0:
    flg=True
    for a in a_list:
        if a%4!=0:
            flg=False
            break
    if flg:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==0:
    w=W//2
    count=0
    flg=True
    for a in a_list:
        if a%4==1 or a%4==3:
            flg=False
            break
        elif a%4==2:
            count+=1
        if count>w:
            flg=False
            break
    if flg:
        print("Yes")
    else:
        print("No")
elif H%2==0 and W%2==1:
    h=H//2
    count=0
    flg=True
    for a in a_list:
        if a%4==1 or a%4==3:
            flg=False
            break
        elif a%4==2:
            count+=1
        if count>h:
            flg=False
            break
    if flg:
        print("Yes")
    else:
        print("No")
else:
    h=H//2
    w=W//2
    count1=0
    count2=0
    flg=True
    for a in a_list:
        if a%4==1 or a%4==3:
            count1+=1
        elif a%4==2:
            count2+=1
        if count2>h+w or count1>1:
            flg=False
            break
    if flg:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
h,w = map(int, input().split())
al = []
for _ in range(h):
    al += list(input())

c = Counter(al)

if h%2==1 and w%2==1:
    for k in c.keys():
        if c[k]%2==1:
            c[k] -= 1
            break

g4 = (h//2)*(w//2)*4
for k in c.keys():
    while c[k]>=4 and g4>0:
        c[k] -= 4
        g4 -= 4
if g4>0:
    print("No")
    exit()

g2 = (h%2)*(w//2)*2 + (w%2)*(h//2)*2
for k in c.keys():
    while c[k]>=2 and g2>0:
        c[k] -= 2
        g2 -= 2
if g2>0:
    print("No")
    exit()

for k in c.keys():
    if c[k]>0:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=[input() for _ in range(H)]
from collections import defaultdict
d=defaultdict(int)
for a in A:
  for j in a:
    #print("j: ",j)
    d[j]+=1

four=0
two=0
one=0

for i in  range(26):
  c=chr(ord("a")+i)
  if d[c]>=4:
    four+=d[c]//4 
    d[c]%=4
  if d[c]>=2:
    two+=d[c]//2
    d[c]%=2
  if d[c]>=1:
    one+=d[c]
    d[c]-=1

if four<(H//2)*(W//2):    
  print("No")
  exit(0)
four-=(H//2)*(W//2)
two+=four*2

if H%2==1:
  if two < W//2:
    print("No")
    exit()
  two-=W//2
if W%2==1:
  if two <H//2:
    print("No")
    exit()
  two-=H//2

print("Yes")

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d=[h%2&w%2,w%2*h//2+h%2*w//2,0,h//2*(w//2)]
for i in map(s.count,set(s)-{'\n'}):
  for j in(3,1,0):
    while d[j]and i>j:
      i-=j+1
      d[j]-=1
print('YNeos'[any(d)::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d=[h%2&w%2,w%2*h//2+h%2*w//2,0,h//2*(w//2)]
for i in map(s.count,set(s)-{'\n'}):
  for j in(3,1,0):
    while d[j]and i>j:
      i-=j+1
      d[j]-=1
print('YNeos'[sum(d)>0::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a,b,c,d=h//2,h%2,w//2,w%2
l=[b&d,d*a+b*c,0,a*c]
for i in map(s.count,set(s)-{'\n'}):
  for j in(3,1,0):
    while l[j]and i>j:
      i-=j+1
      l[j]-=1
print('YNeos'[sum(l)>0::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d=[h%2and w%2,w%2*h//2+h%2*w//2,0,h//2*(w//2)]
for i in map(s.count,set(s)-{'\n'}):
  for j in(3,1,0):
    while d[j]and i>j:
      i-=j+1
      d[j]-=1
print('YNeos'[sum(d)>0::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a,b=h//2*(w//2),h%2and w%2
d=[b,(h*w-a*4-b)//2,0,a]
for i in map(s.count,set(s)-{'\n'}):
  for j in(3,1,0):
    while d[j]and i>j:
      i-=j+1
      d[j]-=1
print('YNeos'[sum(d)>0::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d={}
d[4]=a=h//2*(w//2)
d[1]=b=h%2and w%2
d[2]=(h*w-a*4-b)//2
for i in map(s.count,set(s)-{'\n'}):
  for j in(4,2,1):
    while d[j]and i>=j:
      i-=j
      d[j]-=1
print('YNeos'[sum(d.values())>0::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d={}
d[4]=a=h//2*(w//2)
d[1]=b=h%2and w%2
d[2]=(h*w-a*4-b)//2
for i in map(s.count,set(s)-{'\n'}):
  for j in(4,2,1):
    while d[j]and i>=j:
      i-=j
      d[j]-=1
print('YNeos'[any(t for t in d.values())::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
d={}
d[3]=h//2*(w//2)
d[0]=h%2and w%2
d[1]=(h*w-d[3]*4-d[0])//2
for i in set(s)-{'\n'}:
  i=s.count(i)
  for j in[3,1,0]:
    while d[j]and i>j:
      i-=j+1
      d[j]-=1
print('YNeos'[any(t for t in d.values())::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a=h//2*(w//2)
c=h%2and w%2
b=(h*w-a*4-c)//2
for i in set(s)-{'\n'}:
  i=s.count(i)
  while i>3and a:
    i-=4
    a-=1
  while i>1and b:
    i-=2
    b-=1
  if i and c:
    i-=1
    c-=1
print('YNeos'[any(t for t in(a,b,c))::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a=h//2*(w//2)
c=h%2and w%2
b=(h*w-a*4-c)//2
for i in set(s.replace('\n','')):
  i=s.count(i)
  while i>3and a:
    i-=4
    a-=1
  while i>1and b:
    i-=2
    b-=1
  if i and c:
    i-=1
    c-=1
print('YNeos'[any(t for t in(a,b,c))::2])

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a=h//2*(w//2)
c=h%2and w%2
b=(h*w-a*4-c)//2
l=[]
for i in set(s.replace('\n','')):
    l.append(s.count(i))
for i in l:
    if i>3:
        while i>3and a:
            i-=4
            a-=1
    if i>1:
        while i>1and b:
            i-=2
            b-=1
    if i and c:
        i-=1
        c-=1
if all(not t for t in(a,b,c)):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
t,_,s=open(0).read().partition('\n')
h,w=map(int,t.split())
a=b=c=0
if h%2and w%2:
    a=h//2*(w//2)
    c=1
    b=(h*w-a*4-c)//2
elif h%2or w%2:
    a=h//2*(w//2)
    b=(h*w-a*4)//2
else:
    a=h*w//4
l=[]
for i in set(s.replace('\n','')):
    l.append(s.count(i))
for i in l:
    if i>3:
        while i>3and a:
            i-=4
            a-=1
    if i>1:
        while i>1and b:
            i-=2
            b-=1
    if i and c:
        i-=1
        c-=1
if all(not t for t in(a,b,c)):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

d = Counter(s).most_common()
one = 0
two = 0
if h % 2:
    if w % 2:
        for _, num in d:
            if num % 4 == 1 or num % 4 == 3:
                one += 1
                if one > 1:
                    print("No")
                    exit()
                if num % 4 == 3:
                    two += 1
                    if two > h//2 + w//2:
                        print("No")
                        exit()
            if num % 4 == 2:
                two += 1
                if two > h//2 + w//2:
                    print("No")
                    exit()
    else:
        for _, num in d:
            if num % 4:
                if num % 4 == 2:
                    two += 1
                    if two > w//2:
                        print("No")
                        exit()
                else:
                    print("No")
                    exit()
else:
    if w % 2:
        for _, num in d:
            if num % 4:
                if num % 4 == 2:
                    two += 1
                    if two > h//2:
                        print("No")
                        exit()
                else:
                    print("No")
                    exit()
    else:
        for _, num in d:
            if num % 4:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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


palinmoji = ""
for i in range(h):
    palinmoji += a[i]

palinli = collections.Counter(palinmoji)

if h % 2 == 0 and w % 2 == 0:
    for i in palinli:
        if palinli[i] % 4 != 0:
            print("No")
            exit()
    print("Yes")
elif h % 2 == 0 and w % 2 != 0:
    cnt = 0
    for i in palinli:
        if palinli[i] % 4 != 0:
            if palinli[i] % 2 != 0:
                print("No")
                exit()
            if palinli[i] % 2 == 0:
                cnt += 1
            if cnt == h // 2 + 1:
                print("No")
                exit()
    print("Yes")
elif h % 2 != 0 and w % 2 == 0:
    cnt = 0
    for i in palinli:
        if palinli[i] % 4 != 0:
            if palinli[i] % 2 != 0:
                print("No")
                exit()
            if palinli[i] % 2 == 0:
                cnt += 1
            if cnt == w // 2 + 1:
                print("No")
                exit()
    print("Yes")
elif h % 2 != 0 and w % 2 != 0:
    cnt = 0
    onecnt = 0
    for i in palinli:
        if palinli[i] % 4 != 0:
            if palinli[i] % 2 != 0:
                onecnt += 1
                if (palinli[i] - 1) % 4 != 0 and onecnt == 1:
                    cnt += 1
                    pass
                elif onecnt == 2:
                    print("No")
                    exit()
            if palinli[i] % 2 == 0:
                cnt += 1
    if cnt >= h // 2 + w // 2 + 1:
        print("No")
        exit()
    print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
H, W = map(int, input().split())


# 各文字の出現回数を数えて降順ソート
Counts = defaultdict(int)
for i in range(H):
    A = input()
    for a in A:
        Counts[a] += 1
Counts = deque(sorted(Counts.values()))


# mod4をとっておく（3は1と2にわける）
mod4 = [c % 4 for c in Counts]
for i in range(len(mod4)):
    if mod4[i] == 3:
        mod4[i] = 1
        mod4.append(2)

# 両方偶数:= 全部4の倍数
if (H % 2 == 0) and (W % 2 == 0):
    print('No' if any(mod4) else 'Yes')

# Hが奇数:= 4で割って2余るものが、W // 2個以下
elif (H % 2 == 1) and (W % 2 == 0):
    if (mod4.count(2) <= W // 2) and (set(mod4) == {0, 2}):
        print('Yes')
    else:
        print('No')

# Wが奇数:= 4で割って2余るものが、H // 2個以下
elif (H % 2 == 0) and (W % 2 == 1):
    if (mod4.count(2) <= H // 2) and (set(mod4) == {0, 2}):
        print('Yes')
    else:
        print('No')

# 両方奇数:= 4で割って2余るものが、 H // 2 + W // 2個以下、かつ1が1つ。3の扱いに注意。
else:
    if (mod4.count(2) <= (H // 2) + (W // 2)) and (mod4.count(1) == 1):
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
H, W = [int(_) for _ in input().split()]
memo = {}
for _ in range(H):
    for _ in input():
        memo[_] = memo.get(_, 0) + 1
count = [0] * 4
for k, v in memo.items():
    count[v % 4] += 1
ans = 'No'
if H * W % 2:
    if count[1] + count[3] == 1 and 1 + 2 * count[2] <= H + W - 1:
        ans = 'Yes'
elif H % 2:
    if count[1] == count[3] == 0 and 2 * count[2] <= W and (
            W - 2 * count[2]) % 4 == 0:
        ans = 'Yes'
elif W % 2:
    if count[1] == count[3] == 0 and 2 * count[2] <= H and (
            H - 2 * count[2]) % 4 == 0:
        ans = 'Yes'
else:
    if count[1] == count[2] == count[3] == 0:
        ans = 'Yes'
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

ng = False
if H % 2 == 0 and W % 2 == 0:
	for value in c.values():
		if value % 4 != 0:
			ng = True

elif W % 2 == 0:
	two = 0
	for value in c.values():
		if value % 4 != 0:
			if value % 2 != 0:
				ng = True
			else:
				two += 1
	if two > W // 2:
		ng = True

elif H % 2 == 0:
	two = 0
	for value in c.values():
		if value % 4 != 0:
			if value % 2 != 0:
				ng = True
			else:
				two += 1
	if two > H // 2:
		ng = True

else:
	two = 0
	one = 0
	three = 0
	for value in c.values():
		if value % 4 != 0:
			if value % 2 != 0:
				if value % 4 == 1:
					one += 1
				else:
					three += 1
			else:
				two += 1
	if (one+three) > 1:
		ng = True
	elif one:
		if two > H // 2 + W // 2:
			ng = True
	elif three:
		if two+1 > H // 2 + W // 2:
			ng = True

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=["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"]
L=[0]*26

for j in range(H):
    a=list(input())
    for k in range(W):
        for l in range(26):
            if a[k]==A[l]:
                L[l]+=1
                
for x in range(26):
    L[x]=L[x]%4
                
if H*W%2==1:
    if L.count(1)+L.count(3)==1 and L.count(2)+L.count(3)<=(H+W-2)//2:
        print("Yes")
    else:
        print("No")
elif H%2==1:
    if L.count(1)+L.count(3)==0 and L.count(2)<=W//2:
        print("Yes")
    else:
        print("No")
elif W%2==1:
    if L.count(1)+L.count(3)==0 and L.count(2)<=H//2:
        print("Yes")
    else:
        print("No")
else:
    if sum(L)==0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
import collections
def inpl(): return [int(i) for i in input().split()]

H, W = inpl()
a = collections.Counter(''.join([input() for _ in range(H)]))
nd2, nd4 = 0, 0 
for i in a.values():
    if i%2:
        nd2 += 1
        i -= 1
    if i%4:
        nd4 += 1
ans = 'No'
if H%2 and W%2:
    if nd2 == 1 and (H+W-2)//2 >= nd4:
        ans = 'Yes'
elif H%2:
    if not nd2 and W//2 >= nd4:
        ans = 'Yes'
elif W%2:
    if not nd2 and H//2 >= nd4:
        ans = 'Yes'
else:
    if not nd2 and not nd4:
        ans = 'Yes'
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
H,W=map(int,input().split())
c=defaultdict(int)
for i in range(H):
  s=input()
  for j in range(W):
    c[s[j]]+=1

if H%2==0 and W%2==0:
  for v in c.values():
    if v%4!=0:
      print("No")
      exit()
elif H%2==0 or W%2==0:
  k=0
  for v in c.values():
    if v%2!=0:
      print("No")
      exit()
    else:
      k+=v//4
  if k<(H//2)*(W//2):
    print("No")
    exit()
else:
  k=0
  f=0
  for v in c.values():
    if v%2!=0:
      if f==0:
        k+=v//4
        f=1
      else:
        print("No")
        exit()
    else:
      k+=v//4
  if k<(H//2)*(W//2):
    print("No")
    exit()
  
print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]

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

Mod4 = [0, 0, 0, 0]
for d in D:
    Mod4[D[d] % 4] += 1

if H%2 == W%2 == 0:
    if Mod4[1] == Mod4[2] == Mod4[3] == 0: print("Yes")
    else: print("No")
elif H%2 == 0:
    if Mod4[2] <= H//2 and Mod4[1] == Mod4[3] == 0: print("Yes")
    else: print("No")
elif W%2 == 0:
    if Mod4[2] <= W//2 and Mod4[1] == Mod4[3] == 0: print("Yes")
    else: print("No")
else:
    if Mod4[1] == 1 and Mod4[3] == 0 and Mod4[2] <= (H+W-1)//2: print("Yes")
    elif Mod4[1] == 0 and Mod4[3] == 1 and Mod4[2] <= (H+W-3)//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)]
c=[0 for i in range(26)]
x=0
for i in range(H):
    for j in range(W):
        c[ord(a[i][j])-97]+=1
y=[0,0,0,0]
for i in range(26):
    y[c[i]%4]+=1
if H%2==0 and W%2==0:
    if y[1]+y[2]+y[3]==0:
        print("Yes")
    else:
        print("No")
elif (H-W)%2==1:
    if y[1]+y[3]==0:
        X=H if H%2==0 else W
        if y[2]>X//2:
            print("No")
        else:
            print("Yes")
    else:
        print("No")
else:
    if y[1]+y[3]==1:
        P=y[3]+y[2]
        if P>(W+H-2)//2:
            print("No")
        else:
            print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
from collections import Counter as c
d=c()
h,w=map(int,input().split())
for _ in range(h):
  d+=c(input())

def solve():
  global h,w
  if h%2==0 and w%2==0:
    return False if any(map(lambda x:x%4,d.values())) else True

  e=c()
  for i in d.values():e[i%4]+=1

  if h%2 and w%2:
    if e[3] or e[1]!=1 or e[2]>(h-1)/2+(w-1)/2:return False
    return True

  if w%2:h,w=w,h
  if (e[1] or e[3]) or e[2]>w/2:return False
  return True

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