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:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
table = [0] * 26
a = ord("a")
for _ in range(H):
  s = list(input())[: -1]
  for j in range(W):
    table[ord(s[j]) - a] += 1
#print(table)
t = []
for i in range(26):
  if table[i] > 0: t.append(table[i])

x = 0
y = 0
c = 0
if H % 2 and (W % 2):
  c = 1
  x -= 1
  y -= 1
if H % 2: y += W // 2
if W % 2: x += H // 2
#print(x, y, c)
if H > 1 and (W > 1):
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
    if p % 4:
      if x > 0:
        x -= 1
      elif y > 0:
        y -= 1
      else:
        print("No")
        exit(0)
else:
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
table = [0] * 26
a = ord("a")
for _ in range(H):
  s = list(input())[: -1]
  for j in range(W):
    table[ord(s[j]) - a] += 1
#print(table)
t = []
for i in range(26):
  if table[i] > 0: t.append(table[i])

x = 0
y = 0
c = 0
if H % 2 and (W % 2):
  c = 1
  x -= 1
  y -= 1
if H % 2: y += W
if W % 2: x += H
#print(x, y, c)
if H > 1 and (W > 1):
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
    if p % 4:
      if x > 0:
        x -= 1
      elif y > 0:
        y -= 1
      else:
        print("No")
        exit(0)
else:
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
table = [0] * 26
a = ord("a")
for _ in range(H):
  s = list(input())[: -1]
  for j in range(W):
    table[ord(s[j]) - a] += 1
#print(table)
t = []
for i in range(26):
  if table[i] > 0: t.append(table[i])

x = 0
y = 0
c = 0
if H % 2 and (W % 2):
  c = 1
  x -= 1
  y -= 1
if H % 2: y += W
if W % 2: x += W
#print(x, y, c)
if H > 1 and (W > 1):
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
    if p % 4:
      if x > 0:
        x -= 1
      elif y > 0:
        y -= 1
      else:
        print("No")
        exit(0)
else:
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
print("Yes")

Submission status: Wrong Answer

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

for i in range(H):
    a.extend(list(input()))

b=[0]*26
abc="abcdefghijklmnopqrstuvwxyz"

for i in a:
    b[abc.index(i)]+=1

if max(b)==H*W:
    print("Yes")
    exit()

if H==2 and W==2:
    if max(b)!=4:
        print("No")
        exit()


num_odd=0
num_4=0
num_2=0

for i in range(26):
    if b[i]%4!=0:
        if b[i]%2==1:
            if num_odd!=0:
                print("No")
                exit()
            else:
                num_odd=b[i]
        else:
            num_4+=4*(b[i]//4)
            num_2+=b[i]%4

if H%2==0 and W%2==0:
    if num_odd!=0 or num_2!=0:
        print("No")
    else:
        print("Yes")
elif H%2==1 and W%2==0:
    if num_4-(H-1)*W>=0 and num_4-(H-1)*W+num_2==W:
        print("Yes")
    else:
        print("No")
elif H%2==0 and W%2==1:
    if num_4-(W-1)*H>=0 and num_4-(W-1)*H+num_2==H:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==1:
    if num_4+4*(num_odd//4)>=(H-1)*(W-1):
        print("Yes")
    else:print("No")

Submission status: Wrong Answer

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

for i in range(H):
    a.extend(list(input()))

b=[0]*26
abc="abcdefghijklmnopqrstuvwxyz"

for i in a:
    b[abc.index(i)]+=1

if max(b)==H*W:
    print("Yes")
    exit()

if H==2 and W==2:
    if max(b)!=4:
        print("No")
        exit()


num_odd=0
num_2=0

for i in range(26):
    if b[i]%4!=0:
        if b[i]%2==1:
            if num_odd!=0:
                print("No")
                exit()
            else:
                num_odd=b[i]
        else:
            num_2+=b[i]%4

if H%2==0 and W%2==0:
    if num_odd!=0 or num_2!=0:
        print("No")
    else:
        print("Yes")
elif H%2==1 and W%2==0:
    if W==num_2:
        print("Yes")
    else:
        print("No")
elif H%2==0 and W%2==1:
    if H==num_2:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==1:
    for i in range(1,min(H+1,W+1),2):
        if (num_odd-i*i)%4+num_2+2*i==H+W:
            print("Yes")
            exit()
    for i in range(1,abs(H-W)):
        if (num_odd-min(H,W)**2)-4*i+num_2+2*min(H,W)==H+W:
            print("Yes")
            exit()
    print("No")

Submission status: Wrong Answer

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

for i in range(H):
    a.extend(list(input()))

b=[0]*26
abc="abcdefghijklmnopqrstuvwxyz"

for i in a:
    b[abc.index(i)]+=1

if max(b)==H*W:
    print("Yes")
    exit()

if H==2 and W==2:
    if max(b)!=4:
        print("No")
        exit()


num_odd=0
num_2=0

for i in range(26):
    if b[i]%4!=0:
        if b[i]%2==1:
            if num_odd!=0:
                print("No")
                exit()
            else:
                num_odd=b[i]
        else:
            num_2+=b[i]%4

if H%2==0 and W%2==0:
    if num_odd!=0 or num_2!=0:
        print("No")
    else:
        print("Yes")
elif H%2==1 and W%2==0:
    if W==num_2+num_odd:
        print("Yes")
    else:
        print("No")
elif H%2==0 and W%2==1:
    if H==num_2+num_odd:
        print("Yes")
    else:
        print("No")
elif H%2==1 and W%2==1:
    for i in range(1,min(H+1,W+1),2):
        if num_odd==i*i:
            print("Yes")
            exit()
        elif (num_odd-i*i)%4+num_2+2*i==H+W:
            print("Yes")
            exit()
    print("No")

Submission status: Wrong Answer

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

for i in range(H):
    a.extend(list(input()))

b=[0]*26
abc="abcdefghijklmnopqrstuvwxyz"

for i in a:
    b[abc.index(i)]+=1

if max(b)==H*W:
    print("Yes")
    exit()

if H==2 and W==2:
    if max(b)!=4:
        print("No")
        exit()


odd_num=0

for i in range(26):
    if b[i]%2==1:
        if odd_num!=0:
            print("No")
            exit()
        else:
            odd_num=b[i]

if H%2==1 and W%2==1:
    if odd_num%H==0 or odd_num%W==0:
        print("Yes")
        exit()
    for i in range(1,min(H+1,W+1),2):
        if odd_num==i**2:
            print("Yes")
            exit()
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

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

for i in range(H):
    a.extend(list(input()))

b=[0]*26
abc="abcdefghijklmnopqrstuvwxyz"

for i in a:
    b[abc.index(i)]+=1

if H==2 and W==2:
    if max(b)!=4:
        print("No")
    else:
        print("Yes")
    exit()


odd_num=0

for i in range(26):
    if b[i]%2==1:
        if odd_num!=0:
            print("No")
            exit()
        else:
            odd_num=b[i]

if H%2==1 and W%2==1:
    for i in range(1,min(H+1,W+1),2):
        if odd_num==i**2:
            print("Yes")
            exit()
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    H, W = map(int, input().split())
    import collections
    D = collections.defaultdict(int)
    for _ in range(H):
        s = input().strip()
        for c in s:
            D[c] += 1
    A = list(D.values())
    for i in range((H+1)//2):
        for j in range((W+1)//2):
            A.sort()
            n = 1
            if not (H % 2 == 1 and i == H//2):
                n *= 2
            if not (W % 2 == 1 and j == W//2):
                n *= 2
            if A[-1] >= n:
                A[-1] -= n
            else:
                print("No")
                return
    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
t="abcdefghijklmnopqrstuvwxyz"
h,w=map(int,input().split())
a=[list(input())for _ in range(h)]
b=[0]*27
for i in a:
  for j in i:b[t.index(j)]+=1
fc=tc=oc=0
for i in range(27):
  g=b[i]
  fc+=g//4
  g-=g//4*4
  tc+=g//2
  g-=g//2*2
  oc+=g
if fc!=(h//2)*(w//2):print("No");exit()
if tc!=(h%2)*(w//2)+(h//2)*(w%2):print("No");exit()
if oc!=(h%2)*(w%2):print("No");exit()
print("Yes")
#一例を出すやつ、問題誤読して実装したので供養
"""
c=[w*[0]for _ in range(h)]
fi=ti=0
for i in range(27):
  while b[i]:
    if b[i]>=4:
      j,k=fi//(w//2),fi%(w//2)
      c[j][k]=c[j][w-k-1]=c[h-j-1][k]=c[h-j-1][w-k-1]=t[i]
      fi+=1
      b[i]-=4
    if b[i]==2:
      if ti<h//2 and w%2:c[ti][w//2]=c[h-ti-1][w//2]=t[i]
      else:c[h//2][ti]=c[h//2][w-1-ti]=t[i]
      ti+=1
      b[i]-=2
    if b[i]==1:
      c[h//2][w//2]=t[i]
      b[i]-=1
for i in c:print("".join(i))
"""

Submission status: Wrong Answer

Solution code:
def examC():
    H, W = LI()
    A = [SI() for _ in range(H)]
    d = defaultdict(int)
    ans = "Yes"
    for a in A:
        for s in a:
            d[s] +=1
#    print(d)
    h = H//2; w = W//2; k = h*w
    for key,i in d.items():
        while(d[key]>=4):
            d[key]-=4
            k -=1
            if k == 0:
                break
        if k == 0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    k = 0
    if H%2==1:
        k +=w
    if W%2==1:
        k +=h
    for key,i in d.items():
        while(d[key]>=2):
            d[key] -= 2
            k -= 1
            if k == 0:
                break
        if k ==0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

Solution code:
def examC():
    H, W = LI()
    A = [SI() for _ in range(H)]
    d = defaultdict(int)
    ans = "Yes"
    for a in A:
        for s in a:
            d[s] +=1
#    print(d)
    h = H//2; w = W//2; k = h*w
    for key,i in d.items():
        while(i>=4):
            d[key]-=4
            k -=1
            if k == 0:
                break
        if k == 0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    k = 0
    if H%2==1:
        k +=w
    if W%2==1:
        k +=h
    for key,i in d.items():
        while(i>=2):
            d[key] -= 2
            k -= 1
            if k == 0:
                break
        if k ==0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

Solution code:
def examC():
    H, W = LI()
    A = [SI() for _ in range(H)]
    d = defaultdict(int)
    ans = "Yes"
    for a in A:
        for s in a:
            d[s] +=1
#    print(d)
    h = H//2; w = W//2; k = h*w
    for key,i in d.items():
        if i>=4:
            d[key]-=4
            k -=1
        if k==0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    k = 0
    if H%2==1:
        k +=w
    if W%2==1:
        k +=h
    for key,i in d.items():
        if i >= 2:
            d[key] -= 2
            k -= 1
        if k ==0:
            break
    if k>0:
        ans = "No"
#    print(d,k)
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

Solution code:
import collections
H, W = map(int, input().split())
table = collections.defaultdict(int)
for i in range(H):
    for x in input():
        table[x] += 1

tmp = 0

for x in table:
    if table[x] % 2 == 1:
        table[x] -= 1
        tmp += 1
num = [0, 0]
for x in table:
    num[1] += table[x] // 4
    if table[x] % 4 != 0:
        num[0] += 1


if H % 2 == 1 and W % 2 == 1:
    if tmp != 1:
        print("No")
        exit(0)
    else:
        if H+W - 2 < num[0] * 2:
            print("No")

            exit(0)
        else:
            if ((H+W - 2) - num[0]*2) % 4 != 0:
                print("No")
                exit(0)

elif H % 2 == 1 or W % 2 == 1:
    if tmp != 0:
        print("No")
        exit(0)
    else:
        if W < num[0]*2:
            print("No")
            exit(0)
        else:
            if (W - num[0]*2) % 4 != 0:
                print("No")
                exit(0)

else:
    if num[0] != 0 or tmp != 0:
        print("No")
        exit(0)


print("Yes")

Submission status: Wrong Answer

Solution code:
import collections
H, W = map(int, input().split())
table = collections.defaultdict(int)
for i in range(H):
    for x in input():
        table[x] += 1

tmp = 0

for x in table:
    if table[x] % 2 == 1:
        table[x] -= 1
        tmp += 1
num = [0, 0]
for x in table:
    num[1] += table[x] // 4
    if table[x] % 4 != 0:
        num[0] += 1


if H % 2 == 1 and W % 2 == 1:
    if tmp != 1:
        print("No")
        exit(0)
    else:
        if H+W - 2 < num[0] * 2:
            print("No")

            exit(0)
        else:
            if ((H+W - 2) - num[0]*2) % 4 != 0:
                print("No")
                print("hoge")
                exit(0)

elif H % 2 == 1 or W % 2 == 1:
    if tmp != 0:
        print("No")
        exit(0)
    else:
        if W < num[0]*2:
            print("No")
        else:
            if (W - num[0]*2) % 4 != 0:
                print("No")
                exit(0)

else:
    if num[0] != 0 or tmp != 0:
        print("No")
        exit(0)


print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
a = read().split()
s = []
for i in a:
    s.extend(list(i))

s = Counter(s)
r = [0] * 4
for i in s.values():
    r[i % 4] += 1

if r[1] + r[3] > 1:
    print('No')
    exit()

h = H % 2
w = W % 2

if h and w:
    if not r[1]:
        r[3] -= 1
        r[2] += 1
    if H + W - r[2] * 2 >= 0 and (H + W - 2 - r[2] * 2) % 4 == 0:
        print('Yes')
    else:
        print('No')
else:
    if r[1] + r[3] != 0:
        print('No')
        exit()
    if h:
        if W - r[2] * 2 >= 0 and (W - r[2] * 2) % 4 == 0:
            print('Yes')
        else:
            print('No')
    elif w:
        if H - r[2] * 2 >= 0 and (H - r[2] * 2) % 4 == 0:
            print('Yes')
        else:
            print('No')
    else:
        if r[0] * 4 == H * W:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
a = read().split()
s = []
for i in a:
    s.extend(list(i))

s = Counter(s)
h = H % 2
w = W % 2
one = [i % 2 for i in s.values()].count(1)
not_four = H * W - [i % 2 for i in s.values()].count(0)

if h and w:
    if one == 1 and not_four <= (H + W) // 2:
        print('Yes')
    else:
        print('No')
elif h or w:
    if s.most_common()[-1][1] < 2:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')
else:
    if s.most_common()[-1][1] < 4:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
a = read().split()
s = []
for i in a:
    s.extend(list(i))

s = Counter(s)
h = H % 2
w = W % 2
one = [i % 2 for i in s.values()].count(1)
not_four = H * W - [i % 2 for i in s.values()].count(0)

if h and w:
    if one == 1 and not_four <= (H + W) // 2 - 1:
        print('Yes')
    else:
        print('No')
elif h or w:
    if s.most_common()[-1][1] < 2:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')
else:
    if s.most_common()[-1][1] < 4:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
a = read().split()
s = []
for i in a:
    s.extend(list(i))

s = Counter(s)
H %= 2
W %= 2
one = [i % 2 for i in s.values()].count(1)

if H and W:
    if one <= 1:
        print('Yes')
    else:
        print('No')
elif H or W:
    if s.most_common()[-1][1] < 2:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')
else:
    if s.most_common()[-1][1] < 4:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
a = read().split()
s = []
for i in a:
    s.extend(list(i))

s = Counter(s)
H %= 2
W %= 2
one = [i % 2 for i in s.values()].count(1)

if H and W:
    if one <= 1:
        print('Yes')
    else:
        print('No')
elif H or W:
    if one == 0:
        print('Yes')
    else:
        print('No')
else:
    if s.most_common()[-1][1] < 4:
        print('No')
    else:
        if one == 0:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
d = {}
for i in range(h):
  for c in input():
    if c in d:
      d[c] += 1
    else:
      d[c] = 1
m = ((w + 1)// 2) * ((h + 1) // 2)
ans = "Yes"
if len(d.keys()) > m:
  ans = "No"
oc = 0
fc = 0
for key in d:
  oc += d[key] % 2
  fc += d[key] % 4
if oc > 1 or fc < (w//2) * (h//2):
  ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
d = {}
for i in range(h):
  for c in input():
    if c in d:
      d[c] += 1
    else:
      d[c] = 1
m = ((w + 1)// 2) * ((h + 1) // 2)
ans = "Yes"
if len(d.keys()) > m:
  ans = "No"
oc = 0
for key in d:
  oc += d[key] % 2
if oc > 1:
  ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def solve() :
    d = defaultdict(int)
    for y in range(H) :
        for x in range(W) :
            d[a[y][x]] += 1
            
    one = 1 if H % 2 == W % 2 == 1 else 0
    two = 0

    if H % 2 == 1 : two += W // 2
    if W % 2 == 1 : two += H // 2
        
    for v in d.values() :
        if v % 4 == 2 :
            two -= 1
        elif v % 4 == 1 :
            one -= 1
        
        if one < 0 or two < 0 :
            return 'No'
            
    return 'Yes'
    
print(solve())

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[chr(i) for i in range(97, 97+26)]
d={i:j for i,j in zip(l,range(26))}
c=[0]*26
for i in range(h):
    s=input()
    for j in s:
        c[d[j]]+=1
if h%2==0 and w%2==0:
    if all([i%4==0 for i in c]):#偶数×偶数のとき、全てが4の倍数でなければならない
        print("Yes")
    else:print("No")
else:
    k=h//2*w//2
    if h%2==1 and w%2==1:
        if sum([i%2 for i in c])==1:
            if h==1 or w==1:
                print("Yes") 
            elif sum([i//4 for i in c])>=k:
                print("Yes")
            else:print("No")
        else:
            print("No")
    else:
        if sum([i%2 for i in c])==0:
            if sum([i//4 for i in c])>=k:
                print("Yes")
            else:print("No")
        else:print("No")

Submission status: Wrong Answer

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

S = ""
for _ in range(H):
    S += input()

# print(S)
character_counter = Counter(S)
# print(character_counter)
g1 = (H % 2) * (W % 2)  # 行と列のどちらかが偶数であれば0
g2 = (W // 2) * (H % 2) + (H // 2) * (W % 2)  # 奇数のときに2つのブロックのペアをもてる
g4 = (H // 2) * (W // 2)

# print(g1*1+g2*2+g4*4)  # この値を使い切る必要がある

character_counter_list = sorted(list(character_counter.values()), reverse=True)
# print(character_counter_list)

# g1を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 2 == 1 and g1 > 0:
        character_counter_list[i] -= 1
        g1 -= 1

# g2を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 2 and g2 > 0:
        character_counter_list[i] -= 2  # これによってmod4で0になっている
        g2 -= 1

if g2 % 2 == 1:  # g2が奇数個余っていたら終わり
    print("No")
    exit()

# 余ったg2は2つで1個分のg4として使える
g4 += (g2//2)
# g4を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 0 and g4 > 0:
        tmp = (character_counter_list[i] // 4)
        character_counter_list[i] -= (4*tmp)  # もし文字の数が8個とかなら、g4を2つ使える
        g4 -= tmp

if g1 == 0 and g2 == 0 and g4 == 0 and sum(character_counter_list) == 0:  # 無事に使い切れたらYes
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

S = ""
for _ in range(H):
    S += input()

# print(S)
character_counter = Counter(S)
# print(character_counter)
g1 = (H % 2) * (W % 2)  # 行と列のどちらかが偶数であれば0
g2 = (W // 2) * (H % 2) + (H // 2) * (W % 2)  # 奇数のときに2つのブロックのペアをもてる
g4 = (H // 2) * (W // 2)

# print(g1*1+g2*2+g4*4)  # この値を使い切る必要がある

character_counter_list = sorted(list(character_counter.values()), reverse=True)
# print(character_counter_list)

# g1を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 2 == 1 and g1 > 0:
        character_counter_list[i] -= 1
        g1 -= 1

# g2を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 2 and g2 > 0:
        character_counter_list[i] -= 2  # これによってmod4で0になっている
        g2 -= 1

if g2 % 2 == 1:  # g2が奇数個余っていたら終わり
    print("No")
    exit()

# 余ったg2は2つで1個分のg4として使える
g4 += (g2//2)
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 0 and g4 > 0:
        tmp = character_counter_list[i] // 4
        character_counter_list[i] -= (4*tmp)  # もし文字の数が8個とかなら、g4を2つ使える
        g4 -= tmp

if g1 == 0 and g2 == 0 and g4 == 0 and sum(character_counter_list) == 0:  # 無事に使い切れたらYes
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

S = ""
for _ in range(H):
    S += input()

# print(S)
character_counter = Counter(S)
# print(character_counter)
g1 = (H % 2) * (W % 2)  # 行と列のどちらかが偶数であれば0
g2 = (W // 2) * (H % 2) + (H // 2) * (W % 2)  # 奇数のときに2つのブロックのペアをもてる
g4 = (H // 2) * (W // 2)

# print(g1*1+g2*2+g4*4)  # この値を使い切る必要がある

character_counter_list = sorted(list(character_counter.values()), reverse=True)
# print(character_counter_list)

# g1を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 2 == 1:
        character_counter_list[i] -= 1
        g1 -= 1

# g2を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 2:
        character_counter_list[i] -= 2  # これによってmod4で0になっている
        g2 -= 1

if g2 % 2 == 1:  # g2が奇数個余っていたら終わり
    print("No")
    exit()

# 余ったg2は2つで一個文のg4として使える
g4 += (g2//2)
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 0:
        tmp = character_counter_list[i] // 4
        character_counter_list[i] -= (4*tmp)  # もし文字の数が8個とかなら、g4を2つ使える
        g4 -= tmp

if g1 == 0 and g2 == 0 and g4 == 0 and sum(character_counter_list) == 0:  # 無事に使い切れたらYes
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

S = ""
for _ in range(H):
    S += input()

# print(S)
character_counter = Counter(S)
# print(character_counter)
g1 = (H % 2) * (W % 2)  # 行と列のどちらかが偶数であれば0
g2 = (W // 2) * (H % 2) + (H // 2) * (W % 2)  # 奇数のときに2つのブロックのペアをもてる
g4 = (H // 2) * (W // 2)

# print(g1*1+g2*2+g4*4)  # この値を使い切る必要がある

character_counter_list = sorted(list(character_counter.values()), reverse=True)
# print(character_counter_list)

# g1を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 2 == 1:
        character_counter_list[i] -= 1
        g1 -= 1

# g2を使う
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 2:
        character_counter_list[i] -= 2  # これによってmod4で0になっている
        g2 -= 1

if g2 % 2 == 1:  # g2が奇数個余っていたら終わり
    print("No")
    exit()

# 余ったg2は2つで一個文のg4として使える
g4 += (g2//2)
for i in range(len(character_counter_list)):
    if character_counter_list[i] % 4 == 0:
        tmp = character_counter_list[i] // 4
        character_counter_list[i] -= (4*tmp)  # もし文字の数が8個とかなら、g4を2つ使える
        g4 -= tmp

if g1 == 0 and g2 == 0 and g4 == 0:  # 無事に使い切れたらYes
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
h,w=map(int,input().split())
a=[list(input()) for i in range(h)]
l=[chr(i) for i in range(97, 97+26)]
ctl=[0]*26
for i in range(h):
  for j in range(w):
    ctl[l.index(a[i][j])]+=1

if h%2==0 and w%2==0:
  ct1=0;ct2=0;ct4=(h*w)//4
elif h%2==1 and w%2==1:
  ct1=1;ct2=(h+w-1)//2;ct4=(h*w-ct1-ct2)//4
elif h%2==0 and w%2==1:
  ct1=0;ct2=h//2;ct4=(h*w-ct2)//4
else:
  ct1=0;ct2=w//2;ct4=(h*w-ct2)//4

for i in range(26):
  while ctl[i]>=4 and ct4!=0:
    ctl[i]-=4
    ct4-=1
  if ct4==0:
    break
if ct4!=0:
  print('No')
  sys.exit()

for i in range(26):
  while ctl[i]>=2 and ct2!=0:
    ctl[i]-=2
    ct2-=1
  if ct2==0:
    break
if ct2!=0:
  print('No')
  sys.exit()

for i in range(26):
  while ctl[i]>=1 and ct1!=0:
    ctl[i]-=1
    ct1-=1
  if ct1==0:
    break
if ct1!=0:
  print('No')
  sys.exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
h,w=map(int,input().split())
a=[list(input()) for i in range(h)]
l=[chr(i) for i in range(97, 97+26)]
ctl=[0]*26
for i in range(h):
  for j in range(w):
    ctl[l.index(a[i][j])]+=1

if h%2==0 and w%2==0:
  ct1=0;ct2=0;ct4=(h*w)//4
elif h%2==1 and w%2==1:
  ct1=1;ct2=(h+w-1)//2;ct4=(h*w-ct1-ct2)//4
elif h%2==0 and w%2==1:
  ct1=0;ct2=h//2;ct4=(h*w-ct2)//4
else:
  ct1=0;ct2=w//2;ct4=(h*w-ct2)//4

for i in range(26):
  if ctl[i]>=4:
    ctl[i]-=4
    ct4-=1
    if ct4==0:
      break
if ct4!=0:
  print('No')
  sys.exit()

for i in range(26):
  if ctl[i]>=2:
    ctl[i]-=2
    ct2-=1
    if ct2==0:
      break
if ct2!=0:
  print('No')
  sys.exit()

for i in range(26):
  if ctl[i]>=1:
    ctl[i]-=1
    ct1-=1
    if ct1==0:
      break
if ct1!=0:
  print('No')
  sys.exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())

g1 = (H % 2) * (W % 2)
g2 = (H % 2) * (W//2) + (W % 2) * (H//2)
g4 = (H//2) * (W//2)
flag = True

for _ in range(g1):
    for i in range(len(count_list)):
        if count_list[i] % 4 in [1, 3]:
            count_list[i] -= 1
            break

for _ in range(g2):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2:
            count_list[i] -= 2
            count_list = sorted(count_list, reverse=True)
            break
for _ in range(g4):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 0:
            count_list[i] -= 4
            count_list = sorted(count_list, reverse=True)
            break

if all([val == 0 for val in count_list]):
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())

g1 = (H % 2) * (W % 2)
g2 = (H % 2) * (W//2) + (W % 2) * (H//2)
g4 = (H//2) * (W//2)
flag = True

for _ in range(g1):
    for i in range(len(count_list)):
        if count_list[i] % 4 in [1, 3]:
            count_list[i] -= 1
            break

for _ in range(g2):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2:
            count_list[i] -= 2
            count_list = sorted(count_list, reverse=True)
            break
for _ in range(g4):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 0:
            count_list[i] -= 4
            count_list = sorted(count_list, reverse=True)
            break

if any(count_list):
    print("No")
else:
    print("Yes")