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:
from collections import Counter

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

c = Counter()
for i in range(H):
    c += Counter(list(input()))
cl = sorted(list(c.values()),reverse=True)

if H%2 == 0 and W%2 == 0:
    a = [4]*(H*W//4)
elif H%2 == 0 and W%2 == 1:
    a = [4]*((H-1)*W//4)
    a.extend([2]*(H//2))
elif H%2 == 1 and W%2 == 0:
    a = [4]*(H*(W-1)//4)
    a.extend([2]*(W//2))
elif H%2 == 1 and W%2 == 1:
    a = [4]*((H*W-H-W+1)//4)
    a.extend([2]*((H+W-2)//2))
    a.append(1)

if len(cl) > len(a):
    print("No")
    exit()
else:
    for a in a:
        for i,c in enumerate(cl):
            if c - a >= 0:
                cl[i] -= a
                break
            if i == len(cl)-1:
                print("No")
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

c = Counter()
for i in range(H):
    c += Counter(list(input()))
cl = sorted(list(c.values()),reverse=True)

if H%2 == 0 and W%2 == 0:
    a = [4]*(H*W//4)
elif H%2 == 0 and W%2 == 1:
    a = [4]*((H-1)*W//4)
    a.extend([2]*(H//2))
elif H%2 == 1 and W%2 == 0:
    a = [4]*(H*(W-1)//4)
    a.extend([2]*(W//2))
elif H%2 == 1 and W%2 == 1:
    a = [4]*((H*W-H-W+1)//4)
    a.extend([2]*((H+W-1)//2))
    a.append(1)

if len(cl) > len(a):
    print("No")
    exit()
else:
    for a in a:
        for i,c in enumerate(cl):
            if c - a >= 0:
                cl[i] -= a
                break
            if i == len(cl)-1:
                print("No")
print("Yes")

Submission status: Wrong Answer

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

from collections import Counter
cnt = Counter()
for i in range(H):
    cnt.update(input())

num = [0 for i in range(4)]
for c in cnt.values():
    num[3] += c // 4
    if c % 4 > 0:
        num[c % 4 - 1] += 1

h2 = H // 2
w2 = W // 2
if H % 2 == 0 and W % 2 == 0:
    if num == [0, 0, 0, h2 * w2]:
        print('Yes')
    else:
        print('No')

elif H % 2 != 0 and W % 2 != 0:
    if num == [1, h2 + w2, 0, h2 * w2]:
        print('Yes')
    else:
        print('No')

else:
    if H % 2 == 0: e2 = h2
    else: e2 = w2

    if num == [0, e2, 0, h2 * w2]:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

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

def main():
    h,w = LI()
    d = collections.defaultdict(int)
    for _ in range(h):
        for c in S():
            d[c] += 1

    t = 0
    t2 = 0
    for v in d.values():
        t += v//4
        t2 += (v%4) // 2

    f = h * (w//2) + w * (h//2)
    if t < f:
        return 'No'
    t2 += t-f
    if w%2 == 1:
        t2 -= h//2
    if h%2 == 1:
        t2 -= w//2
    if t2 < 0:
        return 'No'
    return 'Yes'



print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

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

def main():
    h,w = LI()
    d = collections.defaultdict(int)
    for _ in range(h):
        for c in S():
            d[c] += 1

    t = 0
    t2 = 0
    for v in d.values():
        t += v//4
        t2 += (v%4) // 2

    f = (h//2) * (w//2)
    if t < f:
        return 'No'
    t2 += t-f
    if w%2 == 1:
        t2 -= h//2
    if h%2 == 1:
        t2 -= w//2
    if t2 < 0:
        return 'No'
    return 'Yes'



print(main())

Submission status: Wrong Answer

Solution code:
def fourcount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 4):
			tmp += 1

		elif tmp == 4:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 4:
		num += 1
		
	return num
		

def twocount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 2):
			tmp += 1

		elif tmp == 2:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 2:
		num += 1
		
	return num

def decision(H,W,num4,num2):

	if H == 1:
		if (W % 2 == 0) and (num2 == W/2):
			print("Yes")
		elif (W % 2 == 1) and (num2 == (W-1)/2):
			print("Yes")
		else:
			print("No")

	elif W == 1:
		if (H % 2 == 0) and (num2 == H/2):
			print("Yes")
		elif (H % 2 == 1) and (num2 == (H-1)/2):
			print("Yes")
		else:
			print("No")

	elif H == 0 or W == 0:
		print("Yes")

	elif H > W:
		if (H % 2 == 0) and (H / 2 <= num4):
			same = H/2
			decision(H,W-2,num4-same,num2-same*2)
		elif (H % 2 == 1) and ((H-1) / 2 <= num4 ) and (num2 >= 1):
			same =  (H-1) /2
			decision(H,W-2,num4-same,num2-same*2-1)
		else:
			print("No")

	elif W > H:
		if (W % 2 == 0) and (W / 2 <= num4):
			same= W/2
			decision(H-2,W,num4-same,num2-same*2)
		elif (W % 2 == 1) and ((W-1) / 2 <= num4) and (num2 >= 1):
			same =  (W-1) /2
			decision(H-2,W,num4-same,num2-same*2-1)
		else:
			print("No")

	elif W == H:
		if (H % 2 == 0) and (H - 1) <= num4 :
			same = H-1
			decision(H-2,W-2,num4-same,num2-same*2)
		elif (H % 2 == 1) and (H - 2) <= num4 and (num2 >= 2):
			same = H-2
			decision(H-2,W-2,num4-same,num2-same*2-2)
		else :
			print("No")



H,W = map(int,input().split(" "))
a = []
b = []

for i in range(H):
	a = list(input())
	a = a[0:W]
	for string in a:
		b.append(string)

b.sort()

num4 = fourcount(b)
num2 = twocount(b) 

decision(H,W,num4,num2)

Submission status: Wrong Answer

Solution code:
def fourcount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 4):
			tmp += 1

		elif tmp == 4:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 4:
		num += 1
		
	return num
		

def twocount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 2):
			tmp += 1

		elif tmp == 2:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 2:
		num += 1
		
	return num

def decision(H,W,num4,num2):

	if H == 1:
		if (W % 2 == 0) and (num2 == W/2):
			print("Yes")
		elif (W % 2 == 1) and (num2 == (W-1)/2):
			print("Yes")
		else:
			print("No")

	elif W == 1:
		if (H % 2 == 0) and (num2 == H/2):
			print("Yes")
		elif (H % 2 == 1) and (num2 == (H-1)/2):
			print("Yes")
		else:
			print("No")

	elif H == 0 or W == 0:
		print("Yes")

	elif H > W:
		if (H % 2 == 0) and (H / 2 <= num4):
			same = H/2
			decision(H,W-2,num4-same,num2)
		elif (H % 2 == 1) and ((H-1) / 2 <= num4 ) and (num2 >= 1):
			same =  (H-1) /2
			decision(H,W-2,num4-same,num2-1)
		else:
			print("No")

	elif W > H:
		if (W % 2 == 0) and (W / 2 <= num4):
			same= W/2
			decision(H-2,W,num4-same,num2)
		elif (W % 2 == 1) and ((W-1) / 2 <= num4) and (num2 >= 1):
			same =  (W-1) /2
			decision(H-2,W,num4-same,num2-1)
		else:
			print("No")

	elif W == H:
		if (H % 2 == 0) and (H - 1) <= num4 :
			decision(H-2,W-2,num4-(H-1),num2)
		elif (H % 2 == 1) and (H - 2) <= num4 and (num2 >= 2):
			decision(H-2,W-2,num4-(H-2),num2-2)
		else :
			print("No")



H,W = map(int,input().split(" "))
a = []
b = []

for i in range(H):
	a = list(input())
	a = a[0:W]
	for string in a:
		b.append(string)

b.sort()

num4 = fourcount(b)
num2 = twocount(b) - (num4*2)

decision(H,W,num4,num2)

Submission status: Wrong Answer

Solution code:
def fourcount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 4):
			tmp += 1

		elif tmp == 4:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 4:
		num += 1
		
	return num
		

def twocount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 2):
			tmp += 1

		elif tmp == 2:
			num += 1
			tmp = 1

		else:
			tmp = 1

	if tmp == 2:
		num += 1
		
	return num

def decision(H,W,num4,num2):

	if H == 1:
		if (W % 2 == 0) and (num2 == W/2):
			print("Yes")
		elif (W % 2 == 1) and (num2 == (W-1)/2):
			print("Yes")
		else:
			print("No")

	elif W == 1:
		if (H % 2 == 0) and (num2 == H/2):
			print("Yes")
		elif (H % 2 == 1) and (num2 == (H-1)/2):
			print("Yes")
		else:
			print("No")

	elif H == 0 or W == 0:
		print("Yes")

	elif H > W:
		if (H % 2 == 0) and (H / 2 <= num4):
			same = H/2
			decision(H,W-2,num4-same,num2)
		elif (H % 2 == 1) and ((H-1) / 2 <= num4 ) and (num2 >= 1):
			same =  (H-1) /2
			decision(H,W-2,num4-same,num2-1)
		else:
			print("No")

	elif W > H:
		if (W % 2 == 0) and (W / 2 <= num4):
			same= W/2
			decision(H-2,W,num4-same,num2)
		elif (W % 2 == 1) and ((W-1) / 2 <= num4) and (num2 >= 1):
			same =  (W-1) /2
			decision(H-2,W,num4-same,num2-1)
		else:
			print("No")

	elif W == H:
		if (H % 2 == 0) and (H - 1) <= num4 :
			decision(H-2,W-2,num4-(H-1),num2)
		elif (H % 2 == 1) and (H - 2) <= num4 and (num2 >= 2):
			decision(H-2,W-2,num4-(H-2),num2-2)
		else :
			print("No")



H,W = map(int,input().split(" "))
a = []
b = []

for i in range(H):
	a = list(input())
	a = a[0:W]
	for string in a:
		b.append(string)

b.sort()

print(b)

num4 = fourcount(b)
num2 = twocount(b) - (num4*2)

decision(H,W,num4,num2)

Submission status: Wrong Answer

Solution code:
def fourcount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 4):
			tmp += 1

		elif tmp == 4:
			num += 1
			tmp = 1

	if tmp == 4:
		num += 1
		
	return num
		

def twocount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 2):
			tmp += 1

		elif tmp == 2:
			num += 1
			tmp = 1

	if tmp == 2:
		num += 1
		
	return num

def decision(H,W,num4,num2):

	if H == 1:
		if (W % 2 == 0) and (num2 == W/2):
			print("Yes")
		elif (W % 2 == 1) and (num2 == (W-1)/2):
			print("Yes")
		else:
			print("No")

	elif W == 1:
		if (H % 2 == 0) and (num2 == H/2):
			print("Yes")
		elif (H % 2 == 1) and (num2 == (H-1)/2):
			print("Yes")
		else:
			print("No")

	elif H == 0 or W == 0:
		print("Yes")

	elif H > W:
		if (H % 2 == 0) and (H / 2 <= num4):
			same = H/2
			decision(H,W-2,num4-same,num2)
		elif (H % 2 == 1) and ((H-1) / 2 <= num4 ) and (num2 >= 1):
			same =  (H-1) /2
			decision(H,W-2,num4-same,num2-1)
		else:
			print("No")

	elif W > H:
		if (W % 2 == 0) and (W / 2 <= num4):
			same= W/2
			decision(H-2,W,num4-same,num2)
		elif (W % 2 == 1) and ((W-1) / 2 <= num4) and (num2 >= 1):
			same =  (W-1) /2
			decision(H-2,W,num4-same,num2-1)
		else:
			print("No")

	elif W == H:
		if (H % 2 == 0) and (H - 1) <= num4 :
			decision(H-2,W-2,num4-(H-1),num2)
		elif (H % 2 == 1) and (H - 2) <= num4 and (num2 >= 2):
			decision(H-2,W-2,num4-(H-2),num2-2)
		else :
			print("No")



H,W = map(int,input().split(" "))
a = []
b = []

for i in range(H):
	a = list(input())
	a = a[0:W]
	for string in a:
		b.append(string)

b.sort()

num4 = fourcount(b)
num2 = twocount(b) - (num4*2)

decision(H,W,num4,num2)

Submission status: Wrong Answer

Solution code:
def fourcount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 4):
			tmp += 1

		else:
			if tmp == 4:
				num += 1
			
			tmp = 1 
		
	return num	

def twocount(List):

	num = 0
	tmp = 1
	l = len(List)

	for i in range(1,l):

		if (List[i-1] == List[i]) and (tmp < 2):
			tmp += 1

		else:
			if tmp == 2:
				num += 1
			
			tmp = 1 
		
	return num

def decision(H,W,num4,num2):

	if H == 1:
		if (W % 2==0) and (num2 == W/2):
			print("Yes")
		elif (W % 2 == 1) and (num2 == (W-1)/2):
			print("Yes")
		else:
			print("No")

	elif W == 1:
		if (H % 2==0) and (num2 == H/2):
			print("Yes")
		elif (H % 2 == 1) and (num2 == (H-1)/2):
			print("Yes")
		else:
			print("No")

	elif H > W:
		if (H % 2 == 0) and (H / 2 <= num4):
			same = H/2
			decision(H,W-2,num4-same,num2)
		elif (H % 2 == 1) and ((H-1) / 2 <= num4 ) and (num2 >= 1):
			same =  (H-1) /2
			decision(H,W-2,num4-same,num2-1)
		else:
			print("No")

	elif W > H:
		if (W % 2 == 0) and (W / 2 <= num4):
			same = H/2
			decision(H-2,W,num4-same,num2)
		elif (W % 2 == 1) and ((W-1) / 2 <= num4) and (num2 >= 1):
			same =  (W-1) /2
			decision(H-2,W,num4-same,num2-1)
		else:
			print("No")

	elif W == H:
		if (H % 2 == 0) and (H - 1) <= num4 :
			decision(H-2,W-2,num4-(H-1),num2)
		elif (H % 2 == 1) and (H - 2) <= num4 and (num2 >= 2):
			decision(H-2,W-2,num4-(H-2),num2-2)
		else :
			print("No")



H,W = map(int,input().split(" "))
a = []
b = []

for i in range(H):
	a = list(input())
	a = a[0:W]
	for string in a:
		b.append(string)

b.sort()

num4 = fourcount(b)
num2 = twocount(b) - (num4*2)

decision(H,W,num4,num2)

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=sum(filter(lambda x : x%2 , a.values()))
    c2=sum(filter(lambda x : x//4*4, a.values()))
    if c1 != n*m%2 or c2 < (n//2*2)*(m//2*2) :
        print('No')
    else :
        print('Yes')
func()

Submission status: Wrong Answer

Solution code:
hoge = [int(x) for x in input().split()]
moji = ''
char = {}
for i in range(hoge[0]):
    moji += input()
for m in moji:
    if m in char:
        char[m] += 1
    else:
        char.update({m:1})

four = 0
two = 0
one = 0
for c in char:
    four += int(char[c] / 4)
    two += int(char[c]%4/2)
    one += int(char[c]%2)


if hoge[0] % 2 == 1 and hoge[1] % 2 == 1:
    if one == 1 and two == (hoge[0]-1)/2 + (hoge[1]-1)/2 and four == (hoge[0]-1)/2 * (hoge[1]-1)/2:
        print("Yes")
    else:
        print("No")
elif hoge[0] % 2 == 1 and hoge[1] % 2 == 0:
    if one == 0 and two == hoge[1]/2 and four == (hoge[0]-1)/2 * hoge[1]/2:
        print("Yes")
    else:
        print("No")
elif hoge[0] % 2 == 0 and hoge[1] % 2 == 1:
    if one == 0 and two == hoge[0]/2 and four == (hoge[1]-1)/2 * hoge[0]/2:
        print("Yes")
    else:
        print("No")
else:
    if one == 0 and two == 0 and four == hoge[1]/2 * hoge[0]/2:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
def checkParindrome(s):
    d = dict()
    oddNum = 0
    for c in s:
        if c not in d:
            d[c] = 0
        d[c] += 1

    for v in d.values():
        if(v%2 == 1):
            oddNum += 1
    return oddNum < 2

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

a = [list(input()) for i in range(H)]
b = list(map(list, zip(*a)))

isPalindrome = True
for i in range(H):
    if(not checkParindrome(a[i])):
        isPalindrome = False
        break

for j in range(W):
    if(not checkParindrome(b[j])):
        isPalindrome = False
        break

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    word_dict = dict(Counter(list("".join([input() for _ in range(h)]))))

    g1 = (w % 2) & (h % 2)
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for k, v in word_dict.items():
        if v % 4 == 1 or v % 4 == 3:
            g1 -= 1
            word_dict[k] -= 1
            v -= 1
        if g1 == 0:
            break
            
    for k, v in word_dict.items():
        while v > 0 and v % 4 == 0:
            g4 -= 1
            word_dict[k] -= 4
            v -= 4
        if g4 == 0:
            break

    for k, v in word_dict.items():
        while v > 0 and v % 4 == 2:
            g2 -= 1
            word_dict[k] -= 2
            v -= 2
        if g2 == 0:
            break
    
    print("No" if [1 for v in word_dict.values() if v != 0] else "Yes")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    word_dict = dict(Counter(list("".join([input() for _ in range(h)]))))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.items():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key != 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.items():
            if v > 0 and v % 4 == 2:
                key = k
                break
        if key != 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.items():
            if v > 0 and v % 4 == 0:
                key = k
                break
        if key != 0:
            word_dict[key] -= 4
    
    print("Yes" if len([1 for k, v in word_dict.items() if v != 0]) == 0 else "No")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    word_dict = Counter(list("".join([input() for _ in range(h)])))

    g1 = 1 if (w % 2 == 1 and h % 2 == 1) else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key != 0:
            word_dict[key] -= 1
        else:
            print("No")
            return

    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v >= 4 and v % 4 == 0:
                key = k
                break
        if key != 0:
            word_dict[key] -= 4
        else:
            print("No")
            return
        
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v >= 2 and v % 4 == 2:
                key = k
                break
        if key != 0:
            word_dict[key] -= 2
        else:
            print("No")
            return
    print("Yes")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    word_dict = Counter(list("".join([input() for _ in range(h)])))

    g1 = 1 if (w % 2 == 1 and h % 2 == 1) else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key != 0:
            word_dict[key] -= 1
        else:
            print("No")
            return
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v >= 2 and v % 4 == 2:
                key = k
                break
        if key != 0:
            word_dict[key] -= 2
        else:
            print("No")
            return
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v >= 4 and v % 4 == 0:
                key = k
                break
        if key != 0:
            word_dict[key] -= 4
        else:
            print("No")
            return
    
    print("Yes")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    word_dict = Counter(list("".join([input() for _ in range(h)])))

    g1 = 1 if (w % 2 == 1 and h % 2 == 1) else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key != 0:
            word_dict[key] -= 1
        else:
            print("No")
            return
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key != 0:
            word_dict[key] -= 2
        else:
            print("No")
            return
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v > 0 and v % 4 == 0:
                key = k
                break
        if key != 0:
            word_dict[key] -= 4
        else:
            print("No")
            return
    
    print("Yes")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key != 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key != 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v > 0 and v % 4 == 0:
                key = k
                break
        if key != 0:
            word_dict[key] -= 4
    
    print("Yes" if len([1 for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = floor((w % 2) * h/2) + floor((h % 2) * w/2)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v > 0 and v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = (h//2 if w % 2 == 1 else 0) \
            + (w//2 if h % 2 == 1 else 0)
    g4 = h//2 * w//2
    
    for _ in range(g1):
        key = ""
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = ""
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = ""
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import floor

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = (floor(h/2) if w % 2 == 1 else 0) \
            + (floor(w/2) if h % 2 == 1 else 0)
    g4 = floor(h/2) * floor(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = (int(h/2) if w % 2 == 1 else 0) \
            + (int(w/2) if h % 2 == 1 else 0)
    g4 = int(h/2) * int(w/2)
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4

    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
        
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = (int(h/2) if w % 2 == 1 else 0) \
            + (int(w/2) if h % 2 == 1 else 0)
    g4 = int(h/2) * int(w/2)
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0 and word_dict[key] >= 4:
            word_dict[key] -= 4

    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0 and word_dict[key] >= 2:
            word_dict[key] -= 2
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0 and word_dict[key] >= 1:
            word_dict[key] -= 1
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2 = (int(h/2) if w % 2 == 1 else 0) \
            + (int(w/2) if h % 2 == 1 else 0)
    g4 = int(h/2) * int(w/2)
    
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()