Problem:


Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd !=1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd !=1:
            ans="No"
        else:
            s=d[odd_list[0]]
            u = s//4
            v = (s-u)//2
            dw=abs(H-W)
            
            if 2*v+2*even_2 >2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            gu = H if H%2==0 else W
            if 2*even_2>gu:
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd !=1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd !=1:
            ans="No"
        else:
            s=d[odd_list[0]]
            t=s-1
            if t!=2*even_2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            gu = H if H%2==0 else W
            if 2*even_2>gu:
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd !=1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd !=1:
            ans="No"
        else:
            s=d[odd_list[0]]
            if s+2*even_2 > H+W-1:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            gu = H if H%2==0 else W
            if 2*even_2>gu:
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd !=1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd !=1:
            ans="No"
        else:
            s=d[odd_list[0]]
            t=s-1
            if t!= 2*even_2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            gu = H if H%2==0 else W
            if 2*even_2>gu:
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd > 1 or odd==0:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd >1 or odd==0:
            ans="No"
        else:
            s=d[odd_list[0]]
            t=s-1
            if t!= 2*even_2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            if 2*even_2>max(H,W):
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd > 1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd >1:
            ans="No"
        else:
            s=d[odd_list[0]]
            t=s-1
            if t!= 2*even_2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            if 2*even_2>max(H,W):
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
a=[list(input()) for _ in range(H)]

tmp=[]
for j in a:
  for k in j:
    tmp.append(k)
d=dict(Counter(tmp))
#print(d)

odd=0
even_4=0
even_2=0
odd_list=[]

for char in d:
    if d[char]%2==1:
        odd +=1
        odd_list.append(char)
    else:
        if d[char]%4==0:
            even_4 += 1
        else:
            even_2 += 1
#print(odd,even_4,even_2)


ans="Yes"
if H==1 or W==1:
    m = max(H,W)
    if m%2==1:
        if odd > 1:
            ans="No"
    else:
        if odd >= 1:
            ans="No"
    
else:
    if H%2==0 and W%2==0:
        if odd>0 or even_2>0:
            ans="No"
    elif H%2==1 and W%2==1:
        if odd >1:
            ans="No"
        else:
            s=d[odd_list[0]]
            t=s-1
            if t!= 2*even_2:
                ans="No"
    else:
        if odd >=1:
            ans="No"
        else:
            if even_2>max(H,W):
                ans="No"

print(ans)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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

# a〜z -- START --
alf=['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']
# a〜z --- END ---

def main():
  h,w=LI()
  l=[0]*len(alf)

  for _ in range(h):
    a=S()
    for x in a:
      l[alf.index(x)]+=1

  count4=count2=count1=0
  for i in range(len(alf)):
    count4+=l[i]//4
    l[i]%=4
    count2+=l[i]//2
    l[i]%=2
    count1+=l[i]

  # print(count1,count2,count4)

  if h%2==1 and w%2==1:
    if count1!=1:
      return 'No'

    if count2!=w//2+h//2:
      return 'No'

    return 'Yes'
  elif h%2==1 and w%2==0:
    if count1!=0:
      return 'No'

    if count2!=w//2:
      return 'No'

    return 'Yes'
  elif h%2==0 and w%2==1:
    if count1!=0:
      return 'No'

    if count2!=h//2:
      return 'No'

    return 'Yes'
  elif h%2==0 and w%2==0:
    if count1!=0:
      return 'No'

    if count2!=0:
      return 'No'

    return 'Yes'


# main()
print(main())

Submission status: Wrong Answer

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

cnt = [0]*(ord('z')-ord('a')+1)
for i in range(h):
    for j in range(w):
        cnt[ord(a[i][j])-ord('a')] += 1

f_num = 0
t_num = 0
for i in cnt:
    while i >= 4:
        i -= 4
        f_num += 1
    while i >= 2:
        i -=2
        t_num += 1

req_f = (h//2)*(w//2)
req_t = (h%2)*(w//2) + (w%2)*(h//2)

if req_f > f_num:
    print('No')
    exit()
f_num -= req_f
t_num += f_num//2
if req_t > t_num:
    print("No")
    exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

H,W = LI()
a  = [list(map(str,input())) for _ in range(H)]

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[a[i][j]] += 1

if H%2 == 0 and W%2 == 0:
    for k in d.keys():
        if d[k]%4 != 0:
            print('No')
            exit()
    print('Yes')
elif H%2 == 0 and W%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (H//2)%2:
        print('Yes')
    else:
        print('No')
elif W%2 == 0 and H%2 != 0:
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                print('No')
                exit()
            else:
                count += 1
    if count%2 == (W//2)%2:
        print('Yes')
    else:
        print('No')
else:
    odd_flag = False
    count = 0
    for k in d.keys():
        if d[k]%4 != 0:
            if d[k]%2 != 0:
                if not odd_flag:
                    odd_flag = True
                else:
                    print('No')
                    exit()
            else:
                count += 1
    if count%2 == ((H-1)//2+(W-1)//2)%2:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import sys
H,W=map(int,input().split())
a = [[c for c in l.strip()] for l in sys.stdin]
#print(a)
dic={}
for i in range(H):
  for j in range(W):
    if(a[i][j] not in dic):
      dic[a[i][j]]=1
    else:
      dic[a[i][j]]+=1
#print(dic)
flag=True
if(H%2==0 and W%2==0):
  for i in dic.keys():
    if(dic[i]%4!=0):
      flag=False
elif((H%2==1 and W%2==0)):
  count=0
  for i in dic.keys():
    if(dic[i]%2!=0):
      flag=False
    if(dic[i]%2==2):
      count+=1
  if(count>W//2):
    flag=False
elif((H%2==0 and W%2==1)): 
  count=0
  for i in dic.keys():
    if(dic[i]%2!=0):
      flag=False
    if(dic[i]%2==2):
      count+=1
  if(count>H//2):
    flag=False
else:
  count2=0
  count1=0
  for i in dic.keys():
    if(dic[i]%4==1):
      count1+=1
    elif(dic[i]%4==2):
      count2+=1
    elif(dic[i]%4==3):
      count1+=1
      count2+=1
  if(count1>2 or count2>(W+H)//2-1):
    flag=False
if flag:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
alp = "qwertyuiopasdfghjklzxcvbnm"
c = [0]*26
for i in range(h):
    s = input()
    for i in range(26):
        c[i] += s.count(alp[i])
for i in range(26):
    c[i] %= 4
c2 = c.count(2)
c2 += c.count(3)
for i in range(26):
    c[i] %= 2
c1 = c.count(1)
a2 = 0
a1 = 0
if h%2 == 1:
    a2 += w//2
if w%2 == 1:
    a2 += h
if h%2 == 1 and w%2 == 1:
    a1 = 1
if c2 <= a2 and c1 <= a1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = []
for _ in range(h):
    a.append(input())
cnt = [0]*26
for i in a:
    for j in i:
        cnt[ord(j)-ord('a')] += 1

if h%2==0 and w%2==0:
    for i in cnt:
        if i%4!=0:
            print('No')
            break
    else:
        print('Yes')
elif h%2==0 or w%2==0:
    mod2 = 0
    for i in cnt:
        if i%2!=0:
            print('No')
            break
        elif i%4!=0:
            mod2 += i%4
    else:
        tmp = h if h%2==0 else w
        if mod2<=tmp:
            print('Yes')
        else:
            print('No')
else:
    mod2 = 0
    alone = False
    for i in cnt:
        if i%2!=0:
            if alone or i>1:
                print('No')
                break
            alone = True
        elif i%4!=0:
            mod2 += i%4
    else:
        if mod2<=h+w-2:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = []
for _ in range(h):
    a.append(input())
cnt = [0]*26
for i in a:
    for j in i:
        cnt[ord(j)-ord('a')] += 1

if h%2==0 and w%2==0:
    for i in cnt:
        if i%4!=0:
            print('No')
            break
    else:
        print('Yes')
elif h%2==0 or w%2==0:
    mod2 = 0
    for i in cnt:
        if i%2!=0:
            print('No')
            break
        elif i%4!=0:
            mod2 += i%4
    else:
        tmp = h if h%2==0 else w
        if mod2<=tmp:
            print('Yes')
        else:
            print('No')
else:
    mod2 = 0
    alone = False
    for i in cnt:
        if i%2!=0:
            if alone or i>1:
                print('No')
                break
            alone = True
        elif i%4!=0:
            mod2 += i&4
    else:
        if mod2<=h+w-2:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w = ns()
    d = []
    for _ in range(h):
        tmp = list(input())
        d.extend(tmp)

    c = collections.Counter(d)

    flg1 = w % 2
    flg2 = h % 2
    even_pocket = h * flg1 + w * flg2 - flg1 - flg2
    odd_pocket = flg1 * flg2

    ans = True
    for k, v in c.items():
        even_pocket -= ((v % 4) // 2) * 2
        odd_pocket -= v % 2
        if even_pocket < 0 or odd_pocket < 0:
            ans = False
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w = ns()
    d = []
    for _ in range(h):
        tmp = list(input())
        d.extend(tmp)

    c = collections.Counter(d)

    flg1 = w % 2
    flg2 = h % 2
    even_pocket = h * flg1 + w * flg2 - flg1 - flg2
    odd_pocket = flg1 * flg2

    ans = True
    for k, v in c.items():
        even_pocket -= (v % 4) // 2 * 2
        odd_pocket -= v % 2
        if even_pocket < 0 or odd_pocket < 0:
            ans = False
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w = ns()
    d = []
    for _ in range(h):
        tmp = list(input())
        d.extend(tmp)

    c = collections.Counter(d)

    flg1 = w % 2
    flg2 = h % 2
    even_pocket = h * flg1 + w * flg2 - flg1 - flg2
    odd_pocket = flg1 * flg2

    ans = True
    for k, v in c.items():
        even_pocket -= (v % 4) // 2
        odd_pocket -= v % 2
        if even_pocket < 0 or odd_pocket < 0:
            ans = False
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif (H*W)%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and y <= H//2 and z == 0 else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 and len(D.keys()) <= ((H+1)//2)*((W+1)//2) else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif (H*W)%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and z == 0 else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 and len(D.keys()) <= ((H+1)//2)*((W+1)//2) else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif (H*W)%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and z == 0 else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 and len(D.keys()) <= (H//2)*(W//2)+(H//2)+(W//2)+1 else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif H*W%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and z == 0 else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 and len(D.keys()) <= (H//2)*(W//2)+(H//2)+(W//2)+1 else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    H, W = ZZ()
    D = collections.defaultdict(int)
    for i in range(H):
        for c in input(): D[c] += 1

    if 1 in {H, W}:
        if H*W%2 == 0: print('Yes' if all([D[c]%2 == 0 for c in D.keys()]) else 'No')
        else:
            a = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
            print('Yes' if a == 1 else 'No')
    else:
        if H%2 == 0 and W%2 == 0: print('Yes' if all([D[c]%4 == 0 for c in D.keys()]) else 'No')
        elif H*W%2 == 0:
            if W%2 == 0: H, W = W, H
            x = y = z = 0
            for c in D.keys():
                if D[c]%4 == 0: x += 1
                elif D[c]%4 == 2: y += 1
                else: z += 1
            print('Yes' if y%2 == (H//2)%2 and z == 0 else 'No')
        else:
            a = b = 0
            for c in D.keys():
                if D[c]%2 == 1: a += 1
                if D[c] >= 4: b += 1
            print('Yes' if a == 1 and b >= 1 else 'No')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict
H, W = map(int, input().split(' '))
d = defaultdict(int)
for i in range(H):
    for k, v in Counter(input()).items():
        d[k] += v
if H % 2 == 0 and W % 2 == 0:
    for i in d.values():
        if i % 4 != 0:
            print('No')
            exit()
    print('Yes')
elif H % 2 == 0 or W % 2 == 0:
    cnt = H//2 if not H % 2 else W//2
    for i in d.values():
        if i % 4 == 0:
            continue
        elif i % 2 == 0:
            cnt -= 1
            if cnt >= 0:
                continue
        print('No')
        exit()
    print('Yes')
else:
    odd = 1
    cnt = H+W-1
    for i in d.values():
        if i % 2 != 0 and odd:
            odd -=1
            continue
        elif i % 4 == 0:
            continue
        elif i % 2 == 0 and cnt:
            cnt -= 1
            continue
        else:
            print('No')
            exit()
    print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    h,w=map(int,input().split())
    s=""
    for _ in range(h):
        s+=input()
    sc=list(Counter(s).values())
    cunt_1=(h%2)*(w%2)
    cunt_2=(h%2)*(h//2)+(w%2)*(w//2)
    cunt_4=(h//2)*(w//2)
    #print(sc)
    for i,scc in enumerate(sc):
        if scc>=4:
            x=min(scc//4,cunt_4)
            sc[i]-=x*4
            cunt_4-=x
        if cunt_4==0:break
    else:
        print("No")
        sys.exit()
    for i,scc in enumerate(sc):
        if scc>=2:
            x=min(scc//2,cunt_2)
            sc[i]-=x*2
            cunt_2-=x
        if cunt_2==0:break
    else:
        print("No") 
        sys.exit()
    for i,scc in enumerate(sc):
        if scc>=1:
            x=min(scc,cunt_1)
            sc[i]-=x*2
            cunt_1-=x
        if cunt_1==0:break
    else:
        print("No")   
        sys.exit()  
    print("Yes")

        

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    h,w=map(int,input().split())
    s=""
    for _ in range(h):
        s+=input()
    sc=list(Counter(s).values())
    if h%2==1 and w%2==1:
        for i,scc in enumerate(sc):
            if scc%2==1:
                sc[i]-=1
                break
    cunt_2=0
    if h%2==1:cunt_2+=w//2
    if w%2==1:cunt_2+=h//2
    for i,scc in enumerate(sc):
        if scc%4==2:
            if cunt_2==0:
                break
            sc[i]-=2
            cunt_2-=1
    cunt_4=(h//2)*(w//2)
    for i,scc in enumerate(sc):
        if scc>0 and scc%4==0:
            if  cunt_4==0:
                break
            sc[i]-=4
            cunt_4-=1
    print("Yes" if all(scc==0 for scc in sc) else "No")

        

if __name__=='__main__':
    main()

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 cnt2+cnt3>(h-1)//2+(w-1)//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
  rem=1-cnt1
  if cnt1+cnt3!=1:print("No")
  elif cnt2+cnt3-rem>(h-1)//2+(w-1)//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
  rem=1-cnt1
  if cnt1+cnt3!=1:print("No")
  elif cnt2+cnt3-rem>h+w-2:print("No")
  else:
    print("Yes")