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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    H, W = map(int, raw_input().split())
    a = [list(raw_input()) for i in xrange(H)]

    cnt = [0]*26
    for ai in a:
        for c in ai:
            cnt[ord(c) - ord("a")] += 1
    if H%2 == 0 and W%2 == 0:
        for i in xrange(26):
            if cnt[i]%4 != 0:
                return False
        return True
    elif H%2 == 0 or W%2 == 0:
        for i in xrange(26):
            if cnt[i]%2 != 0:
                return False
            cnt[i] /= 2
        X = W/2 if W%2 == 0 else H/2
        odd = sum(cnt[i]%2 != 0 for i in xrange(26)) 
        return odd <= X 
    else:
        odd = sum(cnt[i]%2 != 0 for i in xrange(26)) 
        return odd == 1

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

Submission status: Wrong Answer

Solution code:
import sys
def solver1( m , h, w):
    for k, v in m.items():
        if v%2 == 1:
            m[k] = m[k] - 1
            break
    _2kcount = (h-1)/2 + (w-1)/2
    _4kcount = ((h-1)/2) * ((w-1)/2 )
    for k, v in m.items():
        if v%4 == 2:
            _2kcount -= 1
            m[k] = m[k] - 2
        elif v%4 != 0:
            return 'No'
    if _2kcount < 0:
        return 'No'
    return 'Yes'
    #we don't need to care about 4k case because the total amount of material is fix.
def solver2( m, h, w):
    oddc = h if h%2 == 1 else w
    evenc = w if w%2 == 0 else h
    _2kcount = oddc
    for k, v in m.items():
        if v%4 == 2:
            _2kcount -= 1
            m[k] = m[k] - 2
        elif v%4 != 0:
            return 'No'
    if _2kcount < 0:
        return 'No'
    return 'Yes'

H,W = map(int, sys.stdin.readline().strip().split())

matrix = []
materials = {}
for _ in xrange(H):
    line = sys.stdin.readline().strip()
    for a in line:
        materials[a] = materials.get(a, 0) + 1

if H%2 == 0 and W%2 == 0:
    for value in materials.values():
        if value % 4 == 0:
            continue
        else:
            print 'No'
            exit(0)
    print 'Yes'
    exit(0)
if H%2 == 1 and W%2 == 1:
    print solver1(materials, H, W)
    exit(0)

print solver2(materials, H, W)
exit(0)

Submission status: Wrong Answer

Solution code:
h,w = list(map(int,input().split()))
a = [0] * h
m = [0] * 26

for i in range(h):
    a[i] = list(input())
    for j in range(w):
        m[ord(a[i][j])-97] += 1

wki = w%2
hki = h%2
whan = int(w/2)
hhan = int(h/2)

m.sort()
m.reverse()

yon = int(whan * hhan)
ni =int(  (wki * hhan) + (hki * whan) )
iti = 0
if wki == 1 and hki == 1:
    iti = 1
flag = 0
for i in range(26):
    if m[i] == 1:
        flag +=1
if flag >iti:
    print('No')
    exit()
if flag == 1 and iti:
    iti = 0

for i in range(26):
    while m[i] >=4 and yon > 0:
        m[i] -= 4
        yon -= 1
for i in range(26):
    while m[i] >= 2 and ni >0:
        m[i] -= 2
        ni -= 1


if yon == 0 and ni == 0 and iti == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import itertools
import sys

h, w = map(int, input().split())

a = [input() for i in range(h)]
words = list(set(itertools.chain(*a)))
at = list(itertools.chain(*a))

mods = []
for word in words:
    mod = at.count(word) % 4
    if mod:
        mods.append(mod)

if h % 2 and w % 2:
    if (mods.count(2) == w // 2 + h // 2) and (mods.count(1) == 1):
        print("Yes")
        sys.exit()

    if (mods.count(2) == (w // 2 + h // 2) - 1) and (mods.count(3) == 1):
        print("Yes")
        sys.exit()

if mods.count(3):
    print("No")
    sys.exit()

elif h % 2:
    if mods.count(2) == w // 2:
        print("Yes")
        sys.exit()

elif w % 2:
    if mods.count(2) == h // 2:
        print("Yes")
        sys.exit()

else:
    if not mods:
        print("Yes")
        sys.exit()

print("No")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    H, W = map(int, raw_input().split())
    a = [list(raw_input()) for i in xrange(H)]

    cnt = [0]*26
    for ai in a:
        for c in ai:
            cnt[ord(c) - ord("a")] += 1
    if H%2 == 0 and W%2 == 0:
        for i in xrange(26):
            if cnt[i]%4 != 0:
                return False
        return True
    elif H%2 == 0 or W%2 == 0:
        for i in xrange(26):
            if cnt[i]%2 != 0:
                return False
            cnt[i] /= 2
        X = W/2 if W%2 == 0 else H/2
        odd = sum(cnt[i]%2 != 0 for i in xrange(26)) 
        return odd == X 
    else:
        odd = sum(cnt[i]%2 != 0 for i in xrange(26)) 
        return odd == 1

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

Submission status: Wrong Answer

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

from __future__ import division, print_function
import sys


def main():
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    aa = ""
    for _ in range(h):
        aa += sys.stdin.readline().strip()
    d = {}
    for c in aa:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    # print(d)
    fours = 0
    evens = 0
    odds = 0

    for c, count in d.items():
        fours += count // 4
        evens += (count % 4) // 2
        odds += count % 2

    # print(fours, evens, odds)
    if h % 2 == 0 and w % 2 == 0:
        print("Yes" if odds == 0 and evens == 0 else "No")
    elif h % 2 == 0 and w % 2 == 1:
        if odds != 0:
            print("No")
            return
        if 4 * fours < h * (w - 1):
            print("No")
            return
        print("Yes")
    elif h % 2 == 1 and w % 2 == 0:
        if odds != 0:
            print("No")
            return
        if 4 * fours < w * (h - 1):
            print("No")
            return
        print("Yes")
    else:
        if odds != 1:
            print("No")
            return
        if 4 * fours < w * h - w - h + 1:
            print("No")
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

from __future__ import division, print_function
import sys


def main():
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    aa = ""
    for _ in range(h):
        aa += sys.stdin.readline().strip()
    d = {}
    for c in aa:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    # print(d)
    fours = 0
    evens = 0
    odds = 0

    for c, count in d.items():
        fours += count // 4
        evens += (count % 4) // 2
        odds += count % 2

    # print(fours, evens, odds)
    if h % 2 == 0 and w % 2 == 0:
        print("Yes" if odds == 0 and evens == 0 else "No")
    elif h % 2 == 0 and w % 2 == 1:
        if odds != 0:
            print("No")
            return
        if 4 * fours < h * (w - 1):
            print("No")
            return
        print("Yes")
    elif h % 2 == 1 and w % 2 == 0:
        if odds != 0:
            print("No")
            return
        if 4 * fours < w * (h - 1):
            print("No")
            return
        print("Yes")
    else:
        print("Yes" if odds == 1 and evens * 2 == w + h - 2 else "No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a = {}
for h in range(H):
    for c in input():
        if not c in a:
            a[c] = 1
        else:
            a[c] += 1

charType = len(a.keys())
if charType >= H and charType >= W:
    print("No")
    exit()
oddHW = 0
if H % 2 == 1:
    oddHW += 1
if W % 2 == 1:
    oddHW += 1

oddChar = 0
for c in a.values():
    if c % 2 == 1:
        oddChar += 1
    if oddChar > oddHW:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
def func() :
    n,m=map(int,input().split())
    a={}
    for i in range(n) :
        s = input()
        for c in list(s):
            if c in a:
                a[c]+=1
            else :
                a[c] = 1
    c1=n*m%2
    d1=sum(filter(lambda x : x%2 , a.values()))
    if c1 != d1:
        print('No')
        return
    c4 = (n//2*2)*(m//2*2)
    d4 = sum(filter(lambda x : x//4*4 , a.values()))
    if c4>d4 :
        print('No')
        return
    print('Yes')
func()

Submission status: Wrong Answer

Solution code:
H,W = list(map(int,input().split()))
histo = {}
for i in range(H):
    for c in input():
        histo[c] = histo.get(c,0) + 1
ls = [0,0,0]
for v in histo.values():
    ls[0] += v//4
    ls[1] += (v%4)//2
    ls[2] += v%2
ans = [(H//2)*(W//2), (H//2)*(W%2)+(H%2)*(W//2), (H%2)*(W%2)]
a = "No"
if ls[0]>=ans[0]:
    ls[0] = ans[0]
    ls[1] += (ls[0]-ans[0])*2
    if ls==ans:
        a = "Yes"
print(a)

Submission status: Wrong Answer

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

from __future__ import division, print_function
import sys


def main():
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    aa = ""
    for _ in range(h):
        aa += sys.stdin.readline().strip()
    d = {}
    for c in aa:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    # print(d)
    fours = 0
    evens = 0
    odds = 0

    for c, count in d.items():
        fours += count // 4
        evens += (count % 4) // 2
        odds += count % 2

    # print(fours, evens, odds)
    if h % 2 == 0 and w % 2 == 0:
        print("Yes" if odds == 0 and evens == 0 else "No")
    elif h % 2 == 0 and w % 2 == 1:
        print("Yes" if odds == 0 and evens * 2 == h else "No")
    elif h % 2 == 1 and w % 2 == 0:
        print("Yes" if odds == 0 and evens * 2 == w else "No")
    else:
        print("Yes" if odds == 1 and evens * 2 == w + h - 2 else "No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 else "No"

Submission status: Wrong Answer

Solution code:
import itertools
import sys

h, w = map(int, input().split())

a = [input() for i in range(h)]
words = list(set(itertools.chain(*a)))
at = list(itertools.chain(*a))

mods = []
for word in words:
    mod = at.count(word) % 4
    if mod:
        mods.append(mod)

if mods.count(3):
    print("No")
    sys.exit()

elif h % 2 and w % 2:
    if (mods.count(2) == w // 2 + h // 2) and (mods.count(1) == 1):
        print("Yes")
        sys.exit()

elif h % 2:
    if mods.count(2) == w // 2:
        print("Yes")
        sys.exit()

elif w % 2:
    if mods.count(2) == h // 2:
        print("Yes")
        sys.exit()

else:
    if not mods:
        print("Yes")
        sys.exit()

print("No")

Submission status: Wrong Answer

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] >= 3:
                f = True
                alpha[k] -= 3
                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: Wrong Answer

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

if(H%2 == 0 and W%2 == 0):
    for l in C:
        if(C[l]%4):
            print("No")
            exit()
    print("Yes")
elif(H%2 and W%2):
    t = 0
    for l in C:
        if(C[l]%2):
            t += 1
            C[l] -= 1
    if(t == 0 or t > 1):
        print("No")
        exit()

    t = 0
    for l in C:
        if(C[l]%4):
            t += 1

    if(((H+W-2)//2-t)%2 == 0):
        print("Yes")
        exit()
    else:
        print("No")
        exit()
else:
    t = 0
    r = H*(W%2) + W*(H%2)
    for l in C:
        if(C[l]%2):
            print("No")
            exit()
        elif(C[l]%4):
            t += 1
    if((r//2-t)%2):
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

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:
        single = pairs = 0
        for n in counter.values():
            if n%4 == 0: continue
            if n == 1:
                if single > 0: return False
                single = 1
                continue
            if n%2 == 1:
                return False
            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: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for k, v in c.items():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return o == 0 and f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

ok = True
if H == W == 1:
    pass
elif H == 1 or W == 1:
    HW = H * W
    odd_num = sum(1 for x in c.values() if x % 2)
    even_num = HW - odd_num
    # print("odd, even = ", odd_num, even_num)
    if HW % 2 == 1 and odd_num != 1: ok = False
    elif HW % 2 == 0 and odd_num != 0: ok = False
elif H % 2 == W % 2 == 0:
    # all 4
    for v in c.values():
        if v % 4 != 0:
            ok = False
            break
elif H % 2 == W % 2 == 1:
    two_num = H + W - 2
    one_num = 1
    four_num = H * W - two_num - one_num
    if not valid(two_num, four_num, one_num, c):
        ok = False
else:
    if W % 2 == 0:
        two_num = W
    else:
        two_num = H
    four_num = H * W - two_num
    if not valid(two_num, four_num, 0, c):
        ok = False
    
if ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

from __future__ import division, print_function
import sys


def main():
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    aa = ""
    for _ in range(h):
        aa += sys.stdin.readline().strip()
    d = {}
    for c in aa:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    # print(d)
    fours = 0
    evens = 0
    odds = 0

    for c, count in d.items():
        fours += count // 4
        evens += (count % 4) // 2
        odds += count % 2

    # print(fours, evens, odds)
    if h % 2 == 0 and w % 2 == 0:
        print("Yes" if odds == 0 and evens == 0 else "No")
    elif h % 2 == 0 and w % 2 == 1:
        print("Yes" if fours * 4 + evens * 2 == h * w and evens * 2 == h else "No")
    elif h % 2 == 1 and w % 2 == 0:
        print("Yes" if fours * 4 + evens * 2 == h * w and evens * 2 == w else "No")
    else:
        print("Yes" if odds == 1 and evens * 2 == w + h - 2 else "No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    H, W = map(int, raw_input().split())
    a = [list(raw_input()) for i in xrange(H)]
    if H < W: H, W = W, H

    cnt = [0]*26
    for ai in a:
        for c in ai:
            cnt[ord(c) - ord("a")] += 1
    if H % 2 == 0 and W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 4 != 0:
                return False
        return True
    elif H % 2 == 0 or W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                return False
            cnt[i] /= 2

        X = W/2 if W % 2 == 0 else H/2
        odd = sum(cnt[i]%2 != 0 for i in xrange(26)) 
        return odd <= X 
    else:
        odd = 0
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                odd += 1
        return odd == 1

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

Submission status: Wrong Answer

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-1)//4 <= sum(v4)) and sum(l1)==0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

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

if(H%2 == 0 and W%2 == 0):
    for l in C:
        if(C[l]%4):
            print("No")
            exit()
    print("Yes")
elif(H%2 and W%2):
    t = 0
    for l in C:
        if(C[l]%2):
            t += 1
            C[l] -= 1
    if(t == 0 or t > 1):
        print("No")
        exit()

    t = 0
    for l in C:
        if(C[l]%4):
            t += 1

    if(((H+W-2)//2-t)%2 == 0):
        print("Yes")
        exit()
    else:
        print("No")
        exit()
else:
    t = 0
    r = H*(W%2) + W*(H%2)
    for l in C:
        if(C[l]%2):
            print("No")
            exit()
        elif(C[l]%4):
            t += 1
    if((r//2-t)%4):
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

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

from __future__ import division, print_function
import sys


def main():
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    aa = ""
    for _ in range(h):
        aa += sys.stdin.readline().strip()
    d = {}
    for c in aa:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    print(d)
    fours = 0
    evens = 0
    odds = 0

    for c, count in d.items():
        fours += count // 4
        evens += (count % 4) // 2
        odds += count % 2
        # if count % 4 == 0:
        #     fours += 1
        # elif count % 2 == 0:
        #     evens += 1
        # else:
        #     odds += 1

    print(fours, evens, odds)
    if h % 2 == 0 and w % 2 == 0:
        print("Yes" if odds == 0 and evens == 0 else "No")
    elif h % 2 == 0 and w % 2 == 1:
        print("Yes" if fours * 4 + evens * 2 == h * w and evens * 2 == h else "No")
    elif h % 2 == 1 and w % 2 == 0:
        print("Yes" if fours * 4 + evens * 2 == h * w and evens * 2 == w else "No")
    else:
        print("Yes" if odds == 1 and evens * 2 == w + h - 2 else "No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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]) % 2 != h * w % 2:
    print "No"
    quit()
k = 0
if h * w % 2:
    if c[1] == 1:
        c[1] -= 1
        c[0] += 1
    elif c[3] == 1:
        c[3] -= 1
        c[2] += 1
    k = h + w - 1
elif h % 2:
    k = w
elif w % 2:
    k = h
print "Yes" if c[2] <= k else "No"

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
def func() :
    n,m=map(int,input().split())
    a={}
    for i in range(n) :
        s = input()
        for c in list(s):
            if c in a:
                a[c]+=1
            else :
                a[c] = 1
    c1=n*m%2
    d1=sum(filter(lambda x : x%2 , a.values()))
    if c1 < d1:
        print('No')
        return
    c4 = (n//2*2)*(m//2*2)
    d4 = sum(filter(lambda x : x//4*4 , a.values()))
    if c4>d4 :
        print('No')
        return
    print('Yes')
func()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    H, W = map(int, raw_input().split())
    a = [list(raw_input()) for i in xrange(H)]
    if H < W: H, W = W, H

    cnt = [0]*26
    for ai in a:
        for c in ai:
            cnt[ord(c) - ord("a")] += 1
    if H % 2 == 0 and W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 4 != 0:
                return False
        return True
    elif H % 2 == 0 or W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                return False
            cnt[i] /= 2

        X = W/2 if W % 2 == 0 else H/2
        odd = sum(cnt[i]%2 == 0 for i in xrange(26)) 
        return odd <= X 
    else:
        odd = 0
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                odd += 1
        return odd == 1

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

Submission status: Wrong Answer

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: Wrong Answer

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

print(d)

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: Wrong Answer

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

if(H%2 == 0 and W%2 == 0):
    for l in C:
        if(C[l]%4):
            print("No")
            exit()
    print("Yes")
elif(H%2 and W%2):
    t = 0
    for l in C:
        if(C[l]%2):
            t += 1
            C[l] -= 1
    if(t == 0 or t > 1):
        print("No")
        exit()

    t = 0
    for l in C:
        if(C[l]%4):
            t += 1

    if(((H+W-2)//2-t)%4 == 0):
        print("Yes")
        exit()
    else:
        print("No")
        exit()
else:
    t = 0
    r = H*(W%2) + W*(H%2)
    for l in C:
        if(C[l]%2):
            print("No")
            exit()
        elif(C[l]%4):
            t += 1
    if((r//2-t)%4):
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for k, v in c.items():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

ok = True
if H == W == 1:
    pass
elif H == 1 or W == 1:
    HW = H * W
    odd_num = sum(1 for x in c.values() if x % 2)
    even_num = HW - odd_num
    # print("odd, even = ", odd_num, even_num)
    if HW % 2 == 1 and odd_num != 1: ok = False
    elif HW % 2 == 0 and odd_num != 0: ok = False
elif H % 2 == W % 2 == 0:
    # all 4
    for v in c.values():
        if v % 4 != 0:
            ok = False
            break
elif H % 2 == W % 2 == 1:
    two_num = H + W - 2
    one_num = 1
    four_num = H * W - two_num - one_num
    if not valid(two_num, four_num, one_num, c):
        ok = False
else:
    if W % 2 == 0:
        two_num = W
    else:
        two_num = H
    four_num = H * W - two_num
    if not valid(two_num, four_num, 0, c):
        ok = False
    
if ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w = list(map(int,input().split()))
a = [0] * h
m = [0] * 26

for i in range(h):
    a[i] = list(input())
    for j in range(w):
        m[ord(a[i][j])-97] += 1

wki = w%2
hki = h%2
whan = int(w/2)
hhan = int(h/2)

m.sort()
m.reverse()

yon = int(whan * hhan)
ni =int(  (wki * hhan) + (hki * whan) )
iti = 0
if wki == 1 and hki == 1:
    iti = 1
for i in range(26):
    while m[i] :
        if m[i] > 3 and yon:
            m[i] -= 4
            yon -= 1
        if m[i] > 1 and ni:
            m[i] -= 2
            ni -= 1
        if m[i] and iti:
            m[i] -= 1
            iti -= 1
        else:
            break
if yon == 0 and ni == 0 and iti == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a = {}
for h in range(H):
    for c in input():
        if not c in a:
            a[c] = 1
        else:
            a[c] += 1
oddHW = 0
if H % 2 == 1:
    oddHW += 1
if W % 2 == 1:
    oddHW += 1

oddChar = 0
for c in a.values():
    if c % 2 == 1:
        oddChar += 1
    if oddChar > oddHW:
        print("No")
        exit()
print("Yes")