Problem:


Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W,*L = open(0).read().split()
H,W = map(int, (H,W))
s = ''
for c in L:
  s += c
Cnt = Counter(s)
g1 = 0
g2 = 0
g3 = 0
if H%2==1 and W%2==1:
  g1 = 1
  g2 = H//2+W//2
  g3 = (H//2)*(W//2)
elif H%2==1:
  g1 = 0
  g2 = W//2
  g3 = (H//2)*(W//2)
elif W%2==1:
  g1 = 0
  g2 = H//2
  g3 = (H//2)*(W//2)
else:
  g1 = 0
  g2 = 0
  g3 = (H//2)*(W//2)

while g3:
  for k in Cnt.keys():
    if Cnt[k]%4==0 and Cnt[k]>0:
      Cnt[k] -= 4
      g3 -= 1
      break
  else:
    break
  
while g2:
  for k in Cnt.keys():
    if Cnt[k]%2==0 and Cnt[k]>0:
      Cnt[k] -= 2
      g2 -= 1
      break
  else:
    break
  
while g1:
  for k in Cnt.keys():
    if Cnt[k]%2==1:
      Cnt[k] -= 1
      g1 -= 1
      break
  else:
    break

if all(Cnt[k]==0 for k in Cnt.keys()):
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W,*L = open(0).read().split()
H,W = map(int, (H,W))
s = ''
for c in L:
  s += c
Cnt = Counter(s)
g1 = 0
g2 = 0
g3 = 0
if H%2==1 and W%2==1:
  g1 = 1
  g2 = H//2+W//2
  g3 = (H//2)*(W//2)
elif H%2==1:
  g1 = 0
  g2 = W//2
  g3 = (H//2)*(W//2)
elif W%2==1:
  g1 = 0
  g2 = H//2
  g3 = (H//2)*(W//2)
else:
  g1 = 0
  g2 = 0
  g3 = (H//2)*(W//2)

while g1:
  for k in Cnt.keys():
    if Cnt[k]%4==1 or Cnt[k]%4==3:
      Cnt[k] -= 1
      g1 -= 1
      break
  else:
    break
while g2:
  for k in Cnt.keys():
    if Cnt[k]%4==2:
      Cnt[k] -= 2
      g2 -= 1
      break
  else:
    break
while g3:
  for k in Cnt.keys():
    if Cnt[k]%4==0 and Cnt[k]>0:
      Cnt[k] -= 4
      g3 -= 1
      break
  else:
    break
if all(Cnt[k]==0 for k in Cnt.keys()):
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
import collections

H, W = map(int, input().split())
A = []
for _ in range(H):
    A += list(input())
elements = collections.Counter([x % 4 for x in collections.Counter(A).values()])
c = [0]*4
for x in elements.keys():
    c[x] = elements[x]

ans = False
if H % 2 == 0 and W % 2 == 0:
    if c[1:] == [0, 0, 0]:
        ans = True

elif H % 2 != 0 and W % 2 != 0:
    if c[1] + c[3] == 1:
        if c[1] == 1:
            c[0] += 1
            c[1] = 0
        else:
            c[2] += 1
            c[3] = 0

        if c[1:] == [0, 0, 0] or c[2] <= (H + W)//2 - 1:
            ans = True 

elif H % 2 != 0:
    if c[1:] == [0, 0, 0] or c[2] <= W//2:
        ans = True
else:
    if c[1:] == [0, 0, 0] or c[2] <= H//2:
        ans = True

if ans:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections

H, W = map(int, input().split())
A = []
for _ in range(H):
    A += list(input())
elements = collections.Counter(A)
c = collections.Counter([x % 4 for x in elements.values()])
keys = sorted(list(c.keys()))

ans = False
if H % 2 == 0 and W % 2 == 0:
    if keys == [0]:
        ans = True

elif H % 2 != 0 and W % 2 != 0:
    if (1 in c.keys() and c[1] == 1) ^ (3 in c.keys() and c[3] == 1):
        if 1 in c.keys():
            c[0] += 1
            keys.pop(keys.index(1))
            c.pop(1)
        else:
            c[2] += 1
            keys.pop(keys.index(3))
            c.pop(3)
        
        if keys == [0] or c[2] <= (H + W)//2 - 1:
            ans = True 

elif H % 2 != 0:
    if keys == [0] or ((keys == [0,2] or keys == [2]) and c[2] <= W//2):
        ans = True
else:
    if keys == [0] or ((keys == [0,2] or keys == [2]) and c[2] <= H//2):
        ans = True

if ans:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections

H, W = map(int, input().split())
A = []
for _ in range(H):
    A += list(input())
elements = collections.Counter(A)
c = collections.Counter([x % 4 for x in elements.values()])
keys = sorted(list(c.keys()))

ans = False
if H % 2 == 0 and W % 2 == 0:
    if keys == [0]:
        ans = True

elif H % 2 != 0 and W % 2 != 0:
    if (1 in c.keys() and c[1] == 1) ^ (3 in c.keys() and c[3] == 1):
        if 1 in c.keys():
            c[0] += 1
            c.pop(1)
        else:
            c[2] += 1
            c.pop(3)
        
        if keys == [0] or c[2] <= (H + W)//2 - 1:
            ans = True 

elif H % 2 != 0:
    if keys == [0] or ((keys == [0,2] or keys == [2]) and c[2] <= W//2):
        ans = True
else:
    if keys == [0] or ((keys == [0,2] or keys == [2]) and c[2] <= H//2):
        ans = True

if ans:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections

H, W = map(int, input().split())
A = []
for _ in range(H):
    A += list(input())
elements = collections.Counter(A)
c = collections.Counter([x % 4 for x in elements.values()])
keys = sorted(list(c.keys()))

ans = False
if H % 2 == 0 and W % 2 == 0:
    if keys == [0]:
        ans = True

elif H % 2 != 0 and W % 2 != 0:
    if (1 in c.keys() and c[1] == 1) ^ (3 in c.keys() and c[3] == 1):
        if 1 in c.keys():
            c[0] += 1
            c.pop(1)
        else:
            c[2] += 1
            c.pop(3)
        
        if keys == [0] or c[2] <= (H + W)//2 - 1:
            ans = True 

elif H % 2 != 0:
    if keys == [0] or (keys == [0,2] and c[2] <= W//2):
        ans = True
else:
    if keys == [0] or (keys == [0,2] and c[2] <= H//2):
        ans = True

if ans:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections

H, W = map(int, input().split())
A = []
for _ in range(H):
    A += list(input())
elements = collections.Counter(A)
c = collections.Counter([x % 4 for x in elements.values()])

ans = False
if H % 2 == 0 and W % 2 == 0:
    if c.keys() == [0]:
        ans = True

elif H % 2 != 0 and W % 2 != 0:
    if (1 in c.keys() and c[1] == 1) ^ (3 in c.keys() and c[3] == 1):
        if 1 in c.keys():
            c[0] += 1
            c.pop(1)
        else:
            c[2] += 1
            c.pop(3)
        
        if c.keys() == [0] or c[2] <= (H + W)//2 - 1:
            ans = True 

elif H % 2 != 0:
    if c.keys() == [0] or (sorted(list(c.keys())) == [0,2] and c[2] <= W//2):
        ans = True
else:
    if c.keys() == [0] or (sorted(list(c.keys())) == [0,2] and c[2] <= H//2):
        ans = True


if ans:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
a = [input() for i in range(H)]
C = defaultdict(int)
for s in a:
    for c in s:
        C[c] += 1
m = [0]*4
for k in C:
    m[C[k]%4] += 1
if H&1 and W&1:
    if m[1]+m[3] == 1 and m[2] <= H//2+W//2-m[3]:
        print('Yes')
    else:
        print('No')
elif H&1==0 and W&1==0:
    if all([C[x]%4==0 for x in C]):
        print('Yes')
    else:
        print('No')
else:
    if H&1:
        H, W = W, H
    if m[1]+m[3] < 2:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
D = []
for _ in range(H):
    D.extend(list([v for v in input()]))
C = Counter(D)
L = list([v for i, v in C.most_common()])

g1 = sum(v % 2 for v in L)
g4 = sum(v//4 for v in L)

if g4 >= (W-(W % 2))*(H-(H % 2)):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
D = []
for _ in range(H):
    D.extend(list([v for v in input()]))
C = Counter(D)
L = list([v for i, v in C.most_common()])

g1 = sum(v % 2 for v in L)
g4 = sum(v//4 for v in L)

if g4 <= (W-(W % 2))*(H-(H % 2)):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
D = []
for _ in range(H):
    D.extend(list([v for v in input()]))
C = Counter(D)
L = list([v for i, v in C.most_common()])
# print(C)
if H == 1 and W == 1:
    ans = "Yes"
elif H == 1 or W == 1:
    if sum([1 if v % 2 == 1 else 0 for v in L]) == 1:
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 1 and W % 2 == 1:
    if sum(v % 2 for v in L) == 1 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 0 and W % 2 == 0:
    if all(v % 4 == 0 for v in L):
        ans = "Yes"
    else:
        ans = "No"
else:
    if sum(v % 2 for v in L) == 0 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
D = []
for _ in range(H):
    D.extend(list([v for v in input()]))
C = Counter(D)
L = list([v for i, v in C.most_common()])
# print(C)
if H == 1 and W == 1:
    ans = "Yes"
elif H == 1 or W == 1:
    if sum([1 if v % 2 == 1 else 0 for v in L]) == 1:
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 1 and W % 2 == 1:
    if sum(v % 2 for v in L) == 1 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 0 and W % 2 == 0:
    ans = "Un"
else:
    if sum(v % 2 for v in L) == 0 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
D = []
for _ in range(H):
    D.extend(list([v for v in input()]))
C = Counter(D)
L = list([v for i, v in C.most_common()])
print(C)
if H == 1 and W == 1:
    ans = "Yes"
elif H == 1 or W == 1:
    if sum([1 if v % 2 == 1 else 0 for v in L]) == 1:
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 1 and W % 2 == 1:
    if sum(v % 2 for v in L) == 1 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
elif H % 2 == 0 and W % 2 == 0:
    ans = "Un"
else:
    if sum(v % 2 for v in L) == 0 and (sum(1 if v % 4 == 0 else 0 for v in L) == len(L)-1 or (sum(1 if v % 4 == 0 else 0 for v in L)) == len(L)-2):
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()

    if ans in [True, False]:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    H, W = iip(False)

    A = []
    for i in range(H):
        A.extend([i for i in input()])

    d = count_elements(A)

    a = 0
    b = 0
    for i in d.values():
        if i % 4 == 1:
            a += 1
        elif i % 4 == 2:
            b += 1
        elif i % 4 == 3:
            a += 1
            b += 1

    ans = True

    h = H % 2 == 0
    w = W % 2 == 0

    if h and w:
        if a == 0 and b == 0:
            return True
        else:
            return False

    elif not h:
        if b <= W/2 and a == 0:
            return True
        else:
            return False

    elif not w:
        if b <= H/2 and a == 0:
            return True
        else:
            return False

    else:
        if b <= (H+W)/2 - 1 and a <= 1:
            return True
        else:
            return False


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()

    if ans in [True, False]:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    H, W = iip(False)

    A = []
    for i in range(H):
        A.extend([i for i in input()])

    d = count_elements(A)

    a = 0
    b = 0
    for i in d.values():
        if i % 4 == 1:
            a += 1
        elif i % 4 == 2:
            b += 1
        elif i % 4 == 3:
            a += 1
            b += 1

    ans = True

    h = H % 2 == 0
    w = W % 2 == 0

    if h and w:
        if a == 0 and b == 0:
            return True
        else:
            return False

    elif not h:
        if b <= W/2 and a == 0:
            return True
        else:
            return False

    elif not w:
        if b <= H/2 and a == 0:
            return True
        else:
            return False

    else:
        if b <= (H+W)/2 + 1 and a <= 1:
            return True
        else:
            return False


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()

    if ans in [True, False]:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    H, W = iip(False)

    A = []
    for i in range(H):
        A.extend([i for i in input()])

    d = count_elements(A)

    a = 0
    b = 0
    for i in d.values():
        if i % 4 == 1:
            a += 1
        elif i % 4 == 2:
            b += 1
        elif i % 4 == 3:
            a += 1
            b += 1

    ans = True

    h = H % 2 == 0
    w = W % 2 == 0

    if h and w:
        if a == 0 or b == 0:
            return True
        else:
            return False

    elif not h:
        if b <= W/2 and a == 0:
            return True
        else:
            return False

    elif not w:
        if b <= H/2 and a == 0:
            return True
        else:
            return False

    else:
        if b <= (H+W)/2 + 1 and a <= 1:
            return True
        else:
            return False


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()

    if ans in [True, False]:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    H, W = iip(False)

    A = []
    for i in range(H):
        A.extend([i for i in input()])

    d = count_elements(A)

    a = 0
    b = 0
    for i in d.values():
        if i % 4 == 1:
            a += 1
        elif i % 4 == 2:
            b += 1
        elif i % 4 == 3:
            a += 1
            b += 1

    ans = True

    h = H % 2 == 0
    w = W % 2 == 0

    if h and w:
        if a >= 1 or b >= 1:
            return False
        else:
            return True

    elif not H:
        if b <= W/2 and a == 0:
            return True
        else:
            return False

    elif not W:
        if b <= H/2 and a == 0:
            return True
        else:
            return False

    else:
        if b <= (H+W)/2 + 1 and a <= 1:
            return True
        else:
            return False


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()