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:
h,w=map(int,input().split())
a={}
for i in range(26):
  a[chr(i+97)]=0
for i in range(h):
  s=input()
  for j in s:
    a[j]+=1
if h==1 and w==1:print("Yes")
elif h==1 or w==1:
  cnt=0
  for i in a.values():
    if i%2==1:cnt+=1
  if cnt!=1:print("No")
  else:print("Yes")
elif h%2==0 and w%2==0:
  for i in a.values():
    if i%4!=0:print("No");exit()
  print("Yes")
elif h%2==1 and w%2==0:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>w//2:
    print("No")
  else:print("Yes")
elif h%2==0 and w%2==1:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt3=0;cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
    elif i%4==3:cnt3+=1
  if cnt3!=0:print("No")
  elif cnt1!=1:print("No")
  elif cnt2>h+w-2:print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a={}
for i in range(26):
  a[chr(i+97)]=0
for i in range(h):
  s=input()
  for j in s:
    a[j]+=1
if h==1 and w==1:print("Yes")
elif h==1 or w==1:
  cnt=0
  for i in a.values():
    if i%2==1:cnt+=1
  if cnt!=1:print("No")
  else:print("Yes")
elif h%2==0 and w%2==0:
  for i in a.values():
    if i%4!=0:print("No");exit()
  print("Yes")
elif h%2==1 and w%2==0:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>w//2:
    print("No")
  else:print("Yes")
elif h%2==0 and w%2==1:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt3=0;cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
    elif i%4==3:cnt3+=1
  if cnt1+cnt3!=1:print("No")
  elif 2*(cnt2+cnt3)>h+w-2:print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a={}
for i in range(26):
  a[chr(i+97)]=0
for i in range(h):
  s=input()
  for j in s:
    a[j]+=1
if h==1 and w==1:print("Yes")
elif h==1 or w==1:
  cnt=0
  for i in a.values():
    if i%2==1:cnt+=1
  if cnt!=2:print("No")
  else:print("Yes")
elif h%2==0 and w%2==0:
  for i in a.values():
    if i%4!=0:print("No");exit()
  print("Yes")
elif h%2==1 and w%2==0:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>w//2:
    print("No")
  else:print("Yes")
elif h%2==0 and w%2==1:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt3=0;cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
    elif i%4==3:cnt3+=1
  if cnt1+cnt3!=1:print("No")
  elif 2*(cnt2+cnt3)>h+w-2:print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a={}
for i in range(26):
  a[chr(i+97)]=0
for i in range(h):
  s=input()
  for j in s:
    a[j]+=1
if h==1 and w==1:print("Yes")
elif h==1 or w==1:
  cnt=0
  for i in a.values():
    if i%2==1:cnt+=1
  if cnt>2:print("No")
  else:print("Yes")
elif h%2==0 and w%2==0:
  for i in a.values():
    if i%4!=0:print("No");exit()
  print("Yes")
elif h%2==1 and w%2==0:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>w//2:
    print("No")
  else:print("Yes")
elif h%2==0 and w%2==1:
  cnt=0
  for i in a.values():
    if i%4==1 or i%4==3:
      print("No");exit()
    if i%4==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt3=0;cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
    elif i%4==3:cnt3+=1
  if cnt1+cnt3>1:print("No")
  elif 2*(cnt2+cnt3)>h+w-2:print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

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

d_al = dict()

for ai in a:
    for aij in ai:
        if aij in d_al:
            d_al[aij] += 1
        else:
            d_al[aij] = 1

od = 0
a2 = 0

for i in d_al.values():
    if i%2 == 1:
        od += 1
    elif i%4 == 2:
        a2 += 1

if h%2==1 and w%2==1:
    if od == 1 and a2 <= h+w-1:
        print("Yes")
    else:
        print("No")
elif h%2==1:
    if od == 0 and a2 <= w:
        print("Yes")
    else:
        print("No")
elif w%2==1:
    if od == 0 and a2 <= h:
        print("Yes")
    else:
        print("No")
else:
    if od == 0 and a2 == 0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f=o=0
for i in Counter(''.join(t)).values():f+=i//4;o+=i&1
print('YNeos'[o-h%2>h*w%2or f<h//2*w//2+h//4::2])

Submission status: Wrong Answer

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o-h%2>h*w%2or f-h//4<=h*w//4::2])

Submission status: Wrong Answer

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o-h%2>h*w%2or f<=h*w+h>>2::2])

Submission status: Wrong Answer

Solution code:
from collections import*
h,*t=open(0)
h,w=map(int,h.split())
f,o=1,0
for i in Counter(''.join(t[:-1]for t in t)).values():
    f+=i//4
    o+=i&1
print('YNeos'[o>h%2*w%2*(f>(h//2)*(w//2))::2])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=[input() for _ in range(h)]
from collections import defaultdict
d=defaultdict(lambda:0)
for ai in a:
    for aij in ai:
        d[aij]+=1
print(d)
if h%2==0 and w%2==0:
    print('Yes' if all(v%4==0 for v in d.values()) else 'No')
elif h%2==0 and w%2==1:
    c=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            c+=1
        else:
            print('No')
            exit()
    print('Yes' if c<=h//2 else 'No')
elif h%2==1 and w%2==0:
    c=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            c+=1
        else:
            print('No')
            exit()
    print('Yes' if c<=w//2 else 'No')
else:
    ce=0
    co=0
    for v in d.values():
        if v%4==0:
            pass
        elif v%2==0:
            ce+=1
        else:
            co+=1
    print('Yes' if ce<=h//2+w//2 and co==1 else 'No')

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
aij = [input() for i in range(h)]

def moji_num(s):
    return ord(s) - ord('a')

if h % 2 == 0:
    if w % 2 == 0:
        pat = 1
    else:
        pat = 2
else:
    if w % 2 == 0:
        pat = 3
    else:
        pat = 4

moji = [0]*(26)

for i in range(h):
    for j in range(w):
        moji[moji_num(aij[i][j])] += 1
        
#print(moji)

b1 = 0
b2 = 0

for i in range(26):
    if moji[i] % 2 == 1:
        tmp = moji[i]
        if tmp % 4 == 3:
            b2 += 1
        #b1_li.append(moji_num(moji[i]))
        b1 += 1
        #print(i)
    elif moji[i] % 2 == 0 and moji[i] % 4 != 0:
        b2 += 1


#print(pat,b1,b2)        
        
if b1 > 1:
    print('No')
    exit()
elif pat == 1:
    if b2 > 0:
        print('No')
        exit()
elif pat == 2:
    if b2 > h//2:
        print('No')
        exit()
elif pat == 3:
    if b2 > w//2:
        print('No')
        exit()
else:
    if b2 > h+w-2:
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
aij = [input() for i in range(h)]

def moji_num(s):
    return ord(s) - ord('a')

if h % 2 == 0:
    if w % 2 == 0:
        pat = 1
    else:
        pat = 2
else:
    if w % 2 == 0:
        pat = 3
    else:
        pat = 4

moji = [0]*(26)

for i in range(h):
    for j in range(w):
        moji[moji_num(aij[i][j])] += 1
        
#print(moji)

b1 = 0
b2 = 0

b1_li = [0]*(26)
b2_li = [0]*(26)
b4_li = [0]*(26)

for i in range(26):
    if moji[i] % 2 == 1:
        tmp = moji[i]
        b4_li[i] = tmp // 4
        if tmp % 4 == 3:
            b2_li[i] = 1
        b1_li[i] = 1
        #b1_li.append(moji_num(moji[i]))
        b1 += 1
        #print(i)
    elif moji[i] % 2 == 0 and moji[i] % 4 != 0:
        b2_li[i] = 1
        b2 += 1
    elif moji[i] % 4 == 0:
        b4_li[i] = moji[i] // 4


#print(pat,b1,b2)        
        
if b1 > 1:
    print('No')
    exit()
elif pat == 1:
    if b2 > 0:
        print('No')
        exit()
elif pat == 2:
    if b2 > h//2:
        print('No')
        exit()
elif pat == 3:
    if b2 > w//2:
        print('No')
        exit()
else:
    if b2 > h+w-2:
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

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

from collections import Counter, defaultdict

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
c = defaultdict(int)
for row in A:
    for ch in row:
        c[ch] += 1

# group g1, g2, g4
g1 = 1
g2 = H // 2 + W // 2
g4 = (H // 2) * (W // 2)

# print(c)
# print(g1, g2, g4)

for run in range(g1):
    for key in c:
        if c[key] % 4 == 1 or c[key] % 4 == 3:
            c[key] -= 1
            break

for run in range(g2):
    for key in c:
        if c[key] % 4 == 2:
            c[key] -= 2
            break

for run in range(g4):
    for key in c:
        if c[key] % 4 == 0:
            c[key] -= 4
            break

flag = True
for key in c:
    if c[key] == 0:
        continue
    else:
        flag = False
        break

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

Submission status: Wrong Answer

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

from collections import Counter, defaultdict

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
AT = [[None for _ in range(H)] for _ in range(W)]
for h in range(H):
    for w in range(W):
        AT[w][h] = A[h][w]


def is_valid(M):
    # print(len(M), len(M[0]))
    if len(M) == 1 and len(M[0]) == 1:
        return True

    # for row in M: print(*row)

    # 行の個数
    c = defaultdict(int)
    for row in M:
        for ch in row:
            c[ch] += 1

    counter = 0
    for key in c:
        if c[key] % 2 == 0:
            counter += c[key]
    if len(M) % 2 == 0:
        if (len(M[0]) - 1) * len(M) <= counter:
            return True
        else:
            return False
    else:
        if (len(M[0]) - 1) * len(M) - len(M[0]) + 1 <= counter:
            return True
        else:
            return False
    return False


f1 = is_valid(A)
if not f1:
    print("No")
else:
    f2 = is_valid(AT)
    if f2:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int,input().split())
ab = []
for _ in range(h):
    s=list(input())
    c = collections.Counter(s)
    c = sorted(c.items(), key=lambda x:-x[1])
    kisu_count = 0
    for i in c:
        if i[1]%2==1:
            kisu_count += 1
    if w%2==1 and kisu_count == 1 or w%2==0 and kisu_count == 0:
        s.sort()
        s=''.join(map(str,s))
        ab.append(s)
    else:
        print('No')
        exit()

memo = []

count = 1

ab.sort()

for i in range(1,h):
    if ab[i] == ab[i-1]:
        count += 1
    else:
        memo.append(count%2)
        count = 1

memo.append(count%2)

kisu_count2 = 0

for i in memo:
    if i == 1:
        kisu_count2 += 1

if h%2==1 and kisu_count2 == 1 or h%2==0 and kisu_count2 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int,input().split())
ab = []
for _ in range(h):
    s=list(input())
    c = collections.Counter(s)
    c = sorted(c.items(), key=lambda x:-x[1])
    kisu_count = 0
    for i in c:
        if i[1]%2==1:
            kisu_count += 1
    if w%2==1 and kisu_count <= 1 or w%2==0 and kisu_count == 0:
        s.sort()
        s=''.join(map(str,s))
        ab.append(s)
    else:
        print('No')
        exit()

memo = []

count = 1

ab.sort()

for i in range(1,h):
    if ab[i] == ab[i-1]:
        count += 1
    else:
        memo.append(count%2)
        count = 1

memo.append(count%2)

kisu_count2 = 0

for i in memo:
    if i == 1:
        kisu_count2 += 1

if h%2==1 and kisu_count2 == 1 or h%2==0 and kisu_count2 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int,input().split())
ab = []
for _ in range(h):
    s=list(input())
    c = collections.Counter(s)
    c = sorted(c.items(), key=lambda x:-x[1])
    kisu_count = 0
    for i in c:
        if i[1]%2==1:
            kisu_count += 1
    if w%2==1 and kisu_count <= 1 or w%2==0 and kisu_count == 0:
        s.sort()
        s=''.join(map(str,s))
        ab.append(s)
    else:
        print('No')
        exit()

memo = []

count = 1

ab.sort()

for i in range(1,h):
    if ab[i] == ab[i-1]:
        count += 1
    else:
        memo.append(count%2)
        count = 1
memo.append(count%2)

kisu_count2 = 0

for i in memo:
    if i == 1:
        kisu_count2 += 1

if h%2==1 and kisu_count2 <= 1 or h%2==0 and kisu_count2 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter
H,W = [int(x) for x in stdin.readline().rstrip().split()]
c = Counter()

for i in range(H):
    c += Counter(list(input()))

rem0 = 0
rem2 = 0

for i in c.values():
    if i % 4 == 0:
        rem0 += 1
    elif i % 4 == 2:
        rem2 += 1

num_v = len(c.values())

f = True
if H % 2 == 0 and W % 2 == 0:
    if 4*rem0 != H*W:
        f = False

elif H % 2 != 0 and W % 2 != 0:
    
    if len([i for i in c.values() if i % 2 != 0]) != 1:
        f = False
        
    if rem2 > (H-1)/2 + (W-1)/2:
        f = False
    
    if num_v-1 != rem0 + rem2:
        f = False
    
else:
    if H % 2 == 0:
        if rem2 > H/2:
            f = False
        if rem2 + rem0 != num_v:
            f = False
            
    if W % 2 == 0:
        if rem2 > W/2:
            f = False
        if rem2 + rem0 != num_v:
            f = False
    
if f:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
def solve():
    inputs = open(0).readlines()
    h, w = map(int, inputs[0].split())
    *A, = map(lambda x: x.strip(), inputs[1:])
    B = []
    for S in A:
        C = [0] * 26
        for c in S:
            C[ord(c)-ord('a')] += 1
        B.append(C)
    
    if w % 2 == 0:
        if all(all(x % 2 == 0 for x in b) for b in B):
            C = []
        else:
            return 'No'
    else:
        C = [[i for i, x in enumerate(b) if x % 2 != 0] for b in B]
        if all(len(c) == 1 for c in C):
            B = [[x if x % 2 == 0 else x-1 for x in b] for b in B]
            C = Counter([c[0] for c in C]).values()
        else:
            return 'No'
    C
    D = Counter(map(tuple, B)).values()
    if h % 2 == 0:
        if all(c % 2 == 0 for c in C) and all(d % 2 == 0 for d in D):
            return 'Yes'
    else:
        if sum(c % 2 for c in C) <= 1 and sum(d % 2 for d in D) <= 1:
            return 'Yes'
    return 'No'

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import Counter
import math

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

c=Counter(sum(a,[]))

if h%2==0 and w%2==0:
    g1=0
    g2=0
    g4=(h//2)*(w//2)
elif h%2==1 and w%2==0:
    g1=0
    g2=w//2
    g4=(h//2)*(w//2)
elif h%2==0 and w%2==1:
    g1=0
    g2=h//2
    g4=(h//2)*(w//2)
else:
    g1=1
    g2=(h//2)+(w//2)
    g4=(h//2)*(w//2)

g1_cnt=0
g2_cnt=0
g4_cnt=0

for key in c:
    if c[key]%4==1:
        g1_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g2_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g1_cnt+=1
        g2_cnt+=1
        g4_cnt+=c[key]//4
    else:
        g4_cnt+=c[key]//4

# print(g1,g2,g4)

# print(g1_cnt,g2_cnt,g4_cnt)

flag=False
if g1==g1_cnt:
    if g2==g2_cnt and g4==g4_cnt:
        flag=True
    elif g4_cnt>g2_cnt and g2*2+g4*4==g2_cnt*2+g4_cnt*4:
        flag=True

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
import math

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

c=Counter(sum(a,[]))

if h%2==0 and w%2==0:
    g1=0
    g2=0
    g4=(h//2)*(w//2)
elif h%2==1 and w%2==0:
    g1=0
    g2=w//2
    g4=(h//2)*(w//2)
elif h%2==0 and w%2==1:
    g1=0
    g2=h//2
    g4=(h//2)*(w//2)
else:
    g1=1
    g2=(h//2)+(w//2)
    g4=(h//2)*(w//2)

g1_cnt=0
g2_cnt=0
g4_cnt=0

for key in c:
    if c[key]%4==1:
        g1_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g2_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g1_cnt+=1
        g2_cnt+=1
        g4_cnt+=c[key]//4
    else:
        g4_cnt+=c[key]//4

# print(g1,g2,g4)

# print(g1_cnt,g2_cnt,g4_cnt)

flag=False
if g1==g1_cnt:
    if g2==g2_cnt and g4==g4_cnt:
        flag=True
    elif g4_cnt>=g2_cnt and g2*2+g4*4==g2_cnt*2+g4_cnt*4:
        flag=True

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
import math

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

c=Counter(sum(a,[]))

if h%2==0 and w%2==0:
    g1=0
    g2=0
    g4=(h//2)*(w//2)
elif h%2==1 and w%2==0:
    g1=0
    g2=w//2
    g4=(h//2)*(w//2)
elif h%2==0 and w%2==1:
    g1=0
    g2=h//2
    g4=(h//2)*(w//2)
else:
    g1=1
    g2=(h//2)+(w//2)
    g4=(h//2)*(w//2)


# print(c)

g1_cnt=0
g2_cnt=0
g4_cnt=0

for key in c:
    if c[key]%4==1:
        g1_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g2_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g1_cnt+=1
        g2_cnt+=1
        g4_cnt+=c[key]//4
    else:
        g4_cnt+=c[key]//4

# print(g1,g2,g4)

# print(g1_cnt,g2_cnt,g4_cnt)

print('Yes' if g1==g1_cnt and g2==g2_cnt and g4==g4_cnt else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
import math

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

c=Counter(sum(a,[]))

if h%2==0 and w%2==0:
    g1=0
    g2=0
    g4=(h//2)*(w//2)
elif h%2==1 and w%2==0:
    g1=0
    g2=w//2
    g4=(h//2)*(w//2)
elif h%2==0 and w%2==1:
    g1=0
    g2=h//2
    g4=(h//2)*(w//2)
else:
    g1=1
    g2=(h//2)+(w//2)
    g4=(h//2)*(w//2)


print(c)

g1_cnt=0
g2_cnt=0
g4_cnt=0

for key in c:
    if c[key]%4==1:
        g1_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g2_cnt+=1
        g4_cnt+=c[key]//4
    elif c[key]%4==2:
        g1_cnt+=1
        g2_cnt+=1
        g4_cnt+=c[key]//4
    else:
        g4_cnt+=c[key]//4

# print(g1,g2,g4)

# print(g1_cnt,g2_cnt,g4_cnt)

print('Yes' if g1==g1_cnt and g2==g2_cnt and g4==g4_cnt else 'No')

Submission status: Wrong Answer

Solution code:
import sys
from string import ascii_lowercase as AL

input = sys.stdin.readline


def main():
    H, W = map(int, input().split())

    alpha = {}
    for al in AL:
        alpha[al] = 0
    for i in range(H):
        a = input().rstrip()
        for s in a:
            alpha[s] += 1

    values = list(alpha.values())

    q_h, r_h, = divmod(H, 2)
    q_w, r_w, = divmod(W, 2)
    g_4 = q_h * q_w
    g_2 = q_h * r_h + q_w * r_w
    g_1 = r_h * r_w

    for i in range(len(values)):
        while g_4 > 0:
            if values[i] >= 4:
                g_4 -= 1
                values[i] -= 4
            else:
                break
    for i in range(len(values)):
        while g_2 > 0:
            if values[i] >= 2:
                g_2 -= 1
                values[i] -= 2
            else:
                break
    for i in range(len(values)):
        while g_1 > 0:
            if values[i] == 1:
                g_1 -= 1
                values[i] = 0
            else:
                break

    if g_4 == g_2 == g_1 == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from string import ascii_lowercase as AL

input = sys.stdin.readline


def main():
    H, W = map(int, input().split())

    alpha = {}
    for al in AL:
        alpha[al] = 0
    for i in range(H):
        a = input().rstrip()
        for s in a:
            alpha[s] += 1

    values = list(alpha.values())

    q_h, r_h, = divmod(H, 2)
    q_w, r_w, = divmod(W, 2)
    g_4 = q_h * q_w
    g_2 = q_h * r_h + q_w * r_w
    g_1 = r_h * r_w

    for i in range(len(values)):
        while g_4 > 0:
            if values[i] >= 4:
                g_4 -= 1
                values[i] -= 4
            else:
                break
    for i in range(len(values)):
        while g_2 > 0:
            if values[i] >= 2:
                g_2 -= 1
                values[i] -= 2
            else:
                break
    for i in range(len(values)):
        if values[i] == 1:
            g_1 -= 1
            values[i] = 0

    if g_4 == g_2 == g_1 == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())

a=[]
for _ in range(h):
    a+=list(input())

four=(h//2)*(w//2)
two=h%2*w//2+w%2*h//2
one=(h%2)*(w%2)

import collections
c = collections.Counter(a)

#print(four,two,one)
d=list(c.values())
d.sort()

for item in d:
    if item==1:
        if one==1:
            one-=1
        else:
            print("No")
            exit()
    elif item==2:
        if two>0:
            two-=1
        else:
            print("No")
            exit()
    elif item==3:
        if one==1 and two>0:
            one-=1
            two-=1
        else:
            print("No")
            exit()
    else:
        if item%2==1:
            if one==0:
                print("No")
                exit()
            else:
                item-=1
                one-=1
        while item>=4 and four>=0:
            item-=4
            four-=1
        
        while item>=2:
            item-=2
            two-=1
    
if one==0 and two==0 and four==0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

if h % 2 == 0 and w % 2 == 0:
    for k , v in c.items():
        if v % 4 != 0:
            print("No")
            exit()
    print("Yes")
    
elif h % 2 != 0 and w % 2 != 0:
    m = 0
    n = 0
    for k , v in c.items():
        if v % 4 == 1:
            n += 1
            if n > 1:
                print("No")
                exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2 + h//2:
                print("No")
                exit()
        elif v % 4 == 3:
            n += 1
            m += 1
            if n > 1:
                print("No")
                exit()
            if m > w//2 + h//2:
                print("No")
                exit()
        
    print("Yes")
elif h % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1 or v % 4 == 3:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > h//2 + 1:
                print("No")
                exit()
    print("Yes")
elif w % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1 or v % 4 == 3:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2 + 1:
                print("No")
                exit()
    print("Yes")

Submission status: Wrong Answer

Solution code:
import collections
h , w = map(int,input().split())
p = []
for i in range(h):
    p.extend(list(input()))
c = collections.Counter(p)
if h % 2 == 0 and w % 2 == 0:
    for k , v in c.items():
        if v % 4 != 0:
            print("No")
            exit()
    print("Yes")
    
elif h % 2 != 0 and w % 2 != 0:
    m = 0
    n = 0
    for k , v in c.items():
        if v % 4 == 1:
            n += 1
            if n > 1:
                print("No")
                exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2 + h//2:
                print("No")
                exit()
    print("Yes")
elif h % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > h//2 + 1:
                print("No")
                exit()
    print("Yes")
elif w % 2 != 0:
    m = 0
    for k , v in c.items():
        if v % 4 == 1:
            print("No")
            exit()
        elif v % 4 == 2:
            m += 1
            if m > w//2 + 1:
                print("No")
                exit()
    print("Yes")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len(list((set(D))))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = (H*W-(H//2)*(W//2)*4)//2+1
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m <= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        seigen = (H*W)//4
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s <= seigen)and(q == 0)):
            print("Yes")
        else:
            print("No")

    else:
        q = 0
        e = 0
        if ((H%2) == 1):
            seigen = (H//2+1)*W
            seigen2 = (H//2+1)
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
            seigen2 = (W//2+1)
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                e += 1
        if (q == 0)and(s<=seigen)and(e<=seigen2):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = (H*W-(H//2)*(W//2)*4)//2+1
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m <= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")

    else:
        q = 0
        e = 0
        if ((H%2) == 1):
            seigen = (H//2+1)*W
            seigen2 = (H//2+1)
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
            seigen2 = (W//2+1)
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                e += 1
        if (q == 0)and(s<=seigen)and(e<=seigen2):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = (H//2)*(W//2)
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)== 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m >= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")

    else:
        q = 0
        e = 0
        if ((H%2) == 1):
            seigen = (H//2+1)*W
            seigen2 = (H//2+1)
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
            seigen2 = (W//2+1)
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)!= 0:
                e += 1
        if (q == 0)and(s<=seigen)and(e<=seigen2):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = (H//2)*(W//2)
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)== 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m >= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")

    else:
        q = 0

        if ((H%2) == 1):
            seigen = (H//2+1)*W
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
        for k in range(s):
            q += (Q[k] % 2)
        if (q == 0)and(s<=seigen):
            print("Yes")
        else:
            print("No")