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: Runtime Error

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    h, w = map(int, input().split())
    a = [list(input()) for i in range(h)]
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    
    if h == 1 and w == 1:
        print("Yes")
        exit()
    elif h == 1:
        l = [2] * (w // 2)
        if w % 2 == 1:
            l.append(1)
    elif w == 1:
        l = [2] * (h // 2)
        if h % 2 == 1:
            l.append(1)
    else:
        l = []
        if h == w:
            if h % 2 == 0:
                l = [4] * (h * h // 4)
            else:
                l = [4] * ((h - 1) * (h - 1) // 4)
                for i in range(h):
                    if i == h - 1:
                        l.append(1)
                    else:
                        l.appned(2)
        else:
            if h > w:
                h, w = w, h
            
            if h % 2 == 0:
                l = [4] * (h * h // 4)
                for i in range((h * w - h * h) // 2):
                    l.append(2)
            else:
                l = [4] * (w * w // 4)
                t = 0
                for i in range((w * w - w * h) // 2):
                    l[t] = 2
                    t += 1
    
    l = sorted(l, reverse=True)
    
    h = []
    heapify(h)
    cnt = 0
    for v in d.values():
        if v % 2 == 1:
            cnt+=1
        heappush(h, -v)
    if cnt > 1:
        print("No")
        exit()

    for v in l:
        z = -heappop(h)
        if z < v:
            print("No")
            exit()
        z -= v
        heappush(h, -z)
    print("Yes")
    

                


    






    


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    h, w = map(int, input().split())
    a = [list(input()) for i in range(h)]
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    
    if h == 1 and w == 1:
        print("Yes")
        exit()
    elif h == 1:
        l = [2] * (w // 2)
        if w % 2 == 1:
            l.append(1)
    elif w == 1:
        l = [2] * (h // 2)
        if h % 2 == 1:
            l.append(1)
    else:
        l = []
        if h == w:
            if h % 2 == 0:
                l = [4] * (h * h // 4)
            else:
                l = [4] * ((h - 1) * (h - 1) // 4)
                for i in range(h):
                    if i == h - 1:
                        l.append(1)
                    else:
                        l.appned(2)
        else:
            if h > w:
                h, w = w, h
            
            if h % 2 == 0:
                l = [4] * (h * h // 4)
                for i in range((h * w - h * h) // 2):
                    l.append(2)
            else:
                l = [4] * (w * w // 4)
                t = 0
                for i in range((w * w - w * h) // 2):
                    l[t] = 2
                    t += 1
    
    l = sorted(l, reverse=True)
    
    h = []
    heapify(h)
    cnt = 0
    for v in d.values():
        if v % 2 == 1:
            c+=1
        heappush(h, -v)
    if cnt > 1:
        print("No")
        exit()

    for v in l:
        z = -heappop(h)
        if z < v:
            print("No")
            exit()
        z -= v
        heappush(h, -z)
    print("Yes")
    

                


    






    


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    h, w = map(int, input().split())
    a = [list(input()) for i in range(h)]
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    
    if h == 1 and w == 1:
        print("Yes")
        exit()
    elif h == 1:
        l = [2] * (w // 2)
        if w % 2 == 1:
            l.append(1)
    elif w == 1:
        l = [2] * (h // 2)
        if h % 2 == 1:
            l.append(1)
    else:
        l = []
        if h == w:
            if h % 2 == 0:
                l = [4] * (h * h // 4)
            else:
                l = [4] * ((h - 1) * (h - 1) // 4)
                for i in range(h):
                    if i == h - 1:
                        l.append(1)
                    else:
                        l.appned(2)
        else:
            if h > w:
                h, w = w, h
            
            if h % 2 == 0:
                l = [4] * (h * h // 4)
                for i in range((h * w - h * h) // 2):
                    l.append(2)
            else:
                l = [4] * (w * w // 4)
                t = 0
                for i in range((w * w - w * h) // 2):
                    l[t] = 2
                    t += 1
    
    l = sorted(l, reverse=True)
    
    h = []
    heapify(h)
    cnt = 0
    for v in d.values():
        if v % 2 == 1:
            c+=1
        heappush(h, -v)
    if c > 1:
        print("No")
        exit()

    for v in l:
        z = -heappop(h)
        if z < v:
            print("No")
            exit()
        z -= v
        heappush(h, -z)
    print("Yes")
    

                


    






    


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    h, w = map(int, input().split())
    a = [list(input()) for i in range(h)]
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    
    if h == 1 and w == 1:
        print("Yes")
        exit()
    elif h == 1:
        l = [2] * (w // 2)
        if w % 2 == 1:
            l.append(1)
    elif w == 1:
        l = [2] * (h // 2)
        if h % 2 == 1:
            l.append(1)
    else:
        l = []
        if h == w:
            if h % 2 == 0:
                l = [4] * (h * h // 4)
            else:
                l = [4] * ((h - 1) * (h - 1) // 4)
                for i in range(h):
                    if i == h - 1:
                        l.append(1)
                    else:
                        l.appned(2)
        else:
            if h > w:
                h, w = w, h
            
            if h % 2 == 0:
                l = [4] * (h * h // 4)
                for i in range((h * w - h * h) // 2):
                    l.append(2)
            else:
                l = [4] * (w * w // 4)
                t = 0
                for i in range((w * w - w * h) // 2):
                    l[t] = 2
                    t += 1
    
    l = sorted(l, reverse=True)
    
    h = []
    heapify(h)
    for v in d.values():
        heappush(h,-v)
    
    for v in l:
        z = -heappop(h)
        if z < v:
            print("No")
            exit()
        z -= v
        heappush(h, -z)
    print("Yes")
    

                


    






    


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    h, w = map(int, input().split())
    a = [list(input()) for i in range(h)]
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    
    if h == 1 and w == 1:
        print("Yes")
        exit()
    elif h == 1:
        l = [2] * (w // 2)
        if w % 2 == 1:
            l.append(1)
    elif w == 1:
        l = [2] * (h // 2)
        if h % 2 == 1:
            l.append(1)
    else:
        l = []
        if h == w:
            if h % 2 == 0:
                l = [4] * (h * h // 4)
            else:
                l = [4] * ((h - 1) * (h - 1) // 4)
                for i in range(h):
                    if i == n - 1:
                        l.append(1)
                    else:
                        l.appned(2)
        else:
            if h > w:
                h, w = w, h
            
            if h % 2 == 0:
                l = [4] * (h * h // 4)
                for i in range((h * w - h * h) // 2):
                    l.append(2)
            else:
                l = [4] * (w * w // 4)
                t = 0
                for i in range((w * w - w * h) // 2):
                    l[t] = 2
                    t += 1
    
    l = sorted(l, reverse=True)
    
    h = []
    heapify(h)
    for v in d.values():
        heappush(h,-v)
    
    for v in l:
        z = -heappop(h)
        if z < v:
            print("No")
            exit()
        z -= v
        heappush(h, -z)
    print("Yes")
    
    


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
dic = {}
for h in range(H):
  s = list(input())
  for x in s:
    dic[x] = dic.get(x,0) + 1

c1 = 0
c2 = 0
for x in dic.values():
  if x%2 == 1:
    c1 += 1
    x -= 1
  if x%4 == 2:
    c2 += 1
if c1>(H%2)*(W%2):
  print("No")
  return
if c2>((W*(H%2))//2)+((H*(W%2))//2):
  print("No")
  return
print("Yes")

Submission status: Runtime Error

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

strings = [0] * 26
for i in range(H):
    s = list(map(int, input().split()))
    for j in range(W):
        strings[ord(s[j])-97] -= 1

one = 0
two = 0
four = 0
s = H * W
if H % 2 == 1 and W % 2 == 1:
    one = 1
    two = H // 2 + W // 2
    four = (s-1-2*two)//4
elif H % 2 == 1:
    two = W // 2
    four = (s - two*2)//4
elif W % 2 == 1:
    two = H // 2
    four = (s - two*2)//4
else:
    four = s // 4

flag = 1
while True:
    if four > 0:
        a = heappop(strings)
        if a <= -4:
            a += 4
            heappush(strings, a)
        else:
            flag = 0
            break

    elif two > 0:
        a = heappop(strings)
        if a <= -2:
            a += 2
            heappush(strings, a)
        else:
            flag = 0
            break
    elif one > 0:
        a = heappop(strings)
        if a <= -1:
            a += 1
            heappush(strings, a)
        else:
            flag = 0
            break
    else:
        break

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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
for i in range(H):
    S[i].sort()
S.sort()
ans = [[None] * W for _ in range(H)]


l = 0
for i in range(H):
    k = 0
    if i % 2 == 0:
        for j in range(W):
            if j % 2 == 0:
                ans[l][k] = S[i][j]
            else:
                ans[l][W-k-1] = S[i][j]
                k += 1
    else:
        for j in range(W):
            if j % 2 == 0:
                ans[H-l-1][k] = S[i][j]
            else:
                ans[H-l-1][W-k-1] = S[i][j]
                k += 1
        l += 1

for i in range(H):
    for j in range(W):
        if ans[i][j] != ans[i][W-j-1] or ans[i][j] != ans[H-i-1][j] or a[i][j] != a[H-i-1][W-j-1]:
            print("No")
            exit()

print("Yes")

Submission status: Runtime Error

Solution code:
from collections import defaultdict as de
p=de(int)
h,w=map(int,input().split())
for i in range(h):
    a=list(input())
    for j in range(w):
        p[ord(a[i])-ord("a")]+=1
c=0
d=0
for i in p.values():
    c+=i%2
    d+=i//2
if w%2==1:
    if h-c>=0 and (h-c)%2==0:
        print("Yes")
    else:
        print("No")
else:
    if c!=0:
        print("No")
    else:
        print("Yes")

Submission status: Runtime Error

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

four=0
two=0
one=0

for i in  range(26):
  c=chr(ord("a")+i)
  if d[c]>=4:
    four+=d[c]//4 
    d[c]%=4
  if d[c]>=2:
    two+=d[c]//2
    d[c]%=2
  if d[c]>=1:
    one+=d[c]
    d[c]-=1
    
if four<=(H//2)*(W//2):    
  print("No")
  exit(0)
four-=(H//2)*(W//2)
two+=four*2

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

print("Yes")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections
flg = 1
flg1 = 0
S = [[s for s in a] for a in A]
for s in S:
    c = collections.Counter(s)
    for i in c.values():
        if i % 2 != 0 and len(c.keys()) != 1:
            flg1 = flg1 + 1
            if W % 2 == 0 and flg1 >= 1:
                flg = 0
                break
            elif W % 2 != 0 and flg1 >= 2:
                flg = 0
                break

if flg == 1:    
    S_t = [list(x) for x in zip(*S)]
    for s in S_t:
        c = collections.Counter(s)
        for i in c.values():
            if i % 2 != 0 and len(c.keys()) != 1:
                flg1 = flg1 + 1
                if H % 2 == 0 and flg1 >= 1:
                    flg = 0
                    break
                elif H % 2 != 0 and flg1 >= 2:
                    flg = 0
                    break

if flg == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

H, W = map(int, input().split())
a = ""
for i in range(H):
    a += input()

# count = {}
count_all = len(a)
count_4 = 0
count_2 = 0
count_1 = 0

for s in "abcdefghijklmnopqrstuvwxyz":
    # count[s] = a.count(s)
    c = a.count(s)
    count_4 += c//4
    c = c%4
    count_2 += c//2
    c = c%2
    count_1 += c

if W%2 == 1 and H%2 == 0:
    W, H = H, W

if W%2 == 0 and H%2 == 0:
    if count_4 < W*H//4:
        print("No")
    else:
        print("Yes")
elif W%2 == 0 and H%2 == 1:
    if count_4 < W*(H-1)//4:
        # print(count_4, W*(H-1)//4)
        sys.stderr.write((count_4, W*(H-1)//4))
        print("No")
    elif 2*(count_4 - W*(H-1)//4) + count_2 < W//2:
        print("No")
    else:
        print("Yes")
else:
    if count_4 < (W-1)*(H-1)//4:
        print("No")
    elif 2*(count_4 - (W-1)*(H-1)//4) + count_2 < (W+H-2)//2:
        print("No")
    else:
        print("Yes")

Submission status: Runtime Error

Solution code:
from collections import Counter


H, W = map(int, input().split())
A = [input() for _ in range(H)]

char_to_cnt = Counter()
for h in range(H):
    for w in range(W):
        char_to_cnt.update(A[h][w])

def check(table):
    for i in range(H):
        if "".join(table[i]) != "".join(reversed(table[i])):
            return False
    for j in range(W):
        row = [table[i][j] for i in range(H)]
        if "".join(row) != "".join(reversed(row)):
            return False
    return True



ans = False
def dfs(i, j, table, remain):
    global ans
    if ans:
        return

    # print(i, j, table, remain)
    for char, cnt in remain.items():
        if cnt == 0:
            continue

        new_table = list(table)
        new_table[i][j] = char

        if i >= H // 2:
            if new_table[i][j] != new_table[(H - i - 1)][j]:
                continue

        if j >= W // 2:
            if new_table[i][j] != new_table[i][(W - j - 1)]:
                continue

        if i == H - 1 and j == W - 1:
            if check(new_table):
                ans = True
            return

        new_remain = Counter(remain)
        new_remain[char] -= 1

        if j == W - 1:
            dfs(i + 1, 0, new_table, new_remain)
        else:
            dfs(i, j + 1, new_table, new_remain)

table = [["" for _ in range(W)] for i in range(H)]
dfs(0, 0, table, char_to_cnt)

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

Submission status: Runtime Error

Solution code:
from collections import Counter


H, W = map(int, input().split())
A = [input() for _ in range(H)]

char_to_cnt = Counter()
for h in range(H):
    for w in range(W):
        char_to_cnt.update(A[h][w])

def check(table):
    for i in range(H):
        if "".join(table[i]) != "".join(reversed(table[i])):
            return False
    for j in range(W):
        row = [table[i][j] for i in range(H)]
        if "".join(row) != "".join(reversed(row)):
            return False
    return True

ans = False
def dfs(i, j, table, remain):
    global ans
    if ans:
        return
    # print(i, j, table, remain)
    for char, cnt in remain.items():
        if cnt == 0:
            continue

        new_table = list(table)
        new_table[i][j] = char
        if i == H - 1 and j == W - 1:
            if check(new_table):
                ans = True
            return

        new_remain = Counter(remain)
        new_remain[char] -= 1

        if j == W - 1:
            dfs(i + 1, 0, new_table, new_remain)
        else:
            dfs(i, j + 1, new_table, new_remain)

table = [["" for _ in range(W)] for i in range(H)]
dfs(0, 0, table, char_to_cnt)

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

Submission status: Runtime Error

Solution code:
from collections import Counter
H, W = list(map(int, input().split()))
cnt = Counter()
for i in range(H):
    ls = list(map(str, input().split()))
    for j in ls:
        A = Counter(j)
        cnt += A

ki = 0
gu = 0
f = 0
cnt = 0
for i, j in cnt.most_common():
    cnt += 1
    if (j % 2) != 0:
        ki += 1
    elif (j % 2) == 0:
        gu += 1
    elif (j %4) == 0:
        f += 1
oth = cnt - f


if H == 1 and W == 1:
    print('Yes')
elif (H == 1 or W == 1) and ki == 1:
    print('Yes')
elif oth == 0 and H%2 == 0 and W%2 == 0:
    print('Yes')
elif H%2 != 0 and W%2 == 0 and gu == int(H/2):
    print('Yes')
elif H%2 == 0 and W%2 != 0 and gu == int(W/2):
    print('Yes')
elif H%2 != 0 and W%2 != 0 and gu == int(W/2) + int(H/2):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

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 > 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: Runtime Error

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 > 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: Runtime Error

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 > 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)

print(num4)
print(num2)

decision(H,W,num4,num2)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

    g1 = (w % 2) & (h % 2)
    g2 = (w % 2) * h//2 + (h % 2) * w//2
    g4 = h//2 * w//2
    
    for k in wd.keys():
        if (g1 > 0) and v % 4 == 1 or v % 4 == 3:
            g1 -= 1
            wd[k] -= 1
        while (g4 > 0) and v >= 4:
            g4 -= 1
            wd[k] -= 4
        while (g2 > 0) and v >= 2:
            g2 -= 1
            wd[k] -= 2
            
    print("No" if sum(wd.values()) else "Yes")
    
if __name__=="__main__":
    solve()

Submission status: Runtime Error

Solution code:
def count_unique_char(s):
	d = {}
	ret = 0
	for c in s:
		if c in d:
			d[c] += 1
		else:
			d[c] = 1
	for k, v in sorted(d.items(), key=lambda x:x[1]):
		if v%2 != 0:
			ret += 1
	return ret

def judge(s, is_odd):
	# return value => True  (OK)
	#              => False (NG)
	if n == 0:
		return True
	elif is_odd == False:
		if n <= 1:
			return True
		else:
			return False
	else:
		return False
def disp(ans):
	if ans == True:
		print('Yes')
	else:
		print('No')

w, h = map(int, input().split())
if min(w, h) == 1:
	if h == 1:
		s = input()
	else:
		s = ''
		for i in range(h):
			s += input()
	n = count_unique_char(s)
	if max(w, h)%2 == 0:
		ans = judge(s, True)
	else:
		ans = judge(s, False)
else:
	s_list = ["" for i in range(w)]
	ans = True
	is_odd = True if (w%2 == 0) else False
	for i in range(h):
		s = input()
		n = count_unique_char(s) 
		if judge(n, is_odd) == False:
			ans = False
			break
		for i, c in enumerate(s):
			s_list[i] += c
	is_odd = True if (h%2 == 0) else False
	for s in s_list:
		n = count_unique_char(s)
		if judge(n, is_odd) == False:
			ans = False
			break
		
disp(ans)

Submission status: Runtime Error

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:
    while(char[c] > 0):
        four += char[c] / 4:
        two += char[c]%4/2:
        one += char[c]%2:

if four*4+two*2+one == hoge[0]*hoge[1]:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
from collections import defaultdict

def foo():
	H, W = map(int, input().split())
	s = ""
	for _ in range(H):
		s += input()

	counter = defaultdict(int)
	for c in s:
		counter[c] += 1

	x1, x2 = H // 2, H % 2
	y1, y2 = W // 2, W % 2

	n = x1 * y1
	k = 0
	for c in counter:
		if k == n:
			break
		while k != n and counter[c] >= 4:
			k += 1
			counter[c] -= 4

	if k != n:
		print("No")
		return

	if x2 == 1:
		n = y1
		k = 0
		for c in counter:
			if k == n:
				break
			while k != n and counter[c] >= 2:
				k += 1
				counter[c] -= 2
		if k != n:
			print("No")
			return

	if y2 == 1:
		n = x1
		k = 0
		for c in counter:
			if k == n:
				break
			while k != n and counter[c] >= 2:
				k += 1
				counter[c] -= 2
		if k != n:
			print("No")
			return

	if sum(counter.values() )	
	print("Yes")

foo()

Submission status: Runtime Error

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

using namespace std;

int main() {
	int h,w;
	cin >> h >> w;
	map<char,int> cnt;
	for(int i = 0; i < h; i++) {
		string s;
		cin >> s;
		for(char c : s) cnt[c]++;
	}
	int c1 = 0;
	int c2 = 0;
	for(auto x : cnt) {
		if(x.second % 2) c1 ++;
		c2 += x.second / 4 * 4;
	}
	if(c1 != (h*w)%2 || c2 < (h/2*2)*(w/2*2) ) cout << "No" << endl;
	else cout << "Yes" << endl;
	return 0;
}

Submission status: Runtime Error

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:
            pos = H/2 - cnt2

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

solve()

Submission status: Runtime Error

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:
        odd += 1
    if oddChar > oddHW:
        print("No")
        break
print("Yes")