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 heapq import *
H, W = map(int, input().split())

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ns = {}
for i in range(H):
  As = input()
  for a in As:
    if a not in ns: ns[a] = 0
    ns[a] += 1
ns4 = [0, 0, 0, 0]
for a in ns:
  n = ns[a]
  ns4[n%4] += 1
print(ns4)
if H%2 == 0 and W%2 == 0:
  if ns4[1] > 0 or ns4[2] > 0 or ns4[3] > 0:
    print("No")
  else:
    print("Yes")
if H%2 == 1 and W%2 == 0:
  if ns4[1]+ns4[3] > 0:
    print("No")
  elif ns4[2] > W//2:
    print("No")
  else:
    print("Yes")
if H%2 == 0 and W%2 == 1:
  if ns4[1]+ns4[3] > 0:
    print("No")
  elif ns4[2] > H//2:
    print("No")
  else:
    print("Yes")
if H%2 == 1 and W%2 == 1:
  if ns4[1]+ns4[3] > 1:
    print("No")
  elif ns4[2]+ns4[3] > H//2+W//2:
    print("No")
  else:
    print("Yes")

Submission status: Wrong Answer

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


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

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

print("Yes")

Submission status: Wrong Answer

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


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

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

print("Yes")

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
for i in range(H):
    S[i].sort()
S.sort()
ans = [[None] * W for _ in range(H)]
if H % 2:
    d = 1
else:
    d = 0
l = 0
for i in range(H):
    k = 0
    if i % 2 == 0:
        for j in range(W):
            if j % 2 == 0:
                ans[l][k] = S[i][j]
            else:
                ans[l][W-k-1] = S[i][j]
                k += 1
    else:
        for j in range(W):
            if j % 2 == 0:
                ans[H-l-1][k] = S[i][j]
            else:
                ans[H-l-1][W-k-1] = S[i][j]
                k += 1
        l += 1

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

print("Yes")

Submission status: Wrong Answer

Solution code:
# code-festival-2017-qualaC - Palindromic Matrix
from collections import Counter


def main():
    H, W, *A = open(0).read().split()
    H, W, C = int(H), int(W), Counter("".join(A)).values()
    if H % 2 and W % 2:
        g1, g2, g4 = 1, H // 2 + W // 2, H * W // 4
    elif H % 2 or W % 2:
        g1, g2, g4 = 0, H // 2 + W // 2 - 1, H * W // 4
    else:
        g1, g2, g4 = 0, 0, H * W // 4
    for i in C:
        x = min(i // 4, g4)
        i -= x * 4
        g4 -= x
        y = min(i // 2, g2)
        i -= y * 2
        g2 -= y
        if i % 2 and g1:
            i -= 1
            g1 -= 1
        if i:
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2017-qualaC - Palindromic Matrix
from collections import Counter


def main():
    H, W, *A = open(0).read().split()
    H, W, C = int(H), int(W), Counter("".join(A)).values()
    if H % 2 and W % 2:
        g1, g2, g4 = 1, H // 2 + W // 2, H * W // 4
    elif H % 2 or W % 2:
        g1, g2, g4 = 0, H // 2 + W // 2 - 1, H * W // 4
    else:
        g1, g2, g4 = 0, 0, H * W // 4
    for i in C:
        if i % 2 and g1:
            i -= 1
            g1 -= 1
        x = min(i // 4, g4)
        i -= x * 4
        g4 -= x
        y = min(i // 2, g2)
        i -= y * 2
        g2 -= y
        if i:
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2017-qualaC - Palindromic Matrix
from collections import Counter


def main():
    H, W, *A = open(0).read().split()
    H, W, C = int(H), int(W), Counter("".join(A)).values()
    if H % 2 and W % 2:
        g1, g2, g4 = 1, H // 2 + W // 2, H * W // 4
    elif H % 2 or W % 2:
        g1, g2, g4 = 0, H // 2 + W // 2 - 1, H * W // 4
    else:
        g1, g2, g4 = 0, 0, H * W // 4
    for i in C:
        if i % 2 and g1:
            i -= 1
            g1 -= 1
        if i % 2 == 0:
            x = min(i // 4, g4)
            i -= x * 4
            g4 -= x
            y = min(i // 2, g2)
            i -= y * 2
            g2 -= y
        if i:
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2017-qualaC - Palindromic Matrix
from collections import Counter


def main():
    H, W, *A = open(0).read().split()
    H, W, C = int(H), int(W), Counter("".join(A)).values()
    if H % 2 and W % 2:
        g1, g2, g4 = 1, H // 2 + W // 2, H * W // 4
    elif H % 2 or W % 2:
        g1, g2, g4 = 0, H // 2 + W // 2 - 1, H * W // 4
    else:
        g1, g2, g4 = 0, 0, H * W // 4
    for i in C:
        if i % 2 and g1:
            i -= 1
            g1 -= 1
        if i % 2 == 0 and g4:
            x = min(i // 4, g4)
            i -= x * 4
            g4 -= x
            y = min(i // 2, g2)
            i -= y * 2
            g2 -= y
        if i:
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2017-qualaC - Palindromic Matrix
from collections import Counter


def main():
    H, W, *A = open(0).read().split()
    H, W = int(H), int(W)
    C = [i % 2 for i in Counter("".join(A)).values()]
    if H * W in (2, 4):
        flg = len(C) == 1
    else:
        x = sum(C)
        p = int(H) * int(W) % 2
        flg = x == 1 if p else x == 0
    print("Yes" if flg else "No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#CODEFESTIVAL2017 qual A C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
s = [input() for _ in range(h)]
cnt = defaultdict(lambda : 0)
for i in range(h):
    for j in range(w):
        cnt[s[i][j]] += 1
g1,g2,g4 = (w%2)&(h%2),(w%2)*h//2 + (h%2)*w//2,h//2 * w//2
for i in range(g1):
    for j,k in cnt.items():
        if k > 0 and k % 4 == 1 or k % 4 == 3:
            cnt[j] -= 1
            break
for i in range(g2):
    for j,k in cnt.items():
        if k > 0 and k % 4 == 2:
            cnt[j] -= 2
            break
for i in range(g4):
    for j,k in cnt.items():
        if k >0 and k % 4 == 0:
            cnt[j] -= 4
            break
for i,j in cnt.items():
    if j == 0:
        continue
    print('No')
    quit()
print('Yes')

Submission status: Wrong Answer

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

D = {}

for i in range(H) :
    for s in input() :
        if s not in D :
            D[s] = 1
        else :
            D[s] += 1
cnt4 = 0
cnt2 = 0
for v in D.values() :
    d,r = divmod(v, 4)
    cnt4 += d
    cnt2 += 1 if r >= 2 else 0

cnt4 -= (W//2) * (H//2)

if cnt4 < 0 :
    ans = "No"
else :
    cnt2 += cnt4*2
    if H % 2 == 1 and W % 2 == 0 :
        if cnt2 >= W//2 :
            ans = "Yes"
        else :
            ans = "No"
    elif H % 2 == 0 and W % 2 == 1 :
        if cnt2 >= H//2 :
            ans = "Yes"
        else :
            ans = "No"
    else :
        if cnt2 >= H//2 + W//2 :
            ans = "Yes"
        else :
            ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter


def main():
    H, W = map(int, input().split())
    A = [input() for _ in range(H)]
    space = {}
    space[1] = (H % 2) * (W % 2)
    space[2] = (H % 2) * (W//2*2) + (W % 2) * (H//2*2)
    space[4] = (H//2*2) * (W//2*2)
    cnt = Counter("".join(A))
    for alpha in cnt:
        c = cnt[alpha]
        for k in (4, 2, 1):
            while c//k >= 1 and space[k]:
                y = min(c//k, space[k])
                space[k] -= y
                c -= y * k
        if c:
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[['']*w for _ in range(h)]
print(ans)
char={}
for _ in range(h):
  s=input()
  for c in s:
    if c not in char:
      char[c]=1
    else:
      char[c]+=1
print(char)
ch=h//2
cw=w//2
fh=False
fw=False
if h%2==1:
  ch+=1
  fh=True
if w%2==1:
  cw+=1
  fw=True
for i in range(ch):
  for j in range(cw):
    if (i==ch-1 and fh==True) and (j==cw-1 and fw==True):
      for c in char.keys():
        if char[c]%2==1:
          ans[i][j]=c
          char[c]-=1
          if char[c]==0:
            del char[c]
          break
    elif i==ch-1 and fh==True:
      for c in char.keys():
        if char[c]%4==2:
          ans[i][j]=c
          ans[i][w-j-1]=c
          char[c]-=2
          if char[c]==0:
            del char[c]
          break
      else:
        for c in char.keys():
          if char[c]>=2:
            ans[i][j]=c
            ans[i][w-j-1]=c
            char[c]-=2
            if char[c]==0:
              del char[c]
            break
    elif j==cw-1 and fw==True:
      for c in char.keys():
        if char[c]%4==2:
          ans[i][j]=c
          ans[h-i-1][j]=c
          char[c]-=2
          if char[c]==0:
            del char[c]
          break
      else:
        for c in char.keys():
          if char[c]>=2:
            ans[i][j]=c
            ans[h-i-1][j]=c
            char[c]-=2
            if char[c]==0:
              del char[c]
            break
    else:
      for c in char.keys():
        if char[c]>=4:
          ans[i][j]=c
          ans[i][w-j-1]=c
          ans[h-i-1][j]=c
          ans[h-i-1][w-j-1]=c
          char[c]-=4
          if char[c]==0:
            del char[c]
          break
fy=True
for i in range(ch):
  for j in range(cw):
    if ans[i][j]=='':
      fy=False
if fy==True:
  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=sorted([j%4 for i,j in Counter(s).items()])[::-1]
c=Counter(c)
#c=sorted(c,key=lambda x: x[1])[::-1]
rh=h%2
rw=w%2
k=0
od=c[1]+c[3]
non4=c[2]+od
if rh*rw==1:
    if od!=1:
        k=1
    if c[1]+c[2]*2+c[3]*3>2*w+2*h-1:
        k=1
elif rh==0 and rw==0:
    if non4>0:
        k=1
elif rh==1:
    if od>0:
        k=1
    if c[2]*2>w:
        k=1
else:  # rw==1
    if od>0:
        k=1
    if c[2]*2>h:
        k=1
ans=['Yes','No']
print(ans[k])

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=sorted([j%4 for i,j in Counter(s).items()])[::-1]
c=Counter(c)
#c=sorted(c,key=lambda x: x[1])[::-1]
rh=h%2
rw=w%2
k=0
od=c[1]+c[3]
non4=c[2]+od
if rh*rw==1:
    if od!=1:
        k=1
    if c[1]+c[2]*2+c[3]*3>2*w+2*h-1:
        k=1
elif rh==0 and rw==0:
    if non4>0:
        k=1
elif rh==1:
    if od>0:
        k=1
    if c[2]>2*w:
        k=1
else:  # rw==1
    if od>0:
        k=1
    if c[2]>2*h:
        k=1
ans=['Yes','No']
print(ans[k])

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=sorted([j%4 for i,j in Counter(s).items()])[::-1]
c=Counter(c)
#c=sorted(c,key=lambda x: x[1])[::-1]
rh=h%2
rw=w%2
k=0
od=c[1]+c[3]
non4=c[2]+od
if rh*rw==1:
    if od!=1:
        k=1
    if non4>2*w+2*h-1:
        k=1
elif rh==0 and rw==0:
    if non4>0:
        k=1
elif rh==1:
    if od>0:
        k=1
    if c[2]>2*w:
        k=1
else:  # rw==1
    if od>0:
        k=1
    if c[2]>2*h:
        k=1
ans=['Yes','No']
print(ans[k])
#print(c)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
da = {}
for _ in range(h):
  a = input().strip()
  for _a in a:
    if _a in da:
      da[_a] += 1
    else:
      da[_a] = 1

if h%2==1 and w%2==1:
  oddc = 1
  div2 = w-1 + h-1
  div4 = h*w-1-div2
elif h%2==0 and w%2==0:
  oddc = 0
  div2 = 0
  div4 = h*w
elif h%2==1:
  oddc = 0
  div2 = w
  div4 = w*h-div2
else:
  oddc = 0
  div2 = h
  div4 = w*h - div2

for _, v in da.items():
  while v > 0:
    if v % 2 == 1:
      oddc -= 1
      v -= 1
    elif v % 4 == 0:
      div4 -= v
      v = 0
    else:
      div2 -= 2
      v -= 2

if h%2==1 and w%2==1 and div2>0 and div2%4==0:
  print('Yes')
elif oddc == 0 and div2 == 0 and div4 == 0:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
def get_input(inp):
  li = inp.split("\n")

  def inner():
    return li.pop(0)
  return inner

INPUT="""5 5
aabaa
aabaa
ccacc
aabaa
aabaa"""
input = get_input(INPUT)

h, w = map(int, input().split())
da = {}
for _ in range(h):
  a = input().strip()
  for _a in a:
    if _a in da:
      da[_a] += 1
    else:
      da[_a] = 1

if h%2==1 and w%2==1:
  oddc = 1
  div2 = w-1 + h-1
  div4 = h*w-1-div2
elif h%2==0 and w%2==0:
  oddc = 0
  div2 = 0
  div4 = h*w
elif h%2==1:
  oddc = 0
  div2 = w
  div4 = w*h-div2
else:
  oddc = 0
  div2 = h
  div4 = w*h - div2

for _, v in da.items():
  while v > 0:
    if v % 2 == 1:
      oddc -= 1
      v -= 1
    elif v % 4 == 0:
      div4 -= v
      v = 0
    else:
      div2 -= 2
      v -= 2

if h%2==1 and w%2==1:
  if div2%4==0:
    print('Yes')
  else:
    print('No')
elif oddc == 0 and div2 == 0 and div4 == 0:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
da = {}
for _ in range(h):
  a = input().strip()
  for _a in a:
    if _a in da:
      da[_a] += 1
    else:
      da[_a] = 1

if h%2==1 and w%2==1:
  oddc = 1
  div2 = w-1 + h-1
  div4 = h*w-1-div2
elif h%2==0 and w%2==0:
  oddc = 0
  div2 = 0
  div4 = h*w
elif h%2==1:
  oddc = 0
  div2 = w
  div4 = w*h-div2
else:
  oddc = 0
  div2 = h
  div4 = w*h - div2

for _, v in da.items():
  while v > 0:
    if v % 2 == 1:
      oddc -= 1
      v -= 1
    elif v % 4 == 0:
      div4 -= v
      v = 0
    else:
      div2 -= 2
      v -= 2

if h%2==1 and w%2==1 and div2==w-1+h-1:
  print('Yes')
elif oddc == 0 and div2 == 0 and div4 == 0:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
da = {}
for _ in range(h):
  a = input().strip()
  for _a in a:
    if _a in da:
      da[_a] += 1
    else:
      da[_a] = 1

if h%2==1 and w%2==1:
  oddc = 1
  div2 = w-1 + h-1
  div4 = h*w-1-div2
elif h%2==0 and w%2==0:
  oddc = 0
  div2 = 0
  div4 = h*w
elif h%2==1:
  oddc = 0
  div2 = w
  div4 = w*h-div2
else:
  oddc = 0
  div2 = h
  div4 = w*h - div2

for _, v in da.items():
  while v > 0:
    if v % 2 == 1:
      oddc -= 1
      v -= 1
    elif v % 4 == 0:
      div4 -= v
      v = 0
    else:
      div2 -= 2
      v -= 2

if oddc == 0 and div2 == 0 and div4 == 0:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w=map(int,input().split())
a=[]
for i in range(h):a.extend(input())
ca=Counter(a).most_common()
lca=[]
for i,j in ca[::-1]:lca.append(j)
if h==1:
    c=[w%2,w//2*2,0]
elif w==1:
    c=[h%2,h//2*2,0]
elif h%2+w%2==2:
    c=[1,h//2+w//2,h//2*w//2]
elif h%2==1:
    c=[0,w,h*w-w]
elif w%2==1:
    c=[0,h,h*w-h]
else:
    c=[0,0,h*w]
f=0
while c[2]>0:
    x=lca[-1]//4
    if x==0:
        f=1
        break
    xi=min(x,c[2])
    c[2]-=xi*4
    lca[-1]-=xi*4
    lca.sort()
while c[1]>0 and f==0:
    x=lca[-1]//2
    if x==0:
        f=1
        break
    xi=min(x,c[1])
    c[1]-=xi*2
    lca[-1]-=xi*2
    lca.sort()
if c[0]==lca[-1] and f==0:print("Yes")
else:print("No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

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

d = defaultdict(int)

for row in a:
    for cell in row:
        d[cell] += 1

# print(d)

h = H % 2
w = W % 2

if h > w:
    h, w = w, h
    # (0,0),(0,1),(1,1)
if h == 0:
    if w == 0:
        cnt4 = (H * W) // 4
        cnt2 = cnt1 = 0
    else:
        cnt4 = ((W - 1) // 2) * (H // 2)
        cnt2 = H // 2
        cnt1 = 0
else:
    cnt4 = ((W - 1) // 2) * ((H - 1) // 2)
    cnt2 = ((W - 1) // 2) + ((H - 1) // 2)
    cnt1 = 1

for c in ascii_lowercase:
    if d[c] % 4 == 1 or d[c] % 4 == 3:
        d[c] -= 1
        cnt1 -= 1
        if cnt1 == 0: break
if cnt1 > 0:
    print('No')
    exit()
for c in ascii_lowercase:
    if d[c] % 4 == 2:
        d[c] -= 2
        cnt2 -= 1
        if cnt2 == 0: break
if cnt2 > 0:
    print('No')
    exit()
for c in ascii_lowercase:
    if d[c] % 4 == 0 and d[c] > 0:
        d[c] -= 4
        cnt4 -= 1
        if cnt4 == 0: break
if cnt4 > 0:
    print('No')
    exit()
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

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

d = defaultdict(int)

for row in a:
    for cell in row:
        d[cell] += 1

# print(d)

h = H % 2
w = W % 2

if h > w:
    h, w = w, h
    # (0,0),(0,1),(1,1)
if h == 0:
    if w == 0:
        cnt4 = H * W // 4
        cnt2 = cnt1 = 0
    else:
        cnt4 = (W - 1) // 2 * H // 2
        cnt2 = H // 2
        cnt1 = 0
else:
    cnt4 = (W - 1) // 2 * (H - 1) // 2
    cnt2 = (W - 1) // 2 + (H - 1) // 2
    cnt1 = 1

for c in ascii_lowercase:
    if d[c] % 4 == 1 or d[c] % 4 == 3:
        d[c] -= 1
        cnt1 -= 1
        if cnt1 == 0: break
if cnt1 > 0:
    print('No')
    exit()
for c in ascii_lowercase:
    if d[c] % 4 == 2:
        d[c] -= 2
        cnt2 -= 1
        if cnt2 == 0: break
if cnt2 > 0:
    print('No')
    exit()
for c in ascii_lowercase:
    if d[c] % 4 == 0 and d[c] > 0:
        d[c] -= 4
        cnt4 -= 1
        if cnt4 == 0: break
if cnt4 > 0:
    print('No')
    exit()
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

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

d = defaultdict(int)

for row in a:
    for cell in row:
        d[cell] += 1

# print(d)

h = H % 2
w = W % 2

if h > w:
    h, w = w, h
    # (0,0),(0,1),(1,1)
if h == 0:
    if w == 0:
        print('Yes' if all(map(lambda x: x % 4 == 0, d.values())) else 'No')
    else:
        cnt4 = (W - 1) // 2 * H // 2
        cnt2 = H // 2
        for c in ascii_lowercase:
            if d[c] >= 4:
                d[c] -= 4
                cnt4 -= 1
                if cnt4 == 0: break
        if cnt4 > 0:
            print('No')
            exit()
        for c in ascii_lowercase:
            if d[c] >= 2:
                d[c] -= 2
                cnt2 -= 1
                if cnt2 == 0: break
        if cnt2 > 0:
            print('No')
        else:
            print('Yes')
else:
    cnt4 = (W - 1) // 2 * (H - 1) // 2
    cnt2 = (W - 1) // 2 + (H - 1) // 2
    cnt1 = 1
    for c in ascii_lowercase:
        if d[c] >= 4:
            d[c] -= 4
            cnt4 -= 1
            if cnt4 == 0: break
    if cnt4 > 0:
        print('No')
        exit()
    for c in ascii_lowercase:
        if d[c] >= 2:
            d[c] -= 2
            cnt2 -= 1
            if cnt2 == 0: break
    if cnt2 > 0:
        print('No')
        exit()
    else:
        print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

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

d = defaultdict(int)

for row in a:
    for cell in row:
        d[cell] += 1

# print(d)

h = H % 2
w = W % 2

if h > w:
    h, w = w, h
    # (0,0),(0,1),(1,1)
if h == 0:
    if w == 0:
        print('Yes' if all(map(lambda x: x % 4 == 0, d.values())) else 'No')
    else:
        cnt4 = (W - 1) // 2 * h // 2
        cnt2 = W // 2
        for c in ascii_lowercase:
            if d[c] >= 4:
                d[c] -= 4
                cnt4 -= 1
                if cnt4 == 0: break
        if cnt4 > 0:
            print('No')
            exit()
        for c in ascii_lowercase:
            if d[c] >= 2:
                d[c] -= 2
                cnt2 -= 1
                if cnt2 == 0: break
        if cnt2 > 0:
            print('No')
        else:
            print('Yes')
else:
    cnt4 = (W - 1) // 2 * (H - 1) // 2
    cnt2 = (W - 1) // 2 + (H - 1) // 2
    cnt1 = 1
    for c in ascii_lowercase:
        if d[c] >= 4:
            d[c] -= 4
            cnt4 -= 1
            if cnt4 == 0: break
    if cnt4 > 0:
        print('No')
        exit()
    for c in ascii_lowercase:
        if d[c] >= 2:
            d[c] -= 2
            cnt2 -= 1
            if cnt2 == 0: break
    if cnt2 > 0:
        print('No')
        exit()
    for c in ascii_lowercase:
        if d[c] >= 1:
            d[c] -= 1
            cnt1 -= 1
            if cnt1 == 0: break
    if cnt1 > 0:
        print('No')
    else:
        print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_lowercase

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

d = defaultdict(int)

for row in a:
    for cell in row:
        d[cell] += 1

# print(d)

h = H % 2
w = W % 2

if h > w:
    h, w = w, h
    # (0,0),(0,1),(1,1)
if h == 0:
    if w == 0:
        print('Yes' if all(map(lambda x: x % 4 == 0, d.values())) else 'No')
    else:
        cnt4 = (W - 1) // 2 * h // 2
        cnt2 = W // 2
        for c in ascii_lowercase:
            if d[c] >= 4:
                d[c] -= 4
                cnt4 -= 1
                if cnt4 == 0: break
        if cnt4 > 0:
            print('No')
            exit()
        for c in ascii_lowercase:
            if d[c] >= 2:
                d[c] -= 2
                cnt2 -= 1
                if cnt2 == 0: break
        if cnt2 > 0:
            print('No')
        else:
            print('Yes')
else:
    cnt4 = (W - 1) // 2 * (H - 1) // 2
    cnt2 = (W - 1) // 2 + (H - 1) // 2
    cnt1 = 1
    for c in ascii_lowercase:
        if d[c] >= 4:
            d[c] -= 4
            cnt4 -= 1
            if cnt4 == 0: break
    if cnt4 > 0:
        print('No')
        exit()
    for c in ascii_lowercase:
        if d[c] >= 2:
            d[c] -= 2
            cnt2 -= 1
            if cnt2 == 0: break
    if cnt2 > 0:
        print('No')
    else:
        print('Yes')

Submission status: Wrong Answer

Solution code:
#CODEFESTIVAL2017予選A-C
#PalindromicMatrix
from collections import defaultdict
alpha='abcdefghijklmnopqrstuvwxyz'
data=defaultdict(int)
H,W=map(int,input().split())
for h in range(H):
    line=input()
    for s in alpha:
        data[s]+=line.count(s)
if H%2==1 and W%2==1:
    one=1
    two=H-1+W-1
    four=H*W-one-two
elif H%2==0 and W%2==0:
    one=0
    two=0
    four=H*W
elif H%2==1 and W%2==0:
    one=0
    two=W
    four=H*W-W
else:
    one=0
    two=H
    four=H*W-H
for d in data.values():
    if d==0:continue
    if d%4==0:
        four-=1
    elif d%2==0:
        two-=1
    else:
        one-=1
if one>=0 and two>=0 and four>=0:
    print('Yes')
else:
    print('No')
#notsub

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = [0 for _ in range(26)]
for i in range(h):
    s = input()
    for j in range(w):
        a[ord(s[j]) - ord('a')] += 1
b = [0, 0, 0]
for i in range(26):
    if a[i] == 0:
        continue
    if a[i] % 4 == 0:
        b[2] += 1
    elif a[i] %2 == 0:
        b[1] += 1
    else:
        b[0] += 1
if h % 2 and w % 2:
    print("Yes" if b[1] <= (h+w-2) // 2 and b[0] == 1 else "No")
if h % 2 and w % 2 == 0:
    print("Yes" if b[1] <= w // 2 and b[0] == 0 else "No")
if h % 2 == 0 and w % 2:
    print("Yes" if b[1] <= h // 2 and b[0] == 0 else "No")
if h % 2 == 0 and w % 2 == 0:
    print("Yes" if b[1] == b[0] == 1 else "No")

Submission status: Wrong Answer

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()
S=''
for i in range(H):
    S+=input()

C=Counter(S)
cnt1=cnt2=0
# 縦横とも偶数
if H%2==0 and W%2==0:
    for k, v in C.items():
        if v%4!=0:
            No()
            exit()
    # 全て4つ組
    Yes()
# 縦偶数
elif H%2==0:
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                cnt2+=1
            else:
                No()
                exit()
    # 2つ組がH/2以下で残りが4つ組
    if cnt2<=H//2:
        Yes()
    else:
        No()
# 横偶数
elif W%2==0:
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                cnt2+=1
            else:
                No()
                exit()
    # 2つ組がW/2以下で残りが4つ組
    if cnt2<=W//2:
        Yes()
    else:
        No()
# 両方奇数
else:
    for k, v in C.items():
        if v%4!=0:
            if v%3==0:
                cnt1+=1
                cnt2+=1
            elif v%2==0:
                cnt2+=1
            else:
                cnt1+=1
    # 1つ組1つ、2つ組が(H+W)/2-1以下で残りが4つ組
    if cnt1==1 and cnt2<=(H+W)//2-1:
        Yes()
    else:
        No()

Submission status: Wrong Answer

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()
S=''
for i in range(H):
    S+=input()

C=Counter(S)
if H%2==0 and W%2==0:
    for k, v in C.items():
        if v%4!=0:
            No()
            exit()
    Yes()
elif H%2==0:
    mod2=0
    for k, v in C.items():
        if v%4!=0:
            if v==2:
                mod2+=1
            else:
                No()
                exit()
    if mod2<=H//2:
        Yes()
    else:
        No()
elif W%2==0:
    mod2=0
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                mod2+=1
            else:
                No()
                exit()
    if mod2<=W//2:
        Yes()
    else:
        No()
else:
    mod2=mod1=0
    for k, v in C.items():
        if v%4!=0:
            if v%2==0:
                mod2+=1
            elif v%3==0:
                mod2+=1
                mod1+=1
            else:
                mod1+=1
    if mod1==1 and mod2<=(H+W)//2-1:
        Yes()
    else:
        No()