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

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1 = 1 if w % 2 == 1 and h % 2 == 1 else 0
    g2, g4 = int(h/2) + int(w/2), int(h/2) * int(w/2)
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1, g2, g4 = 1, int(h/2) + int(w/2), int(h/2) * int(w/2)
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if (v % 4 == 1 or v % 4 == 3) and v >= 1:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2 and v >= 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0 and v >= 4:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    h, w = map(int, input().split())
    al = [input() for _ in range(h)]
    word_dict = Counter(list("".join(al)))

    g1, g2, g4 = 1, int(h/2) + int(w/2), int(h/2) * int(w/2)
    for _ in range(g1):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 1 or v % 4 == 3:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 1
    
    for _ in range(g2):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 2:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 2
    
    for _ in range(g4):
        key = 0
        for k, v in word_dict.most_common():
            if v % 4 == 0:
                key = k
                break
        if key is not 0:
            word_dict[key] -= 4
    
    print("Yes" if len([k for k, v in word_dict.most_common() if v != 0]) == 0 else "No")
    
    
if __name__=="__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

[h,w]=i2()
a=""
for i in range(h):
    a+=input()
from collections import Counter
c=Counter(list(a))
c=list(c.values())
b2=0
b4=0
b1=0
if h%2==1 and w%2==0:
 b2=w//2
if w%2==1 and h%2==0:
 b2=h//2
if w%2==1 and h%2==1:
 b1=1
 b2=w//2+h//2
s="Yes"
b4=(h*w-b2*2)//4
for i in c:
    if i%4==0:
     if b4-i//4>=0:
        b4-=i//4
     elif b2-(i-4*b4)//2>=0:
        b2-=(i-4*b4)//2
        b4=0
     else:
        s="No"
    elif i%2==0:
      if i>=4:
          if b4-i//4>=0:
             b4-=i//4
             if b2-i%4//2>=0:
                 b2-=i%4//2
             else:
                 s="No"
          elif b2-i//2>=0:
             b2-=i//2
          else:
             s="No"
      elif i==2 and b2>=1:
          b2-=1
      else:
          s="No"
    else:
        if b1==1:
          t=i-1
          if t>0:
            if t%4==0:
             if b4-t//4>=0:
                b4-=t//4
             elif b2-(t-b4*4)//2>=0:
                t-=4*b4
                b4=0
                b2-=t//2
             else:
                s="No"
            elif t%2==0:
              if t>=4:
                  if b4-t//4>=0:
                     b4-=t//4
                     if b2-t%4//2>=0:
                         b2-=t%4//2
                     else:
                         s="No"
                  elif b2-(t-b4*4)//2>=0:
                     t-=4*b4
                     b4=0
                     b2-=t//2
                  else:
                     s="No"
              elif t==2 and b2>=1:
                  b2-=1
              else:
                  s="No"

        else:
           s="No"
print(s)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

[h,w]=i2()
a=""
for i in range(h):
    a+=input()
from collections import Counter
c=Counter(list(a))
c=list(c.values())
b2=0
b4=0
b1=0
if h%2==1 and w%2==0:
 b2=w//2
if w%2==1 and h%2==0:
 b2=h//2
if w%2==1 and h%2==1:
 b1=1
 b2=w//2+h//2
s="Yes"
b4=(h*w-b2*2)//4
for i in c:
    if i%4==0:
     if b4-i//4>=0:
        b4-=i//4
     elif b2-(i-4*b4)//2>=0:
        b2-=(i-4*b4)//2
        b4=0
     else:
        s="No"
    elif i%2==0:
      if i>=4:
          if b4-i//4>=0:
             b4-=i//4
             if b2-i%4//2>=0:
                 b2-=i%4//2
             else:
                 s="No"
          elif b2-i//2>=0:
             b2-=i//2
          else:
             s="No"
      elif i==2 and b2>=1:
          b2-=1
      else:
          s="No"
    else:
        if b1==1:
          t=i-1
          if t>0:
            if t%4==0:
             if b4-t//4>=0:
                b4-=t//4
             elif b2-(t-b4*4)//2>=0:
                t-=4*b4
                b4=0
                b2-=t//2
             else:
                s="No"
            elif t%2==0:
              if t>=4:
                  if b4-t//4>=0:
                     b4-=t//4
                     if b2-t%4//2>=0:
                         b2-=t%4//2
                     else:
                         s="No"
                  elif b2-t//2>=0:
                     b2-=t//2
                  else:
                     s="No"
              elif t==2 and b2>=1:
                  b2-=1
              else:
                  s="No"

        else:
           s="No"
print(s)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

[h,w]=i2()
a=""
for i in range(h):
    a+=input()
from collections import Counter
c=Counter(list(a))
c=list(c.values())
b2=0
b4=0
b1=0
if h%2==1 and w%2==0:
 b2=w//2
if w%2==1 and h%2==0:
 b2=h//2
if w%2==1 and h%2==1:
 b1=1
 b2=w//2+h//2
s="Yes"
b4=(h*w-b2*2)//4
for i in c:
    if i%4==0:
     if b4-i//4>=0:
        b4-=i//4
     elif b2-i//2>=0:
        b2-=i//2
     else:
        s="No"
    elif i%2==0:
      if i>=4:
          if b4-i//4>=0:
             b4-=i//4
             if b2-i%4//2>=0:
                 b2-=i%4//2
             else:
                 s="No"
          elif b2-i//2>=0:
             b2-=i//2
          else:
             s="No"
      elif i==2 and b2>=1:
          b2-=1
      else:
          s="No"
    else:
        if b1==1:
          t=i-1
          if t>0:
            if t%4==0:
             if b4-t//4>=0:
                b4-=t//4
             elif b2-t//2>=0:
                b2-=t//2
             else:
                s="No"
            elif t%2==0:
              if t>=4:
                  if b4-t//4>=0:
                     b4-=t//4
                     if b2-t%4//2>=0:
                         b2-=t%4//2
                     else:
                         s="No"
                  elif b2-t//2>=0:
                     b2-=t//2
                  else:
                     s="No"
              elif t==2 and b2>=1:
                  b2-=1
              else:
                  s="No"

        else:
           s="No"
print(s)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

[h,w]=i2()
a=""
for i in range(h):
    a+=input()
from collections import Counter
c=Counter(list(a))
c=list(c.values())
b2=0
b4=0
b1=0
if h==1 or w==1:
    b1=1
if h%2==1 and w%2==0:
 b2=w//2
if w%2==1 and h%2==0:
 b2=h//2
if w%2==1 and h%2==1:
 b1=1
 b2=w//2+h//2
s="Yes"
b4=(h*w-b2*2)//4
for i in c:
    if i%4==0:
     if b4-i//4>=0:
        b4-=i//4
     elif b2-i//2>=0:
        b2-=i//2
     else:
        s="No"
    elif i%2==0:
      if i>=4:
          if b4-i//4>=0:
             b4-=i//4
             if b2-i%4//2>=0:
                 b2-=i%4//2
             else:
                 s="No"
          elif b2-i//2>=0:
             b2-=i//2
          else:
             s="No"
      elif i==2 and b2>=1:
          b2-=1
      else:
          s="No"
    else:
        if b1==1:
          t=i-1
          if t>0:
            if t%4==0:
             if b4-t//4>=0:
                b4-=t//4
             elif b2-t//2>=0:
                b2-=t//2
             else:
                s="No"
            elif t%2==0:
              if t>=4:
                  if b4-t//4>=0:
                     b4-=t//4
                     if b2-t%4//2>=0:
                         b2-=t%4//2
                     else:
                         s="No"
                  elif b2-t//2>=0:
                     b2-=t//2
                  else:
                     s="No"
              elif t==2 and b2>=1:
                  b2-=1
              else:
                  s="No"

        else:
           s="No"
print(s)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

## in horizon, set(strings)->decide palindromic
## in vertical, set(strings)->decide palindromic
## many even,one odd
[h,w]=i2()
a=""
for i in range(h):
    a+=input()
from collections import Counter
c=Counter(list(a))
c=list(c.values())
b2=0
b4=0
b1=0
if h==1 or w==1:
    b1=1
if h%2==1 and w%2==0:
 b2=w//2
if w%2==1 and h%2==0:
 b2=h//2
if w%2==1 and h%2==1:
 b2+=2
s="Yes"
b4=(h*w-b2*2)//4
for i in c:
    if i%4==0:
     if b4-i//4>=0:
        b4-=i//4
     elif b2-i//2>=0:
        b2-=i//2
     else:
        s="No"
    elif i%2==0:
      if i>=4:
          if b4-i//4>=0:
             b4-=i//4
             if b2-i%4//2>=0:
                 b2-=i%4//2
             else:
                 s="No"
          elif b2-i//2>=0:
             b2-=i//2
          else:
             s="No"
      elif i==2 and b2>=1:
          b2-=1
      else:
          s="No"
    else:
        if b1==1:
          t=i-1
          if t>0:
            if t%4==0:
             if b4-t//4>=0:
                b4-=t//4
             elif b2-t//2>=0:
                b2-=t//2
             else:
                s="No"
            elif t%2==0:
              if t>=4:
                  if b4-t//4>=0:
                     b4-=t//4
                     if b2-t%4//2>=0:
                         b2-=t%4//2
                     else:
                         s="No"
                  elif b2-t//2>=0:
                     b2-=t//2
                  else:
                     s="No"
              elif t==2 and b2>=1:
                  b2-=1
              else:
                  s="No"

        else:
           s="No"
print(s)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().strip().split())
lines = []
for i in range(H):
    lines.append(input().strip())
chars = {}
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=0
        chars[c]+=1
print(chars)

def judge(H,W,chars):
    if H%2 + W%2==2:
        ones = 1
        twos = (H + W - 2)//2
    elif H%2 + W%2==0:
        ones = 0
        twos = 0
    elif H%2 == 1:
        ones = 0
        twos = W//2
    else:
        ones = 0
        twos = H//2
    print(ones,twos)
    for c in chars:
        if ones<0 or twos<0:
            return False
        chars[c] %= 4
        if chars[c]==0:
            continue
        if chars[c] == 2:
            twos-=1
        elif chars[c] == 3:
            twos-=1
            ones-=1
        else:
            ones-=1
    return True


if judge(H, W, chars):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().strip().split())
lines = []
for i in range(H):
    lines.append(input().strip())
chars = {}
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=1
        chars[c]+=1


def judge(H,W,chars):
    if H%2 + W%2==2:
        ones = 1
        twos = (H + W - 2)//2
    elif H%2 + W%2==0:
        ones = 0
        twos = 0
    elif H%2 == 1:
        ones = 0
        twos = W//2
    else:
        ones = 0
        twos = H//2
    
    for c in chars:
        if ones<0 or twos<0:
            return False
        chars[c] %= 4
        if chars[c]==0:
            continue
        if chars[c] == 2:
            twos-=1
        elif chars[c] == 3:
            twos-=1
            ones-=1
        else:
            ones-=1
    return True


if judge(H, W, chars):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().strip().split())
lines = []
for i in range(H):
    lines.append(input().strip())
chars = {}
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=1
        chars[c]+=1


def judge(H,W,chars):
    ones = 1
    twos = H+W-2
    for c in chars:
        if ones<0 or twos<0:
            return False
        if chars[c]%4==0:
            continue
        chars[c] %=4
        if chars[c] == 2:
            twos-=1
        elif chars[c] == 3:
            twos-=1
            ones-=1
        else:
            ones-=1
    return True


if judge(H, W, chars):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().strip().split())
lines = []
for i in range(H):
    lines.append(input().strip())

chars = {}
for line in lines:
    for c in line:
        if c not in chars:
            chars[c]=1
        chars[c]+=1
def judge(H,W,chars):
    ones = 1
    twos = H+W-2
    for c in chars:
        if ones<0 or twos<0:
            return False
        if chars[c]%4==0:
            continue
        if chars[c]%2==0:
            twos-=1
        else:
            ones-=1
    return True


if judge(H, W, chars):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import math, sys
h,w = map(int, input().split())
a = [ list(input()) for y in range(h) ]
table = {}
table2 = []
for y in range(h):
    for x in range(w):
        if a[y][x] in table:
            table[a[y][x]] += 1
        else:
            table[a[y][x]] = 1
            table2.append(a[y][x])

size2 = math.floor(h/2)+math.floor(w/2)
size4 = math.floor(h/2)*math.floor(w/2)

for tmp in table2:
    if table[tmp]%4 == 1 or table[tmp]%4 == 3:
        table[tmp] -= 1
        break

for tmp in table2:
    if table[tmp]%4 == 2:
        table[tmp] -= 2
        size2 -= 1
        if size2 == 0:
            break

for tmp in table2:
    if table[tmp]%4 == 0 and table[tmp] != 0:
        table[tmp] -= 4
        size4 -= 1
        if size4 == 0:
            break

for tmp in table2:
    if table[tmp] != 0:
        print("No")
        sys.exit()
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for v in c.values():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return o == 0 and f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

def solve(H, W, c):
    ok = True
    if H == W == 1:
        pass
    elif H == 1 or W == 1:
        HW = H * W
        odd_num = sum(1 for x in c.values() if x % 2)
        even_num = HW - odd_num
        # print("odd, even = ", odd_num, even_num)
        if HW % 2 == 1 and odd_num != 1: ok = False
        elif HW % 2 == 0 and odd_num != 0: ok = False
    elif H % 2 == W % 2 == 0:
        # all 4
        for v in c.values():
            if v % 4 != 0:
                ok = False
                break
    elif H % 2 == W % 2 == 1:
        two_num = H + W - 2
        one_num = 1
        four_num = H * W - two_num - one_num
        if not valid(two_num, four_num, one_num, c):
            ok = False
    else:
        if W % 2 == 0:
            two_num = W
        else:
            two_num = H
        four_num = H * W - two_num
        if not valid(two_num, four_num, 0, c):
            ok = False
        
    return ok

if solve(H, W, c):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for v in c.values():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return o == 0 and f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

def solve(H, W, c):
    ok = True
    if H == W == 1:
        pass
    elif H == 1 or W == 1:
        HW = H * W
        odd_num = sum(1 for x in c.values() if x % 2)
        even_num = HW - odd_num
        # print("odd, even = ", odd_num, even_num)
        if HW % 2 == 1 and odd_num != 1: ok = False
        elif HW % 2 == 0 and odd_num != 0: ok = False
    elif H % 2 == W % 2 == 0:
        # all 4
        for v in c.values():
            if v % 4 != 0:
                ok = False
                break
    elif H % 2 == W % 2 == 1:
        two_num = H + W - 2
        one_num = 1
        four_num = H * W - two_num - one_num
        if not valid(two_num, four_num, one_num, c):
            ok = False
    else:
        if W % 2 == 0:
            two_num = W
        else:
            two_num = H
        four_num = H * W - two_num
        if not valid(two_num, four_num, 0, c):
            ok = False
        
    return ok

if solve(H, W, c):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for v in c.values():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return o == 0 and f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

def solve(H, W, c):
    ok = True
    if H == W == 1:
        pass
    elif H == 1 or W == 1:
        HW = H * W
        odd_num = sum(1 for x in c.values() if x % 2)
        even_num = HW - odd_num
        # print("odd, even = ", odd_num, even_num)
        if HW % 2 == 1 and odd_num != 1: ok = False
        elif HW % 2 == 0 and odd_num != 0: ok = False
    elif H % 2 == W % 2 == 0:
        # all 4
        for v in c.values():
            if v % 4 != 0:
                ok = False
                break
    elif H % 2 == W % 2 == 1:
        two_num = H + W - 2
        one_num = 1
        four_num = H * W - two_num - one_num
        if not valid(two_num, four_num, one_num, c):
            ok = False
    else:
        if W % 2 == 0:
            two_num = W
        else:
            two_num = H
        four_num = H * W - two_num
        if not valid(two_num, four_num, 0, c):
            ok = False
        
    return ok

if solve(H, W, c):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for v in c.values():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return o == 0 and f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

def solve(H, W, c):
    ok = True
    if H == W == 1:
        pass
    elif H == 1 or W == 1:
        HW = H * W
        odd_num = sum(1 for x in c.values() if x % 2)
        even_num = HW - odd_num
        # print("odd, even = ", odd_num, even_num)
        if HW % 2 == 1 and odd_num != 1: ok = False
        elif HW % 2 == 0 and odd_num != 0: ok = False
    elif H % 2 == W % 2 == 0:
        # all 4
        for v in c.values():
            if v % 4 != 0:
                ok = False
                break
    elif H % 2 == W % 2 == 1:
        two_num = H + W - 2
        one_num = 1
        four_num = H * W - two_num - one_num
        if not valid(two_num, four_num, one_num, c):
            ok = False
    else:
        if W % 2 == 0:
            two_num = W
        else:
            two_num = H
        four_num = H * W - two_num
        if not valid(two_num, four_num, 0, c):
            ok = False
        
    return ok

if solve(H, W, c):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

target4 = int(H / 2) * int(W / 2)
if H % 2 == 0:
    if W % 2 == 0:
        target2 = 0
    else:
        target2 = int(H / 2)
else:
    if W % 2 == 0:
        target2 = int(W / 2)
    else:
        target2 = max(int(W / 2), int(H / 2))
target1 = 1 if (H % 2 == 1 and W % 2 == 1) else 0

a = [input() for _ in range(H)]

flag = False

count1 = 0
count2 = 0
count4 = 0
for i in range(26):
    count = sum([a[h].count(chr(97 + i)) for h in range(H)])
    count4 += int(count / 4)
    # count2 += int((count - int(count / 4) * 4) / 2)
    count2 += (count % 4) // 2
    count1 += count % 2

if target1 != count1:
    print('No')
elif target2 < count2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

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

A = []
if W==1:
    for h in range(H):
        A.append(input())
else:
    for h in range(H):
        for w in list(input()):
            A.append(w)

from collections import Counter
data = Counter(A)

ok = True
if W%2==1 and H%2==1:
    flag_4 = False
    get_quad = 0
    for i in data:
        # can get 4
        while data[i]//4 >= 1 and flag_4 == False:
            # want 4
            if get_quad < (W-1)*(H-1) / 4:
                data[i] -=4
                get_quad += 1
            if get_quad == (W-1)*(H-1) / 4:
                flag_4 = True
    if flag_4 == False:
        print("No")
        ok= False
        
    get_double = 0
    flag_2 = False
    for i in data:
        # can get 2
        while data[i]//2 >= 1 and flag_2 == False:
            # want 4
            if get_quad < (W+H-2) / 2:
                data[i] -=2
                get_double += 1
            if get_quad == (W+H-2) / 2:
                flag_2 = True
                
    if flag_2 == False:
        print("No")
        ok= False
    
elif W%2==1 and H%2==0:
    flag_4 = False
    get_quad = 0
    for i in data:
        # can get 4
        while data[i]//4 >= 1 and flag_4 == False:
            # want 4
            if get_quad < (W-1)*(H) / 4:
                data[i] -=4
                get_quad += 1
            if get_quad == (W-1)*(H) / 4:
                flag_4 = True
    if flag_4 == False:
        print("No")
        ok= False
        
    get_double = 0
    flag_2 = False
    for i in data:
        # can get 2
        while data[i]//2 >= 1 and flag_2 == False:
            # want 4
            if get_quad < (H) / 2:
                data[i] -=2
                get_double += 1
            if get_quad == (H) / 2:
                flag_2 = True
                
    if flag_2 == False:
        print("No")
        ok= False
elif W%2==0 and H%2==1:
    flag_4 = False
    get_quad = 0
    for i in data:
        # can get 4
        while data[i]//4 >= 1 and flag_4 == False:
            # want 4
            if get_quad < (H-1)*(W) / 4:
                data[i] -=4
                get_quad += 1
            if get_quad == (H-1)*(W) / 4:
                flag_4 = True
    if flag_4 == False:
        print("No")
        ok= False
        
    get_double = 0
    flag_2 = False
    for i in data:
        # can get 2
        while data[i]//2 >= 1 and flag_2 == False:
            # want 4
            if get_quad < (W) / 2:
                data[i] -=2
                get_double += 1
            if get_quad == (W) / 2:
                flag_2 = True
                
    if flag_2 == False:
        print("No")
        ok= False
    
elif W%2==0 and H%2==0:
    flag_4 = False
    get_quad = 0
    for i in data:
        # can get 4
        while data[i]//4 >= 1 and flag_4 == False:
            # want 4
            if get_quad < (H)*(W) / 4:
                data[i] -=4
                get_quad += 1
            if get_quad == (H-1)*(W) / 4:
                flag_4 = True
    if flag_4 == False:
        print("No")
        ok= False
        
    get_double = 0
    flag_2 = False
    for i in data:
        # can get 2
        while data[i]//2 >= 1 and flag_2 == False:
            # want 4
            if get_quad < (W) / 2:
                data[i] -=2
                get_double += 1
            if get_quad == (W) / 2:
                flag_2 = True
                
    if flag_2 == False:
        print("No")
        ok= False
if ok:
    print("Yes")

Submission status: Wrong Answer

Solution code:
hoge = [int(x) for x in input().split()]
moji = ''
char = {}
for i in range(hoge[0]):
    moji += input()
for m in moji:
    if m in char:
        char[m] += 1
    else:
        char.update({m:1})
if hoge[0] % 2 == 1 and hoge[1] % 2 == 1:
    one = 1
    two = (hoge[0]-1)/2 + (hoge[1]-1)/2
    four = (hoge[0]-1)/2 * (hoge[1]-1)/2
elif hoge[0] % 2 == 1 and hoge[1] % 2 == 0:
    one = 0
    two = hoge[1]/2
    four = (hoge[0]-1)/2 * hoge[1]/2
elif hoge[0] % 2 == 0 and hoge[1] % 2 == 1:
    one = 0
    two = hoge[0]/2
    four = (hoge[1]-1)/2 * hoge[0]/2
else:
    one = 0
    two = 0
    four = hoge[1]/2 * hoge[0]/2

foo = 0
for c in char:
    while(char[c] > 0):
        if char[c] >= 4:
            char[c] -= 4
            four -= 1
        elif char[c] >= 2:
            char[c] -= 2
            two -= 1
        else:
            char[c] -= 1
            one -= 1

if four < 0 or two < 0 or one < 0:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

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

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    kisuu = 0
    kisugusu = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1
        if a % 2 == 1:
            kisuu += 1
        if a % 2 == 0:
            tmp = a / 2
            while(tmp != 1):
                if tmp % 2 == 1:
                    kisugusu += 1
                    break   
                tmp /= 2             

    if one > 1 or ((H * W) % 2 == 1 and (one == 0 and kisugusu == 0)):
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            if kisuu > 1:
                print('No')
            else:
                if H % 2 == 0 and W % 2 == 0 and kisugusu > 0:
                    print('No')
                else:
                    print('Yes')
else:
    print('No')