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())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
List=[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1:
      print("Yes")
    else:
      print("No")
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
List=[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1:
      print("Yes")
    else:
      print("No")
  else:
    print("No")

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:
  cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
  if cnt1!=1:print("No")
  elif 2*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%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==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==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
  if cnt1!=1:print("No")
  elif 2*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
print(a)
if 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==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==2:cnt+=1
  if cnt>h//2:
    print("No")
  else:print("Yes")
else:
  cnt2=0;cnt1=0
  for i in a.values():
    if i%4==1:cnt1+=1
    elif i%4==2:cnt2+=1
  if cnt1!=1:print("No")
  elif 2*cnt2>h+w-2:print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

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.append(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: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h==1 and w==1:
    print("No")
    exit()
from collections import Counter
a=[]
for _ in range(h):
    a+=list(input())
c=Counter(a)
four=1 if h>1 and w>1 else 0
four+=(w-2)//2
four+=(h-2)//2
two=0
two+=(w-2)%2
two+=(h-2)%2
for k,v in c.items():
    four-=v//4
    c[k]-=v//4
    two-=c[k]//2
    c[k]-=c[k]//2
if four==0 and two==0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import collections
h,w=map(int,input().split())
a=''
for i in range(h):
    a+=str(input())
l=list(a)
r=collections.Counter(l)
rr=list(r.values())
R=[]
x=0;y=0;z=0
for i in rr:
    R.append(i%4)
    if i%4==1 or i%4==3:
        x+=1
    elif i%4==2:
        y+=1
    else:
        z+=1

if h%2==0 and w%2==0:
    if x==0 and y==0:
        print('Yes')
    else:
        print('No')
elif h%2==1 and w%2==1:
    if min(h,w)!=1:
        if x<=1 and y<=(h-1)*(w-1)//2:
            print('Yes')
        else:
            print('No')
    else:
        if x<=1 and y<=max(h,w)//2:
            print('Yes')
        else:
            print('No')
else:
    if h%2==0:
        if x==0 and y<=h//2:
            print('Yes')
        else:
            print('No')
    else:
        if x==0 and y<=w//2:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import collections
h,w=map(int,input().split())
a=''
for i in range(h):
    a+=str(input())
l=list(a)
r=collections.Counter(l)
rr=list(r.values())
if h%2==0 and w%2==0:
    for i in rr:
        if i%4!=0:
            print('No')
            break
    else:
        print('Yes')
elif h%2==1 and w%2==1:
    z=0
    for i in rr:
        if i%4!=0:
            z+=1
    if z<=3:print('Yes')
    else:print('No')
else:
    z=0
    for i in rr:
        if i%4==1 or i%4==3:
            z=100
        if i%4==2:
            z+=1
    if z<=2:print('Yes')
    else:print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h, w = map(int, input().split())
s = ''
for i in range(h):
    s += input()
c = list(Counter(s).values())
for i in range(len(c)):
    c[i] %= 2
a = c.count(1)
if h % 2 and w % 2 and a == 1:
    print('Yes')
elif a == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    h, w = map(int, input().split())
    alpha = [0 for _ in range(26)]

    # 各アルファベットの個数を数える
    for i in range(h):
        ai = list(input())

        for aii in ai:
            # アルファベットを数値に変換
            index = ord(aii) - ord('a')
            alpha[index] += 1

    # 回文となる条件
    # 1. hとwがともに偶数：
    # アルファベットの個数が全て4の倍数のとき，Yes
    # 2. hとwのどちらかが偶数：
    # アルファベットの個数が全て2の倍数のとき，Yes
    # 3. hとwがともに奇数：
    # (h - 1) * (w - 1)マス分，アルファベットの個数が4の倍数であり，
    # かつ，1種類を除いて，2の倍数のとき，Yes
    if h % 2 == 0 and w % 2 == 0:
        for a in alpha:
            if a % 4 != 0:
                print('No')
                exit()

        print('Yes')
    elif h % 2 == 0 or w % 2 == 0:
        for a in alpha:
            if a % 2 != 0:
                print('No')
                exit()

        print('Yes')
    else:
        # 気がつけなかった点
        # 4で割ったときに2余る数が使われるのが，(h - 1) // 2 + (w - 1) // 2以下である
        mod = [0 for _ in range(4)]

        for a in alpha:
            mod[a % 4] += 1

        if mod[3]:
            print('No')
        elif mod[1] != 1:
            print('No')
        elif mod[2] > ((h - 1) // 2 + (w - 1) // 2):
            print('No')
        else:
            print('Yes')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    h, w = map(int, input().split())
    alpha = [0 for _ in range(26)]

    # 各アルファベットの個数を数える
    for i in range(h):
        ai = list(input())

        for aii in ai:
            # アルファベットを数値に変換
            index = ord(aii) - ord('a')
            alpha[index] += 1

    # 回文となる条件
    # 1. hとwがともに偶数：
    # アルファベットの個数が全て4の倍数のとき，Yes
    # 2. hとwのどちらかが偶数：
    # アルファベットの個数が全て2の倍数のとき，Yes
    # 3. hとwがともに奇数：
    # (h - 1) * (w - 1)マス分，アルファベットの個数が4の倍数であり，
    # かつ，1種類を除いて，2の倍数のとき，Yes
    if h % 2 == 0 and w % 2 == 0:
        for a in alpha:
            if a % 4 != 0:
                print('No')
                exit()

        print('Yes')
    elif h % 2 == 0 or w % 2 == 0:
        for a in alpha:
            if a % 2 != 0:
                print('No')
                exit()

        print('Yes')
    else:
        need = (h - 1) * (w - 1)

        for index, a in enumerate(alpha):
            if need <= 3:
                break

            p, q = divmod(a, 4)
            alpha[index] -= min(p * 4, need)
            need -= min(p * 4, need)

        count = 0

        for a in alpha:
            if a % 2 == 1:
                count += 1

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    h, w = map(int, input().split())
    alpha = [0 for _ in range(26)]

    # 各アルファベットの個数を数える
    for i in range(h):
        ai = list(input())

        for aii in ai:
            # アルファベットを数値に変換
            index = ord(aii) - ord('a')
            alpha[index] += 1

    # 回文となる条件
    # 1. hとwがともに偶数：
    # アルファベットの個数が全て4の倍数のとき，Yes
    # 2. hとwのどちらかが偶数：
    # アルファベットの個数が全て2の倍数のとき，Yes
    # 3. hとwがともに奇数：
    # (h - 1) * (w - 1)マス分，アルファベットの個数が4の倍数であり，
    # かつ，1種類を除いて，2の倍数のとき，Yes
    if h % 2 == 0 and w % 2 == 0:
        for a in alpha:
            if a % 4 != 0:
                print('No')
                exit()

        print('Yes')
    elif h % 2 == 0 or w % 2 == 0:
        for a in alpha:
            if a % 2 != 0:
                print('No')
                exit()

        print('Yes')
    else:
        need = (h - 1) * (w - 1)

        for index, a in enumerate(alpha):
            p, q = divmod(a, 4)
            alpha[index] -= min(p * 4, need)
            need -= min(p * 4, need)

            if need == 0:
                break

        count = 0

        for a in alpha:
            if a % 2 == 1:
                count += 1

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
mod1 = 1 if oe == 2 else 0
mod2 = (H//2)*(W%2)+(W//2)*(H%2)
mod4 = (H//2)*(W//2)
for _ in range(mod1):
  for c in counts.keys():
    if counts[c] % 4 == 1:
      counts[c] -= 1
      break
for _ in range(mod2):
  for c in counts.keys():
    if counts[c] % 4 == 2:
      counts[c] -= 2
      mod2 -= 1
      break
if mod2 % 2 == 0:
  mod4 += mod2//2
  for _ in range(mod4):
    for c in counts.keys():
      if counts[c] % 4 == 0:
      	counts[c] -= 4
      	break
  for n in counts.values():
    if n > 0:
      ans = "No"
      break
else:
  ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
if oe == 0:
  for n in counts.values():
    if n % 4 != 0:
      ans = "No"
      break
elif oe == 1:
  even = H if H % 2 == 0 else W
  modis2 = 0
  for n in counts.values():
    if n % 4 == 2:
      modis2 += 1
      if modis2 > even / 2:
        ans = "No"
        break
    elif n % 4 != 0:
      ans = "No"
      break
else:
  modcount = [0] * 4
  for n in counts.values():
    modcount[n%4] += 1
  if modcount[1]+modcount[3] == 1 and modcount[2] + modcount[3] == (H//2)+(W//2):
    ans = "Yes"
  else:
    ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
num1 = 1 if oe == 2 else 0
num2 = ((H//2)*(W%2))+((W//2)*(H%2))
num4 = (H//2)*(W//2)
for _ in range(num1):
  for c in counts.keys():
    if counts[c] % 4 in (1, 3):
      counts[c] -= 1
      break
for _ in range(num2):
  for c in counts.keys():
    if counts[c] % 4 == 2:
      counts[c] -= 2
      break
x = 0
for _ in range(num4):
  for c in counts.keys():
    if counts[c] % 4 == 0 and counts[c] > 0:
      counts[c] -= 4
      x += 1
      break
ans = "Yes"
for n in counts.values():
  if n != 0:
    ans = "No"
if x < num4:
  ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
num1 = 1 if oe == 2 else 0
num2 = ((H//2)*(W%2))+((W//2)*(H%2))
num4 = (H//2)*(W//2)
for _ in range(num1):
  for c in counts.keys():
    if counts[c] % 4 in (1, 3):
      counts[c] -= 1
      break
for _ in range(num2):
  for c in counts.keys():
    if counts[c] % 4 == 2:
      counts[c] -= 2
      break
for _ in range(num4):
  for c in counts.keys():
    if counts[c] % 4 == 0 and counts[c] > 0:
      counts[c] -= 4
      break
ans = "Yes"
for n in counts.values():
  if n != 0:
    ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
num1 = 1 if oe == 2 else 0
num2 = ((H//2)*(W%2))+((W//2)*(H%2))
num4 = (H//2)*(W//2)
for _ in range(num1):
  for c in counts.keys():
    if counts[c] % 4 in (1, 3):
      counts[c] -= 1
for _ in range(num2):
  for c in counts.keys():
    if counts[c] % 4 == 2:
      counts[c] -= 2
for _ in range(num4):
  for c in counts.keys():
    if counts[c] % 4 == 0 and counts[c] > 0:
      counts[c] -= 4  
ans = "Yes"
for n in counts.values():
  if n != 0:
    ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
counts = {}
for a_ in A:
  for a in a_:
    if not a in counts:
      counts[a] = 1
    else:
      counts[a] += 1
ans = "Yes"
oe = H%2+W%2
if oe == 0:
  for n in counts.values():
    if n % 4 != 0:
      ans = "No"
      break
elif oe == 1:
  even = H if H % 2 == 0 else W
  modis2 = 0
  for n in counts.values():
    if n % 4 == 2:
      modis2 += 1
      if modis2 > even / 2:
        ans = "No"
        break
    elif n % 4 != 0:
      ans = "No"
      break
else:
  if H == 1 or W == 1:
    oddcount = 0
    for n in counts.values():
      if n % 2 == 1:
        oddcount += 1
        if oddcount > 2:
          ans = "No"
          break
  else:
    modcount = [0] * 4
    for n in counts.values():
      modcount[n%4] += 1
    if modcount[1]+modcount[3] == 1 and modcount[2] + modcount[3] == H+W-1:
      ans = "Yes"
    else:
      ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import defaultdict, Counter

def main():
    h, w = map(int, input().strip().split())
    mat = [0] * h
    d = {}
    for i in range(h):
        s = input().strip()
        for k, v in dict(Counter(s)).items():
            if k in d:
                d[k] += v
            else:
                d[k] = v
    num = [0] * 4
    for k, v in d.items():
        num[v % 4] += 1

    ans = "No"
    if h % 2 == 1 and w % 2 == 1:
        if num[1] + num[3] == 1 and num[2] <= h // 2 + w // 2:
            ans = "Yes"
    elif h % 2 == 1:
        if num[1] + num[3] == 0 and num[2] <= max(w, h) // 2:
            ans = "Yes"
    elif w % 2 == 1:
        if num[1] + num[3] == 0 and num[2] <= max(w, h) // 2:
            ans = "Yes"
    else:
        if num[1] + num[3] + num[2] == 0:
            ans = 'Yes'
    print(ans)
    # print(num)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
dct=defaultdict(int)
dct2=defaultdict(int)

h,w=map(int,input().split())
for i in range(h):
  s=input()
  for j in range(w):
    dct[s[j]]+=1


for k,v in dct.items():
  dct2[4]+=v//4
  dct2[2]+=v//2-2*(v//4)
  dct2[1]+=v%2


f=0

if h%2==0 and w%2==0:
  if dct2[1]==dct2[2]==0:
    f=1
elif h%2==1 and w%2==1:
  if dct2[1]==1 and dct2[2]==((h-1)+(w-1))//2 and dct2[4]==((h-1)*(w-1))//4:
    f=1
elif h%2==1:
  if dct2[1]==0 and dct2[2]==w//2 and dct2[4]==((h-1)*(w))//4:
    f=1
else :
  if dct2[1]==0 and dct2[2]==h//2 and dct2[4]==((w-1)*(h))//4:
    f=1

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import defaultdict, Counter

def fail():
    print('No')
    sys.exit(0)

def suc():
    print('Yes')
    sys.exit(0)

def main():
    h, w = map(int, input().strip().split())
    mat = [0] * h
    d = {}
    for i in range(h):
        s = input().strip()
        for k, v in dict(Counter(s)).items():
            if k in d:
                d[k] += v
            else:
                d[k] = v
    if ((h+1) // 2) * ((w+1) // 2) < len(d):
        fail()
    if len(d) == 1:
        suc()
    if h % 2 == 1 and w % 2 == 1:
        c = 0
        for v in d.values():
            if v % 2 == 1:
                c += 1
        if c == 1:
            suc()
        else:
            fail()
    else:
        for v in d.values():
            if v % 2 != 0:
                print('No')
                return
        print('Yes')
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import defaultdict, Counter

def fail():
    print('No')
    sys.exit(0)

def suc():
    print('Yes')
    sys.exit(0)

def main():
    h, w = map(int, input().strip().split())
    mat = [0] * h
    d = {}
    for i in range(h):
        s = input().strip()
        for k, v in dict(Counter(s)).items():
            if k in d:
                d[k] += v
            else:
                d[k] = v
    if ((h+1) // 2) * ((w+1) // 2) < len(d):
        fail()
    if h % 2 == 1 and w % 2 == 1:
        c = 0
        for v in d.values():
            if v % 2 == 1:
                c += 1
        if c == 1:
            suc()
        else:
            fail()
    else:
        for v in d.values():
            if v % 2 != 0:
                print('No')
                return
        print('Yes')
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W = [int(x) for x in input().split()]
A = [input() for _ in range(H)]
char_cnts = [0]*26
for a in A:
    for c in a:
        char_cnts[ord(c)-97] += 1
if(H==2 or W==2):
    for i in char_cnts:
        if(i%2!=0):
            print('No')
            break
    else:
        print('Yes')
else:
    cnt = (H//2)*(W//2)
    for i in char_cnts:
        cnt -= i//4
        if(cnt<=0):
            print('Yes')
            break
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import collections, itertools

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

b = [[None] * w for _ in range(h)]
count = 0
for x in range(h):
    for y in range(w):
        if b[x][y] is not None:
            continue
        b[x][y] = count
        b[h-x-1][y] = count
        b[x][w-y-1] = count
        b[h-x-1][w-y-1] = count
        count += 1

d1 = collections.Counter(a[x][y] for x, y in itertools.product(range(h), range(w)))
d2 = collections.Counter(b[x][y] for x, y in itertools.product(range(h), range(w)))
for k2, v2 in d2.items():
    ok = False
    for k1, v1 in d1.items():
        if v2 <= v1:
            d1[k1] -= v2
            ok = True
            break
    if not ok:
        print('No')
        exit(0)

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

h,w = map(int, input().split())
e_mod4 = (h//2) * (w//2)
e_mod2 = (h%2) * w//2 + (w%2) * h//2
e_mod1 = (h%2) * (w%2)

ca = Counter([j for i in range(h) for j in list(input())])

mod4, mod2, mod1 = 0, 0, 0
for i in ca.values():
     mod4 += i // 4
     i %= 4
     mod2 += i // 2
     i %= 2
     mod1 += i

print(e_mod4,e_mod2,e_mod1)
print(mod4, mod2)

if mod4 > e_mod4:
    mod2 += (mod4 - e_mod4) * 2
    mod4 = e_mod4

print("Yes" if e_mod4 == mod4 and e_mod2 == mod2 and e_mod1 == mod1 else "No")

Submission status: Wrong Answer

Solution code:
from collections import Counter

h,w = map(int, input().split())
e_mod4 = (h//2) * (w//2)
e_mod2 = (h%2) * w//2 + (w%2) * h//2
e_mod1 = (h%2) * (w%2)

ca = Counter([j for i in range(h) for j in list(input())])

mod4, mod2, mod1 = 0, 0, 0
for i in ca.values():
     mod4 += i // 4
     i %= 4
     mod2 += i // 2
     i %= 2
     mod1 += i

if mod4 > e_mod4:
    e_mod2 += (mod4 - e_mod4) * 2

print("Yes" if e_mod4 == mod4 and e_mod2 == mod2 and e_mod1 == mod1 else "No")

Submission status: Wrong Answer

Solution code:
from collections import Counter

h,w = map(int, input().split())
e_mod4 = (h//2) * (w//2)
e_mod2 = (h%2) * w//2 + (w%2) * h//2
e_mod1 = (h%2) * (w%2)

ca = Counter([j for i in range(h) for j in list(input())])

mod4, mod2, mod1 = 0, 0, 0
for i in ca.values():
     mod4 += i // 4
     i %= 4
     mod2 += i // 2
     i %= 2
     mod1 += i

if mod4 > e_mod4:
    mod2 += (mod4 - e_mod4) * 2

print("Yes" if e_mod4 == mod4 and e_mod2 == mod2 and e_mod1 == mod1 else "No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import ceil

h,w = map(int, input().split())
e_mod4 = (h//2) * (w//2)
e_mod2 = (h%2) * w//2 + (w%2) * h//2
e_mod1 = (h%2) * (w%2)

ca = Counter([j for i in range(h) for j in list(input())])
mod4, mod2, mod1 = 0, 0, 0

for i in ca.values():
     mod4 += i // 4
     i %= 4
     mod2 += i // 2
     i %= 2
     mod1 += i

mod2 += (mod4 - e_mod4) * 2

print("Yes" if e_mod2 == mod2 and e_mod1 == mod1 else "No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H,W=map(int,input().split())
A="".join([input() for _ in range(H)])
cnts = defaultdict(lambda: 0)
for c in A:
    cnts[c]+=1
    
n4 = ((H-H%2)*(W-W%2))//4
n2 = (H%2)*(W//2)+(W%2)*(H//2)
n1 = 1 if H%2+W%2 == 2 else 0

for _,cnt in cnts.items():
    n4 -= (cnt//4)
    n2 -= (cnt%4)//2
    n1 -= (cnt%2)
print("Yes" if n4==0 and n2==0 and n1==0 else "No")