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:
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 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
    for i in d.values():
        if i % 2 == 1:
            a += 1

    if W % 2 == 0:
        if a == 0:
            ans = True
        else:
            ans = False

    if W % 2 == 1:
        if a == H:
            ans = True
        else:
            ans = False

    YesNo(ans)



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

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:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


# 回文の二次元版
# 1次元回文→文字数が奇数だったら,一つの文字以外出現回数はすべて2の倍数。偶数だったら出現回数はすべて2の倍数
# 二次元では？
# H,Wがともに偶数なら→すべての文字の出現回数は4の倍数
# Hが奇数、Wが偶数なら→4の倍数でない要素を、真ん中に詰めていきたい(そのような要素がWについても回文になっているか評価)
# つまり4の倍数が(W*(H-1))個分ある かつ それ以外の要素は2の倍数ある
# Wが奇数、Hが偶数→↑WHをひっくり返すだけ
# W,Hがともに奇数→(W-1)*(H-1)個分ある かつ それ以外の要素は1つ以外2の倍数である

# 上記を一般化すると、二次元回文が満たすべき条件は、
# - 同一文字4個セットは(W-(W&1))*(H-(H&1))//4 個以上ある
# - 奇数個の要素はちょうどH&W個ある
# - その他の要素はすべて2の倍数である(ただしこの条件は2番めの条件に内包される)

from collections import Counter

H, W = read_ints()
cnt = Counter()
for _ in range(H):
    cnt.update(input())

num4 = 0
num1 = 0
for v in cnt.values():
    if v & 1:  # 条件2
        num1 += 1

    while v >= 4:  # 条件1の4文字セットを作る 4以上なら4個セットを作ることができる
        v -= 4
        num4 += 1

# print(num4, num1)
if num4 >= (W - (W & 1)) * (H - (H & 1)) // 4 and num1 == H & W:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


# 回文の二次元版
# 1次元回文→文字数が奇数だったら,一つの文字以外出現回数はすべて2の倍数。偶数だったら出現回数はすべて2の倍数
# 二次元では？
# H,Wがともに偶数なら→すべての文字の出現回数は4の倍数
# Hが奇数、Wが偶数なら→4の倍数でない要素を、真ん中に詰めていきたい(そのような要素がWについても回文になっているか評価)
# つまり4の倍数が(W*(H-1))個分ある かつ それ以外の要素は2の倍数ある
# Wが奇数、Hが偶数→↑WHをひっくり返すだけ
# W,Hがともに奇数→(W-1)*(H-1)個分ある かつ それ以外の要素は1つ以外2の倍数である

# 上記を一般化すると、二次元回文が満たすべき条件は、
# - 同一文字4個セットは(W-(W&1))*(H-(H&1))//4 個ある
# - 奇数個の要素ははH&W個ある
# - その他の要素はすべて2の倍数である(ただしこの条件は2番めの条件に内包される)

from collections import Counter

H, W = read_ints()
cnt = Counter()
for _ in range(H):
    cnt.update(input())

num4 = 0
num1 = 0
for v in cnt.values():
    if v % 2 == 0:
        while v > 2:
            v -= 4
            num4 += 1
    else:
        num1 += 1
# print(num4, num1)
if num4 >= (W - (W & 1)) * (H - (H & 1)) // 4 and num1 == H & W:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


# 回文の二次元版
# 1次元回文→文字数が奇数だったら,一つの文字以外出現回数はすべて2の倍数。偶数だったら出現回数はすべて2の倍数
# 二次元では？
# H,Wがともに偶数なら→すべての文字の出現回数は4の倍数
# Hが奇数、Wが偶数なら→4の倍数でない要素を、真ん中に詰めていきたい(そのような要素がWについても回文になっているか評価)
# つまり4の倍数が(W*(H-1))個分ある かつ それ以外の要素は2の倍数ある
# Wが奇数、Hが偶数→↑WHをひっくり返すだけ
# W,Hがともに奇数→(W-1)*(H-1)個分ある かつ それ以外の要素は1つ以外2の倍数である

# 上記を一般化すると、二次元回文が満たすべき条件は、
# - 同一文字4個セットは(W-(W&1))*(H-(H&1))//4 個ある
# - 奇数個の要素ははH&W個ある
# - その他の要素はすべて2の倍数である(ただしこの条件は2番めの条件に内包される)

from collections import Counter

H, W = read_ints()
cnt = Counter()
for _ in range(H):
    cnt.update(input())

num4 = 0
num1 = 0
for v in cnt.values():
    if v % 2 == 0:
        while v > 2:
            v -= 4
            num4 += 1
    else:
        num1 += 1
# print(num4, num1)
if num4 == (W - (W & 1)) * (H - (H & 1)) // 4 and num1 == H & W:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l="".join([input() for i in range(h)])
from collections import Counter as co
amari=[0,0,0,0]
for i in co(l).values():
    amari[i%4]+=i%4
if h%2 and w%2:
    if amari[1]>=2:print("No")
    elif amari[1]==1:
        if amari[2]==(h+w-2):print("Yes")
        else:print("No") 
    elif amari[3]==3 and amari[2]+2==(h+w-2):print("Yes")
    else:print("No")
elif (h+w)%2:
    if amari[2]==sum(amari) and (w if h%2 else h)-1==amari[2]:print("Yes")
    else:print("No")
else:print("No") if sum(amari) else print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l="".join([input() for i in range(h)])
from collections import Counter as co
amari=[0,0,0,0]
for i in co(l).values():
    if i%4:amari[i%4]+=1
if h%2 and w%2:
    if amari[1]>=2:print("No")
    elif amari[1]==1:
        if amari[2]<=(h+w-2) and (h+w)%2==amari[2]%2:print("Yes")
        else:print("No") 
    elif amari[3]==1 and amari[2]<=(h+w-2) and (h+w)%2==amari[2]%2:print("Yes")
    else:print("No")
elif (h+w)%2:
    if amari[2]==sum(amari) and (w if h%2 else h)>=amari[2] and (w if h%2 else h)%2==amari[2]%2:print("Yes")
    else:print("No")
else:print("No") if sum(amari) else print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=["".join(sorted(input())) for i in range(h)]
from collections import Counter as co
odd=0
for i in co(l).values():
    odd+=i%2
if h%2!=odd:print("No");exit()
for j in set(l):
    odd=0
    for v in co(j).values():
        odd+=v%2
    if w%2!=odd:print("No");break
else:print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=["".join(sorted(input())) for i in range(h)]
from itertools import groupby as gb
odd=0
for i in [len(list(b)) for a,b in gb(l)]:
    odd+=i%2
if h%2!=odd:print("No");exit()
for j in set(l):
    odd=0
    for v in [len(list(b)) for a,b in gb(j)]:
        odd+=v%2
    if w%2!=odd:print("No");break
else:print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=["".join(sorted(input())) for i in range(h)]
from collections import Counter as co
odd=0
for i in co(l).values():
    if i%2:odd+=1
if odd>=2 or h%2!=odd:print("No");exit()
for j in set(l):
    odd=0
    for v in co(j).values():
        odd+=v%2
    if odd>=2 or w%2!=odd:print("No");exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
# CODE FESTIVAL 2017 qual A
# C - Palindromic Matrix
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import Counter
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
S = [input()[:-1] for _ in range(H)]
C = Counter(''.join(S))

a = H//2*W//2
b = H//2*(W % 2) + W//2*(H % 2)
c = (H % 2)*(W % 2)


for _ in range(c):
    for k, v in C.items():
        if v % 2 == 1 and v > 0:
            C[k] -= 1
            c -= 1
b += c//2
for _ in range(b):
    for k, v in C.items():
        if v % 4 == 2 and v > 0:
            C[k] -= 2
            b -= 1
a += b//2
for _ in range(a):
    for k, v in C.items():
        if v % 4 == 0 and v > 0:
            C[k] -= 4


if sum(C.values()) == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# CODE FESTIVAL 2017 qual A
# C - Palindromic Matrix
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import Counter
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
S = [input()[:-1] for _ in range(H)]
C = Counter(''.join(S))

a = H//2*W//2
b = H//2*(W%2) + W//2*(H%2)
c = (H % 2)*(W % 2)

for _ in range(c):
    for k, v in C.items():
        if v % 2 == 1 and v > 0:
            C[k] -= 1

for _ in range(b):
    for k, v in C.items():
        if v % 4 == 2 and v > 0:
            C[k] -= 2

for _ in range(a):
    for k, v in C.items():
        if v % 4 == 0 and v > 0:
            C[k] -= 4

if sum(C.values()) == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# CODE FESTIVAL 2017 qual A
# C - Palindromic Matrix
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import Counter
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
S = [input()[:-1] for _ in range(H)]
C = Counter(''.join(S))

a = H//2*W//2
b = H//2*(W%2) + W//2*(H%2)
c = H % 2*W % 2

for _ in range(c):
    for k, v in C.items():
        if v % 2 == 1 and v > 0:
            C[k] -= 1

for _ in range(b):
    for k, v in C.items():
        if v % 4 == 2 and v > 0:
            C[k] -= 2

for _ in range(a):
    for k, v in C.items():
        if v % 4 == 0 and v > 0:
            C[k] -= 4

if sum(C.values()) == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

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

from collections import Counter

h,w = na()
c = Counter()

for i in range(h):
    c.update(ns())

c = list(c.values())

for i in range(len(c)):
    c[i] %= 4

print(c)
v,x,y,z = c.count(0),c.count(1),c.count(2),c.count(3)

if x+z > 1:
    print('No')
else:
    a = y+z
    if h%2 == 1:
        a -= w//2
    if w%2 == 1:
        a -= h//2
    if a <= 0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

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

mod = [0,0,0,0]
for i in backet:
  mod[i%4] += 1
ans = False
if h%2 and w%2:
  if mod[1] + mod[3] == 1 and (mod[2]+mod[3] - (h//2+w//2))%2 == 0 and mod[2]+mod[3] - (h//2+w//2) >= 0:
    ans = True
elif h%2:
  if mod[1]+mod[3] == 0 and (mod[2]+mod[3]-w//2)%2==0 and mod[2]+mod[3]-w//2>=0:
    ans = True
elif w%2:
  if mod[1]+mod[3] == 0 and (mod[2]+mod[3]-h//2)%2 == 0 and mod[2]+mod[3]-h//2 >= 0:
    ans = True
else:
  if mod[1] == mod[2] == mod[3] == 0:
    ans = True
if ans:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

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

mod = [0,0,0,0]
for i in backet:
  mod[i%4] += 1
ans = False
if h%2 and w%2:
  if mod[1] + mod[3] == 1 and (mod[2]+mod[3] - (h//2+w//2))%2 == 0:
    ans = True
elif h%2:
  if mod[1]+mod[3] == 0 and (mod[2]+mod[3]-w//2)%2 == 0:
    ans = True
elif w%2:
  if mod[1]+mod[3] == 0 and (mod[2]+mod[3]-h//2)%2 == 0:
    ans = True
else:
  if mod[1] == mod[2] == mod[3] == 0:
    ans = True
if ans:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

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

mod = [0,0,0,0]
for i in backet:
  mod[i%4] += 1
ans = False
if h%2 and w%2:
  if mod[1] + mod[3] == 1 and mod[2]+mod[3] == h//2 + w//2:
    ans = True
elif h%2:
  if mod[1]+mod[3] == 0 and mod[2]+mod[3] == w//2:
    ans = True
elif w%2:
  if mod[1]+mod[3] == 0 and mod[2]+mod[3] == h//2:
    ans = True
else:
  if mod[1] == mod[2] == mod[3] == 0:
    ans = True
if ans:
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    one = 0
    two = 0
    for i in d.values():
        one += i & 1
        two += (i >> 1) & 1
    if x:
        if one == 1:
            if h + w - 2 >= two:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if one:
            print("No")
        else:
            x = (h & 1) * (w >> 1) + (w & 1) * (h >> 1)
            if x:
                if x >= two:
                    print("Yes")
                else:
                    print("No")
            else:
                if two:
                    print("No")
                else:
                    print("Yes")

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    one = 0
    two = 0
    for i in d.values():
        one += i & 1
        two += (i >> 1) & 1
    if x:
        if one == 1:
            if h + w - 2 >= two:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if one:
            print("No")
        else:
            x = (h & 1) * w >> 1 + (w & 1) * h >> 1
            if x:
                if x >= two:
                    print("Yes")
                else:
                    print("No")
            else:
                if two:
                    print("No")
                else:
                    print("Yes")

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    one = 0
    two = 0
    for i in d.values():
        one += i & 1
        two += (i >> 1) & 1
    if x:
        if one == 1:
            if h + w - 2 >= two:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if one:
            print("No")
        else:
            x = (h & 1) * h + (w & 1) * w
            if x:
                if x >= two:
                    print("Yes")
                else:
                    print("No")
            else:
                if two:
                    print("No")
                else:
                    print("Yes")

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    one = 0
    two = 0
    for i in d.values():
        one += i & 1
        two += (i >> 1) & 1
    if x:
        if one <= 1:
            if h + w - 2 >= two:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if one:
            print("No")
        else:
            x = (h & 1) * (h - 1) + (w & 1) * (w - 1)
            if x:
                if x >= two:
                    print("Yes")
                else:
                    print("No")
            else:
                if two:
                    print("No")
                else:
                    print("Yes")

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    one = 0
    two = 0
    for i in d.values():
        one += i & 1
        two += i >> 1 & 1
    if x:
        if one <= 1:
            if h + w - 2 >= two:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if one:
            print("No")
        else:
            x = (h & 1) * (h - 1) + (w & 1) * (w - 1)
            if x:
                if x >= two:
                    print("Yes")
                else:
                    print("No")
            else:
                if two:
                    print("No")
                else:
                    print("Yes")

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    four = 0
    for i in d.values():
        if i & 1:
            if x:
                x -= 1
            else:
                print("No")
                return
        four |= i >= 4
    if four or w == 1 or h == 1:
        print("Yes")
    else:
        print("No")
        

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    x = h & 1 and w & 1
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    four = 0
    for i in d.values():
        if i & 1:
            if x:
                x -= 1
            else:
                print("No")
                return
        four |= i & 4 and not i & 3
    if four or w == 1 or h == 1:
        print("Yes")
    else:
        print("No")
        

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w = LI()
    a = SR(h)
    d = defaultdict(int)
    for i in range(h):
        for j in range(w):
            d[a[i][j]] += 1
    four = 0
    for i in d.values():
        if i & 1:
            print("No")
            return
        four |= i & 4 and not i & 3
    if four:
        print("Yes")
    else:
        print("No")
        

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import collections
h, w = map(int,input().split())
a = []
for i in range(h):
    a.append(list(input()))

tmp = []
for i in range(h):
    for j in a[i]:
        tmp.append(j)
#print(tmp)
c = collections.Counter(tmp)
tmp2 = list(set(tmp))
four = 0
two = 0
odd = 0
for i in tmp2:
    if c[i] % 4 == 0:
        four += 1
    elif c[i] % 2 == 0:
        two += 1
    else:
        odd += 1

if h % 2 == 1 and w % 2 == 1:
    if odd == 1 and (two == 2 or two == 0):
        ans = "Yes"
    else:
        ans = "No"
elif h % 2 == 0 and w % 2 == 0:
    if two == 0 and odd == 0:
        ans = "Yes"
    else:
        ans = "No"
elif h % 2 == 0 and w % 2 == 1:
    if odd == 0 and two <= h // 2:
        ans = "Yes"
    else:
        ans = "No"
elif h % 2 == 1 and w % 2 == 0:
    if odd == 0 and two <= w // 2:
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dic = {}
for i in range(h):
    for j in range(w):
        if a[i][j] not in dic.keys():
            dic[a[i][j]] = 1
        else:
            dic[a[i][j]] += 1
group = {1:0, 2:0, 4:0}
for v in dic.values():
    group[4] += v // 4
    v = v % 4
    group[2] += v // 2
    group[1] += v % 2
if h&1 and w&1:
    group[1] -= 1
    group[2] -= (h//2 + w//2)
elif h&1 or w&1:
    group[2] -= (h*w - 4*((h//2)*(w//2)))//2
group[4] -= ((h//2) * (w//2))
for v in group.values():
    if v != 0:
        print('No')
        break
else:
    print('Yes')