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:
# coding: utf-8
def get_ln_inputs():
    return input().split()


def get_ln_int_inputs():
    return list(map(int, get_ln_inputs()))


def can_construct_palindromic_matrix(char_counts, H, W):
    double_placeable_cells = (H >> 1 if W & 1 else 0) + (W >> 1 if H & 1 else 0)
    single_placeable_cell = (H & W) & 1

    counts_mod4 = list(map(lambda x : x % 4, char_counts))
    counts_div4 = list(map(lambda x : x // 4, char_counts))
    if counts_mod4.count(1) + counts_mod4.count(3) > single_placeable_cell:
        return False

    counts_mod4 = list(map(lambda x : 0 if x & 1 else x, counts_mod4))
    if counts_mod4.count(2) > double_placeable_cells or (double_placeable_cells - counts_mod4.count(2)) & 1:
        return False

    return True


def main():
    H, W = get_ln_int_inputs()
    matrix = list()
    for _ in range(H):
        matrix.append(get_ln_inputs()[0])
    
    counts = [0 for _ in range(26)]
    for i in range(H):
        for j in range(W):
            character = matrix[i][j]
            counts[ord(character) - 97] += 1
    
    print("Yes" if can_construct_palindromic_matrix(counts, H, W) else "No")
    return


main()

Submission status: Accepted

Solution code:
from collections import Counter
H,W = map(int,input().split())
src = [input() for i in range(H)]
counter = Counter()
for row in src:
    counter.update(row)

def solve():
    if H%2 == 0 and W%2 == 0:
        for n in counter.values():
            if n%4 != 0: return False
        return True
    elif H%2 == 1 and W%2 == 1:
        odd = pairs = 0
        for n in counter.values():
            if n%4 == 0: continue
            if n%2 == 1:
                if odd > 0: return False
                odd = 1
                if (n-1)%4 == 0: continue
            pairs += 2
            if pairs > H+W-1: return False
        return True
    else:
        even = (H if H%2 == 0 else W)
        pairs = 0
        for n in counter.values():
            if n%4 == 0: continue
            if n%2 == 1: return False
            pairs += 2
            if pairs > even: return False
        return True

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

Submission status: Accepted

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

matel = {}

for i in range(H):
    line = raw_input()
    for c in line:
        if not c in matel:
            matel[c] = 0
        matel[c] += 1




def cnter():
    cnt4 = 0
    cnt2 = 0
    cnt1 = 0

    for key in matel.keys():
        vs = matel[key]
        while vs >= 4:
            cnt4+=1
            vs-=4
        while vs >= 2:
            cnt2 += 1
            vs-=2
        cnt1 +=vs

    return cnt1,cnt2,cnt4



def solve():
    cnt1,cnt2,cnt4 = cnter()

    if H%2 == 0 and W%2 == 0:
        if cnt2 == 0 and cnt1 == 0:
            print "Yes"
        else:
            print "No"
    elif H%2 == 1 and W%2 == 1:
        if cnt1 != 1:
            print "No"
            return
        po = (W/2)+(H/2) - cnt2
        if po < 0:
            print "No"
            return

        if po %2 == 0:
            print "Yes"
        else:
            print "No"

    else:
        if cnt1:
            print "No"
            return 
        if H%2 == 1:
            po = W/2 - cnt2
        else:
            po = H/2 - cnt2

        if po < 0:
            print "No"
            return
        if po % 2 == 0:
            print "Yes"
        else:
            print "No"

solve()

Submission status: Accepted

Solution code:
from collections import defaultdict
h, w = map(int, raw_input().split())
a = [raw_input().strip() for _ in xrange(h)]
d = defaultdict(int)
for r in a:
    for c in r:
        d[c] += 1
c = [0] * 4
for x in d.values():
    c[x%4] += 1
if c[1] + c[3] > 1 or c[1] + c[3] != h * w % 2:
    print "No"
    quit()
k = 0
if h * w % 2:
    if c[1]:
        c[0] += 1
    elif c[3]:
        c[2] += 1
    k = h + w - 2
elif h % 2:
    k = w
elif w % 2:
    k = h
print "Yes" if c[2] <= k / 2 else "No"

Submission status: Accepted

Solution code:
# def makelist(n, m):
#     return [[0 for i in range(m)] for j in range(n)]

yes = "Yes"
no = "No"

H, W = map(int, input().split())
alph = [0]*26

for i in range(H):
    for c in input():
        alph[ord(c) - 97] += 1

four = 0
two = 0
one = 0
ans = no
for e in alph:
    four += e // 4
    e %= 4
    two += e // 2
    one += e % 2

#print(four, two, one)
    
if (H, W) == (1, 1):
    ans = yes
else:
    if H % 2 == 0 and W % 2 == 0:
        if one > 0 or two > 0:
            ans = no
        else:
            ans = yes
    elif H % 2 == 0 and W % 2 == 1:
        if one == 0 and two <= H // 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 0:
        if one == 0 and two <= W // 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 1:
        if one == 1 and two <= H // 2 + W // 2:
            ans = yes

print(ans)

Submission status: Accepted

Solution code:
import sys, string

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

alpha = [0 for i in range(26)]

for _ in range(H):
    a = input()
    for i, char in enumerate(string.ascii_lowercase):
        alpha[i] += a.count(char)

for i in range(H//2):
    for j in range(W//2):
        f = False
        for k in range(26):
            if alpha[k] >= 4:
                f = True
                alpha[k] -= 4
                break

        if not f:
            print('No')
            exit()

print(alpha, file=sys.stderr)

if H % 2 == 1:
    for j in range(W // 2):
        f = False
        for k in range(26):
            if alpha[k] >= 2:
                f = True
                alpha[k] -= 2
                break

        if not f:
            print('No')
            exit()

print(alpha, file=sys.stderr)

if W % 2 == 1:
    for i in range(H // 2):
        f = False
        for k in range(26):
            if alpha[k] >= 2:
                f = True
                alpha[k] -= 2
                break

        if not f:
            print('No')
            exit()

print(alpha, file=sys.stderr)
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
a = [input() for i in range(H)]
ans = "Yes"
c = [0 for i in range(26)]
for i in range(H):
    for j in range(W):
        c[ord(a[i][j])-ord('a')] += 1
c1 = 0
c2 = 0
c4 = 0

if H == 1 and W == 1:
    ans = "Yes"

elif H % 2 == 0 and W % 2 == 0:
    for i in range(26):
        if not c[i] % 4 == 0:
            ans = "No"
            break

else:
    for i in range(26):
        c4 += int(c[i]/4)
        c2 += int((c[i]%4)/2)
        c1 += c[i]%2
    if c1 != H*W%2:
        ans = "No"
    if H % 2 == 1 and W % 2 == 0 and c2 > (H % 2) * (W / 2):
        ans = "No"
    if H % 2 == 0 and W % 2 == 1 and c2 > (H / 2) * (W % 2):
        ans = "No"
    if H % 2 == 1 and W % 2 == 1 and c2 > int(H / 2) * (W % 2) + (H % 2) * int(W / 2):
        ans = "No"



print(ans)

Submission status: Accepted

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

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

for _ in range(H):
    C.update(input())


if H%2 == 0 and W%2 == 0:
    for v in C.values():
        if v%4 != 0:
            print("No")
            break
    else:
        print("Yes")

elif (H%2 == 1) and (W%2 == 1):
    values = list(C.values())
    v4 = [v//4 for v in values]
    l1 = [v%2 for v in values]
    if ((H-1)*(W-1)//4 <= sum(v4)) and sum(l1)==1:
        print("Yes")
    else:
        print("No")

else:
    if H%2 == 0:
        H, W = W, H
    values = list(C.values())
    v4 = [v//4 for v in values]
    l1 = [v%2 for v in values]
    if ((H-1)*(W)//4 <= sum(v4)) and sum(l1)==0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

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

counts = {}
for h in xrange(H):
  for c in raw_input():
    counts[c] = counts.get(c, 0) + 1

odd = two = 0
for count in counts.itervalues():
  if count % 2 == 1:
    odd += 1
  elif count % 4 != 0:
    two += 1

if (H * W) % 2 == 1:
  # all odd
  ans = odd <= 1 and two <= (H-1)/2 + (W-1)/2
elif H % 2 == 1:
  ans = (odd == 0 and two <= W/2)
elif W % 2 == 1:
  # odd and even
  ans = (odd == 0 and two <= H/2)
else:
  # all even
  ans = (odd == 0 and two == 0)

print ['No', 'Yes'][ans]

Submission status: Accepted

Solution code:
import string

[H,W]=map(int, raw_input().split())
lw=string.ascii_lowercase
A=[0 for i in range(26)]
for i in range(H):
    st=raw_input()
    for c in st:
        A[lw.index(c)]+=1

K=(W/2)*(H/2)
L=(W*(H%2)+H*(W%2)-2*(H%2)*(W%2))/2
M=(H%2)*(W%2)

f=0
ans=["No","Yes"]
P=sum([a/4 for a in A])
#print P,K
if P>=K:
    P-=K
    while(K>0):
        for i in range(26):
            if A[i]>=4:
                A[i]-=4
                K-=1
                if K==0: break
        #print K,A
    Q=sum([a/2 for a in A])
    #print Q,L
    if Q>=L:
        Q-=L
        L=0
        R=Q*2+sum([a%2 for a in A])
        #print R,M
        if R>=M:
            f=1
print ans[f]

Submission status: Accepted

Solution code:
H, W = [int(x) for x in input().split(" ")]

d = {}

for h in range(H):
    row = input()
    for a in list(row):
        if a in d:
            d[a] += 1
        else:
            d[a] = 1


res = False

if H % 2 == 0 and W % 2 == 0:
    for v in d.values():
        if v % 4 != 0:
            res = True
            break
    res = not res

elif H % 2 == 1 and W % 2 == 0:
    cnt4 = (W // 2) * ((H - 1) // 2)
    cnt2 = (W // 2)
    temp4 = 0
    temp2 = 0
    f = True
    for v in d.values():
        if v % 2 != 0:
            f = False
            break

        temp4 += v // 4
        temp2 += 1 if v % 4 != 0 else 0
    
    if f and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True
elif H % 2 == 0 and W % 2 == 1:
    cnt4 = (H // 2) * ((W - 1) // 2)
    cnt2 = (H // 2)
    temp4 = 0
    temp2 = 0
    f = True
    for v in d.values():
        if v % 2 != 0:
            f = False
            break

        temp4 += v // 4
        temp2 += 1 if v % 4 != 0 else 0
    
    if f and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True
else:
    cnt4 = ((H - 1) // 2) * ((W - 1) // 2)
    cnt2 = ((H - 1) // 2) + ((W - 1) // 2)
    temp4 = 0
    temp2 = 0
    temp1 = 0

    for v in d.values():
        temp4 += v // 4
        temp2 += (v % 4) // 2
        temp1 += (v % 2)
    
    if temp1 == 1 and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True

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

Submission status: Accepted

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

hw = raw_input()
h,w = hw.split()
h = int(h)
w = int(w)

al = [chr(i) for i in range(97, 97+26)]

a = ""
for i in range(h):
    s = raw_input()
    a += s

#print a

#print al

c = []

for x in al:
    count = a.count(x)
    c.append(count)

#print c

status1 = 0
status2 = 0
for x in c:
    if x % 4 == 0:
        pass
    elif x % 2 == 0:
        status1 += 1
    else:
        status2 += 1

ans = 0
if h % 2 == 0 and w % 2 == 0:
    if status1 > 0 or status2 > 0:
        ans = 1
elif h % 2 == 0:
    if status2 > 0:
        ans = 1
    elif status1 > h / 2:
        ans = 1
elif w % 2 == 0:
    if status2 > 0:
        ans = 1
    elif status1 > w / 2:
        ans = 1
else:
    if status2 > 1:
        ans = 1
    elif status1 > h / 2 + w / 2:
        ans = 1

if ans == 0:
    print "Yes"
else:
    print "No"

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import defaultdict

def II(): return int(input())
def ILI(): return list(map(int, input().split()))


def read():
    H, W = ILI()
    count = defaultdict(int)
    for __ in range(H):
        sen = str(input())
        for i in range(W):
            count[sen[i]] += 1
    return H, W, count


def solve(H, W, count):
    odd = 0
    num_2 = 0
    num_4 = 0
    for val in count.values():
        div_2, mod_2 = divmod(val, 2)
        odd += mod_2
        div_2 *= 2
        div_4, mod_4 = divmod(div_2, 4)
        num_4 += div_4 * 4
        num_2 += mod_4
    ans = 0
    if H % 2 == 0:
        if W % 2 == 0:
            if odd == 0:
                if num_4 == H * W:
                    ans = "Yes"
                else:
                    ans = "No"
            else:
                ans = "No"
        else:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > H:
                    ans = "No"
                else:
                    ans = "Yes"
    else:
        if W % 2 == 0:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > W:
                    ans = "No"
                else:
                    ans = "Yes"
        else:
            if odd != 1:
                ans = "No"
            else:
                if num_2 > (W - 1) + (H - 1):
                    ans = "No"
                else:
                    ans = "Yes"
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
H,W=map(int,input().split())
L=[0]*26
for i in range(H):
  A=str(input())
  for j in range(W):
    L[ord(A[j])-97]+=1

fours0=0
fours1=0
fours2=0
fours3=0
for i in range(26):
  if L[i]%4==0:
    fours0+=1
  if L[i]%4==1:
    fours1+=1
  if L[i]%4==2:
    fours2+=1
  if L[i]%4==3:
    fours3+=1

if W%2==0 and H%2==0:
  if fours1+fours2+fours3==0: print("Yes")
  else: print("No")
elif W%2==1 and H%2==1:
  if fours1+fours3<=1 and fours1+fours3+2*fours2<=W+H-1:
    print("Yes")
  else: print("No")
elif W%2==1: 
  if fours1+fours3==0 and fours2*2<=H:
    print("Yes")
  else: print("No")
else: 
  if fours1+fours3==0 and fours2*2<=W:
    print("Yes")
  else: print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import heapq
import math
import sys

tmp = input()
H, W = [int(a) for a in tmp.split()]
is_evens = [a % 2 == 0 for a in [H, W]]
H2, W2 = [(a+1) // 2 for a in [H, W]]

chars = []
for i in range(H):
    tmp = input()
    chars = chars + [a for a in tmp]
char_counts = {}
for char in chars:
    if char not in char_counts:
        char_counts[char] = 0
    char_counts[char] += 1

valids = {'4': 0, '2': 0, '1': 0}
if is_evens[0] and is_evens[1]:
    valids['4'] = H2 * W2
elif is_evens[0] and not is_evens[1]:
    valids['4'] = (W2 - 1) * H2
    valids['2'] = H2
elif is_evens[1] and not is_evens[0]:
    valids['4'] = (H2 - 1) * W2
    valids['2'] = W2
else:
    valids['4'] = (H2 - 1) * (W2 - 1)
    valids['2'] = (H2 - 1) + (W2 - 1)
    valids['1'] = 1
nows = {'4': 0, '2': 0, '1': 0}
for c, num_ in char_counts.items():
    num = num_
    if num % 2 == 1:
        nows['1'] += 1
        num -= 1
        if nows['1'] > valids['1']:
            print('No')
            sys.exit()
    if num % 4 == 2:
        nows['2'] += 1
        num -= 2
        if nows['2'] > valids['2']:
            print('No')
            sys.exit()
    nows['4'] += (num // 4)
if nows['4'] > valids['4']:
    amari = nows['4'] - valids['4']
    nows['2'] += amari * 2
    if nows['2'] > valids['2']:
        print('No')
        sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
from collections import Counter
import string
H,W = list(map(int,input().split()))


a = [input() for _ in range(H)]

c= Counter("".join(a))


if H==1 or W==1:
    allow1 =H %2 + W%2 -1
    allow2 = (H+W-1)//2
else:
    allow1 = 1 if H%2 and W%2 else 0
    allow2 = H%2*W//2 + W%2*H//2

for s in string.ascii_lowercase:
    count = c[s]
    if count%4==0:
        continue

    elif count%4==2:
        allow2 -=1

    elif count%4 ==1:
        allow1 -=1
    elif count%4==3:
        allow1 -=1
        allow2 -=1

if allow1>=0 and allow2 >=0:
    print("Yes")
else:
    print("No")
#print(allow1,allow2)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(h, w, a):
    c = Counter(a)
    m1, m2 = 0, 0
    for v in c.values():
        if v & 3 == 0:
            continue
        elif v & 1 == 0:
            m2 += 1
        else:
            m1 += 1
            if (v - 1) & 3:
                m2 += 1

    if h & 1:
        if w & 1:
            if m1 > 1:
                return 'No'
            if m2 > h // 2 + w // 2:
                return 'No'
            return 'Yes'
        if m1 > 0:
            return 'No'
        if m2 > w // 2:
            return 'No'
        return 'Yes'
    if m1 > 0:
        return 'No'
    if w & 1:
        if m2 > h // 2:
            return 'No'
        return 'Yes'
    if m2 > 0:
        return 'No'
    return 'Yes'


h, w = map(int, input().split())
a = ''.join(input().strip() for _ in range(h))
print(solve(h, w, a))

Submission status: Accepted

Solution code:
import sys
import socket
from collections import Counter

if socket.gethostname() == 'N551J':
    sys.stdin = open('c1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve():
    h, w = read_int_list()
    c = Counter()
    for i in range(h):
        c += Counter(read_str())
    values = sorted(c.values(), reverse=True)
    m = [4] * ((h // 2) * (w // 2))
    if h % 2 == 1:
        m = m + [2] * (w // 2)
    if w % 2 == 1:
        m = m + [2] * (h // 2)
    if h % 2 == 1 and w % 2 == 1:
        m = m + [1]
    for v in m:
        if values[0] < v:
            return 'No'
        else:
            values[0] -= v
            if values[0] == 0:
                values.pop(0)
            else:
                values.sort(reverse=True)
    return 'Yes'


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

import collections

def main():
    H, W = map(int, input().split())
    cnt = collections.Counter()
    for i in range(H):
        for c in input().strip():
            cnt[c] += 1
    cnts = list(cnt.values())
    l = len(cnts)

    if H % 2 and W % 2:
        for i in range(l):
            if cnts[i] % 2 != 0:
                cnts[i] -= 1
                break
    d = 0
    if H % 2:
        d += (W // 2) * 2
    if W % 2:
        d += (H // 2) * 2
    i = 0
    while d > 0 and i < l:
        if cnts[i] % 4 != 0:
            cnts[i] -= 2
            d -= 2
        i += 1

    for k in cnts:
        if k % 4 != 0:
            return "No"
    return "Yes"


print(main())

Submission status: Accepted

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

cnt = {}
for ai in a:
    for c in ai:
        if c in cnt:
            cnt[c] += 1
        else:
            cnt[c] = 1
c1 = 0
c2 = 0
c4 = 0
for k, v in cnt.items():
    c4 += (v // 4) * 4
    c2 += ((v % 4) // 2) * 2
    c1 += v % 2

ans = "No"
if h % 2 == 0:
    if w % 2 == 0:
        if c4 == h * w:
            ans = "Yes"
    else:
        if c2 <= h and c1 == 0:
            ans = "Yes"
else:
    if w % 2 == 0:
        if c2 <= w and c1 == 0:
            ans = "Yes"
    else:
        if c2 <= h + w - 1 and c1 == 1:
            ans = "Yes"
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
H, W = map(int, input().split())
cnt4, cnt2, cnt1 = 0, 0, 0
for v in Counter(''.join(input() for _ in range(H))).values():
    if v % 4 == 0:
        continue
    elif v % 2 == 0:
        cnt2 += 1
    else:
        cnt1 += 1
        if (v - 1) % 4 != 0:
            cnt2 += 1
odd = (H % 2 == 1) + (W % 2 == 1)
if odd == 2:
    if cnt1 <= 1 and cnt2 <= (H + W - 2) // 2:
        print("Yes")
    else:
        print("No")
elif odd == 1 and H % 2 == 1:
    if cnt1 == 0 and cnt2 <= W // 2:
        print("Yes")
    else:
        print("No")
elif odd == 1 and W % 2 == 1:
    if cnt1 == 0 and cnt2 <= H // 2:
        print("Yes")
    else:
        print("No")
else:
    if cnt1 == 0 and cnt2 == 0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter

def check():
    h, w = map(int, input().split()) 
    c = Counter()
    for i in range(h):
        c.update(input())
    cc = {}
    cc[4] = (h >> 1) * (w >> 1)
    cc[2] = (h & 1) * (w >> 1) + (w & 1) * (h >> 1)
    cc[1] = (h & 1) * (w & 1)
    tt = list(c.values())
    for v in [4, 2, 1]:
        for i in range(len(tt)):
            k = min(cc[v], tt[i] // v)
            tt[i] -= k * v
            cc[v] -= k
    return sum(tt) == 0


print(["No", "Yes"][check()])

Submission status: Accepted

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

cnt = [0, 0, 0, 0]
for k, v in counter.items():
    cnt[v % 4] += 1
if h % 2 == 0 and w % 2 == 0:
    if cnt[1] == 0 and cnt[2] == 0 and cnt[3] == 0:
        print('Yes')
    else:
        print('No')
elif h % 2 == 1 and w % 2 == 1:
    if (cnt[1] == 1 and cnt[2] <= (h - 1)//2 + (w - 1)//2 and cnt[3] == 0) or (cnt[1] == 0 and cnt[2] <= (h - 1)//2 + (w - 1)//2 - 1 and cnt[3] == 1):
        print('Yes')
    else:
        print('No')
elif h % 2 == 1:
    if cnt[1] == 0 and cnt[3] == 0 and cnt[2] <= w // 2:
        print('Yes')
    else:
        print('No')
else:
    if cnt[1] == 0 and cnt[3] == 0 and cnt[2] <= h // 2:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
Y,X = map(int, raw_input().split())
nc = [0]*256
for y in range(Y):
	s = raw_input()
	for c in s:
		nc[ord(c)] += 1
n1 = 0
n2 = 0
for i in range(256):
	if nc[i]%4 != 0:
		if nc[i]%2 == 0:
			n2 += nc[i]%4
		else:
			n2 += nc[i]%4/2*2
			n1 += nc[i]%2

m1 = 0
m2 = 0
if X%2 and Y%2:
	m1 = 1
	m2 = X+Y-1
else:
	if X%2:
		m2 += Y
	if Y%2:
		m2 += X

#print n1,n2,m1,m2
if m1 < n1 or m2 < n2:
	print "No"
else:
	print "Yes"

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
s = ''
for i in range(n):
	s += raw_input()
c = [s.count(i) % 4 for i in 'abcdefghijklmnopqrstuvwxyz']
d1 = 0
d2 = 0
if n % 2 == 0 and m % 2 == 0:
	pass
if n % 2 == 0 and m % 2 == 1:
	d2 = n / 2
if n % 2 == 1 and m % 2 == 0:
	d2 = m / 2
if n % 2 == 1 and m % 2 == 1:
	d2 = n / 2 + m / 2
	d1 = 1

for i in c:
	while i >= 2 and d2 > 0:
		i -= 2
		d2 -= 1
	while i >= 1 and d1 > 0:
		i -= 1
		d1 -= 1
	if i > 0:
		print 'No'
		exit()

print 'Yes'

Submission status: Runtime Error

Solution code:
from collections import defaultdict
H,W = map(int,input().split())
d = defaultdict(int)
for _ in range(H):
    for c in list(input()):
        d[c] += 1
for i in range((H-1)//2+1):
    for j in range((W-1)//2+1):
        s = set([(x,y) for x,y in [(i,j),(i,W-1-j),(H-1-i,j),(H-1-i,W-1-j)] if 0<=x<H and 0<=y<W])
        f = True
        for k,_ in sorted(d.items(),key=lambda x:x[1]):
            if d[k] >= len(s):
                d[k] -= len(s)
                f = False
                for x,y in s:
                    test[x][y] = k
                break
        if f:
            print("No")
            exit(0)
print("Yes")

Submission status: Runtime Error

Solution code:
from collections import Counter
h,w=map(int,input().split())
#a=[list(input()) for i in range(w)]
a=[]
flag=True
for i in range(h):
  a+=list(input())
ca=Counter(a)
if h%2==0 and w%2==0:
  for i in ca:
    if ca[i]%4!=0:
      flag=False
      print('No')
      break
  if flag:
    print('Yes')

elif h%2!=0 and w%2!=0:
  yon=(h-1)*(w-1)
  ni=h*w-yon-1
  iti=1
  for i in ca:
    while ca[i]>0
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      elif iti>0:
        iti-=1
        ca[i]-=1
      else:
        break
  if yon==0 and ni==0 and iti==0:
    print('Yes')
  else:
    print('No')
else:
  if h%2==0:
    yon=(h)*(w-1)
  else:
    yon=(h-1)*(w)
  ni=h*w-yon
  for i in ca:
    while ca[i]>0:
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      else:
        break
  if yon==0 and ni==0:
    print('Yes')
  else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import Counter
import numpy as np

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

arr = sum(arr, [])

arr = Counter(arr)
arr = np.array(list(arr.values()))


def solve(arr):
    # 偶数×偶数なら、制約がかなりきつい
    if h % 2 == 0 and w % 2 == 0:
        return np.all(c % 4 == 0)

    # 奇数×奇数なら、
    elif h % 2 == 1 and w % 2 == 1:
        even = (h + w) // 2 - 1
        if (arr % 2 == 1).sum() != 1:
            return False
        odd = arr[np.where(arr % 2 == 1)].sum()
        if odd % 4 == 3:
            even -= 1
        arr = arr[np.where(arr != odd)]
        ans = (arr % 4 == 2).sum() <= even
        ans &= np.all(arr[np.where(arr % 4 != 2)] % 4 == 0)
        return ans

    #　どちらかの辺が偶数なら
    else:
        even = h // 2 if h % 2 == 0 else w // 2
        ans = (arr % 4 == 2).sum() <= even
        ans &= np.all(arr[np.where(arr % 4 != 2)] % 4 == 0)
        return ans


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

Submission status: Runtime Error

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

    H,W = map(int,input().split())
    C = defaultdict(int)
    for _ in range(H):
        row = list(input())
        for i in range(W):
            C[row[i]] += 1
    if H%2==0 and W%2==0:
        flag = 0
        for a in C:
            if C[a]%4!=0:
                flag = 1
                break
        if flag==0:
            print("Yes")
        else:
            print("No")
    elif (H%2==0 and W%2==1) or (H%2==1 and W%2==0):
        n = (H//2)*(W//2)
        for a in C:
            if C[a]>=4:
                n -= C[a]//4
                C[a] = C[a]%4
        if n>0:
            print("No")
        else:
            flag = 0
            for a in C:
                if C[a]%2==1:
                    flag=1
                    break
            if flag==0:
                print("Yes")
            else:
                print("No")
    else:
        n = (H//2)*(W//2)
        for a in C:
            if C[a]>=4:
                n -= C[a]//4
                C[a] = C[a]%4
        if n>0:
            print("No")
        else:
            cnt = 0
            for a in C:
                cnt += C[a]%2
            if cnt==1:
                print("Yes")
            else:
                print("No")


    # 1つの行において列数が偶数の場合、例えば左端にaを置いた場合は、
    # その列がpalindromeになる関係上必然的に右端にもaを置くことになる。
    # さらに、列方向に見た場合は、今置いた2つのaに対して、一番下の行で
    # それぞれを置く必要がある。したがって、1つのaを置くと、芋づる式に
    # 合計４つのaを置くことになる。
    # 行・列のどちらかが奇数の場合は、その真ん中のところにおいては
    # 2個でよかったり、3個でよかったり、1個で良かったりするので、場合分けする。
    # 1つの行において列数が偶数の場合、例えば左端にaを置いた場合は、
    # その列がpalindromeになる関係上必然的に右端にもaを置くことになる。
    # さらに、列方向に見た場合は、今置いた2つのaに対して、一番下の行で
    # それぞれを置く必要がある。したがって、1つのaを置くと、芋づる式に
    # 合計４つのaを置くことになる。
    # 行・列のどちらかが奇数の場合は、その真ん中のところにおいては
    # 2個でよかったり、3個でよかったり、1個で良かったりするので、場合分けする。

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main() {
    int h,w;
    cin >> h >> w;
    vector<string> a(h);
    for (int i=0; i<h; i++) cin >> a[i];
    for (int i=0; i<h; i++) sort(a[i].begin(), a[i].end());

    bool ans = true;
    for (int i=0; i<h; i++) {
        map<char, int> mp;
        for (const auto& c : a[i]) mp[c]++;
        vector cnt(2,0);
        for (const auto& [a,b] : mp) cnt[b%2]++;
        if (cnt[1] > 1) ans = false;
    }


    map<string, int> mp;
    for (int i=0; i<h; i++) {
        mp[a[i]]++;
    }
    vector cnt(2,0);
    for (const auto& [a,b] : mp) cnt[b%2]++;
    if (cnt[1] > 1) ans = false;

    cout << (ans? "Yes" : "No") << endl;
}

Submission status: Runtime Error

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

l1 = h*w%2
l2 = (h//2)*(w%2) + (w//2)*(h%2)
l3 = (h//2)*(w//2)
if l1:
    for key, val in cA.items():
        if val%2==1:
            cA[key] -= 1
            break
for _ in range(l2):
    for key, val in cA.items():
        if val%4=-2:
            cA[key] -= 2
            break
print('Yes' if sum(val%4 for val in cA.values()) == 0 else 'No')

Submission status: Runtime Error

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

l1 = h*w%2
l2 = (h//2)*(w%2) + (w//2)*(h%2)
l3 = (h//2)*(w//2)
if l1:
    for key, val in cA.items():
        if val%2==1:
            cA[key] -= 1
            break
for _ in range(l2):
    for key, val in cA.items():
        if val%4=-2:
            cA[key] -= 2
            break
print('Yes' if sum(a[i]%4 for i in a) == 0 else 'No')

Submission status: Runtime Error

Solution code:
import collections
h,w = map(int, input().split())
s = ''.join([''.join(input().split()) for _ in range(h)])

ctr = collections.Counter(s)
if h % 2 == 0 and w % 2 == 0:
    four, two = h*w//4
elif h % 2 == 0:
    four, two = (h*w-h)//4, h//2
elif w % 2 == 0:
    four, two = (h*w-w)//4, w//2
else:
    four, two = (h*w-h-w+1), (h+w-1)//2

cf = sum([c//4 for c in ctr.values()])
ct = sum([(c%4)//2 for c in ctr.values()])

if cf >= four and ct >= 2*(four-cf) + two:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import Counter
h,w=map(int,input().split())
s=""
for i in range(h):
  s+=input()
sc=Counter(s)
d=[0,0,0]
for v in sc.values():
    d[0]+=(v//4)*4
    d[1]+=2 if v%4>=2 else 0
    d[2]+=v%2

if w%2==0 and h%2==0:
  print("Yes" if d[0]==h*w else "No")
elif (w%2)*(h%2)==1:
  print("Yes" if d[1]<=(w-1)+(h-1) and d[2]==1 else "No")
elif w%2==0:
  print("Yes" if d[1]<=w and d[2]==0 else "No")
elif h%2==0:
  print("Yes" if d[1]<=h and d[2]==0 else "No")

from collections import Counter
h,w=map(int,input().split())
s=""
for i in range(h):
  s+=input()
sc=Counter(s)
d=[0,0,0]
for v in sc.values():
    d[0]+=(v//4)*4
    d[1]+=2 if v%4>=2 else 0
    d[2]+=v%2

if w%2==0 and h%2==0:
  print("Yes" if d[0]==h*w else "No")
elif (w%2)*(h%2)==1:
  print("Yes" if d[1]<=(w-1)+(h-1) and d[2]==1 else "No")
elif w%2==0:
  print("Yes" if d[1]<=w and d[2]==0 else "No")
elif h%2==0:
  print("Yes" if d[1]<=h and d[2]==0 else "No")