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 defaultdict
h, w = map(int, input().split())

a = [list(input()) for _ in range(h)]
d = defaultdict(int)
for i in range(h):
    for j in range(w):
        d[ord(a[i][j]) - ord('a')] += 1

odd = 0
two = 0
four = 0
for i in range(26):
    four += d[i] // 4
    d[i] %= 4
    if d[i] % 2 == 0:
        two += d[i] // 2
    else:
        odd += 1

# 両方奇数: 奇数が1個、偶数がh//2 + w//2セット、あと全部4の倍数
# 両方偶数: 全部4の倍数
# 片方
if four == h // 2 * (w // 2):
    if h % 2 == 1 and w % 2 == 1:
        if odd == 1 and two <= h // 2 + w // 2:
            print('Yes')
        else:
            print('No')
    elif h % 2 == 1:
        if odd == 0 and two <= w // 2:
            print('Yes')
        else:
            print('No')
    elif w % 2 == 1:
        if odd == 0 and two <= h // 2:
            print('Yes')
        else:
            print('No')
    else:
        if odd == 0 and two == 0:
            print('Yes')
        else:
            print('No')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
h, w = map(int, input().split())

a = [list(input()) for _ in range(h)]
d = defaultdict(int)
for i in range(h):
    for j in range(w):
        d[ord(a[i][j]) - ord('a')] += 1

odd = 0
two = 0
four = 0
for i in range(26):
    four += d[i] // 4
    d[i] %= 4
    if d[i] % 2 == 0:
        two += d[i] // 2
    else:
        odd += 1

if four == h // 2 * (w // 2):
    if h % 2 == 1 and w % 2 == 1:
        if odd == 1 and two == h // 2 + w // 2:
            print('Yes')
        else:
            print('No')
    elif h % 2 == 1:
        if two == w // 2:
            print('Yes')
        else:
            print('No')
    elif w % 2 == 1:
        if two == h // 2:
            print('Yes')
        else:
            print('No')
    else:
        print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

A = [list(input()) for _ in range(H)]
okFlag = True
for a in A:
    memo = {}
    for ai in a:
        if ai in memo:
            memo[ai] += 1
        else:
            memo[ai] = 1
            
mod0 = []
mod1_3 = []
mod2 = []
for key, value in memo.items():
    if value % 4 == 0:
        mod0.append(key)
    elif value % 4 == 2:
        mod2.append(key)
    else:
        mod1_3.append(key)

if W % 2 == 0 and H % 2 == 0:
    if len(mod1_3) > 0 or len(mod2) > 0:
        print("No")
    else:
        print("Yes")

elif W % 2 == 0 or H % 2 == 0:
    if H % 2 == 0:
        W, H = H, W
 
    if W // 2 < len(mod2):
        print("No")
    elif len(mod1_3) > 0:
        print("No")
    else:
        print("Yes")

else:
    if len(mod1_3) == 1 and len(mod2) <= H // 2 + W // 2:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
h, w = map(int, input().split())

a = [list(input()) for _ in range(h)]
d = defaultdict(int)
for i in range(h):
    for j in range(w):
        d[ord(a[i][j]) - ord('a')] += 1

odd = 0
two = 0
four = 0
for i in range(26):
    four += d[i] // 4
    d[i] %= 4
    if d[i] % 2 == 0:
        two += d[i] // 2
    else:
        odd += 1

# 両方奇数: 奇数が1個、偶数がh//2 + w//2セット、あと全部4の倍数
# 両方偶数: 全部4の倍数
# 片方
if four == h // 2 * (w // 2):
    if h % 2 == 1 and w % 2 == 1:
        if odd == 1 and two == h // 2 + w // 2:
            print('Yes')
        else:
            print('No')
    elif h % 2 == 1:
        if two == w // 2:
            print('Yes')
        else:
            print('No')
    else:
        print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
a = ''
for _ in range(H):
    a += input()
cnt_counter = list(Counter(a).values())
yes_flag = False
if H % 2 == 0 and W % 2 == 0:
    if all([i % 4 == 0 for i in cnt_counter]):
        yes_flag = True
elif H % 2 == 1 and W % 2 == 1:
    two_cnt = H // 2 + W // 2
    four_cnt = (H // 2) * (W // 2)
    for i in range(len(cnt_counter)):
        if cnt_counter[i] % 2 == 1:
            cnt_counter[i] -= 1
            if cnt_counter[i] == 0:
                cnt_counter.pop(i)
            break
    else:
        print('No')
        exit()
    for i in range(len(cnt_counter)):
        if cnt_counter[i] % 2 == 0 and cnt_counter[i] % 4 != 0:
            two_cnt -= 1
            cnt_counter[i] -= 2
        if two_cnt == 0:
            break
    for i in range(len(cnt_counter)):
        if cnt_counter[i] <= 0:
            continue
        if cnt_counter[i] % 4 != 0:
            break
        if two_cnt > 2:
            two_cnt -= 2
        else:
            four_cnt -= 1
    if two_cnt == four_cnt == 0:
        yes_flag = True
else:
    if H % 2 == 0:
        two_cnt = H // 2
    else:
        two_cnt = W // 2
    four_cnt = (H // 2) * (W // 2)
    for i in range(len(cnt_counter)):
        if cnt_counter[i] % 2 == 0 and cnt_counter[i] % 4 != 0:
            two_cnt -= 1
            cnt_counter[i] -= 2
        if two_cnt == 0:
            break
    for i in range(len(cnt_counter)):
        if cnt_counter[i] <= 0:
            continue
        if cnt_counter[i] % 4 != 0:
            break
        if two_cnt > 2:
            two_cnt -= 2
        else:
            four_cnt -= 1
    if two_cnt == four_cnt == 0:
        yes_flag = True
if yes_flag:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

A = [list(input()) for _ in range(H)]
okFlag = True
for a in A:
    memo = {}
    for ai in a:
        if ai in memo:
            memo[ai] += 1
        else:
            memo[ai] = 1
            
    kisu_count = 0
    for key,val in memo.items():
        if val%2==1:
            if W%2==1:
                kisu_count += 1
            else:
                okFlag = False
                break
        if W%2==1 and kisu_count == 2:
            okFlag = False
            break
memo = {}
for a in A:
    a.sort()
    a=str(a)
    if a in memo:
        memo[a] += 1
    else:
        memo[a] = 1
        
kisu_count = 0
for key,val in memo.items():
    if val%2==1:
        if H%2==1:
            kisu_count += 1
        else:
            okFlag = False
            break
    if H%2==1 and kisu_count == 2:
        okFlag = False
        break
if okFlag:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
def main():
    h, w = map(int, input().split())
    alp = [0]*26
    for _ in range(h):
        s = input()
        for c in s:
            p = ord(c) - ord("a")
            alp[p] += 1
    f = True
    cnt_1, cnt_2, cnt_4 = 0, 0, 0
    if h%2 == 1 and w%2 == 1:
        cnt_1 = 1
        cnt_2 = w-1 + h-1
        cnt_4 = (w-1)*(h-1)
    elif h%2 == 1 and w%2 == 0:
        cnt_2 = h-1
        cnt_4 = w*(h-1)
    elif h%2 == 0 and w%2 == 1:
        cnt_2 = w-1
        cnt_4 = (w-1)*h
    else:
        cnt_4 = w*h
    for i in range(26):
        while cnt_4 > 0 and alp[i]>=4:
            alp[i] -= 4
            cnt_4 -= 4
    if cnt_4 > 0:
        f = False
    for i in range(26):
        while cnt_2 > 0 and alp[i]>=2:
            alp[i] -= 2
            cnt_2 -= 2
    if cnt_2 > 0:
        f = False
    for i in range(26):
        while cnt_1 > 0 and alp[i]>=1:
            alp[i] -= 1
            cnt_1 -= 1
    if cnt_1 > 0:
        f = False
    if f:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())

    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i & 1 for i in a_ac_v])
        if h&1 == 1 and w&1 == 1:
            if a_ac_v_parity_ac[1] == 1:
                print('Yes')
            else:
                print('No')
            sys.exit()
        else:
            if a_ac_v_parity_ac[1] >= 1:
                print('No')
            else:
                print('Yes')
            sys.exit()

    num = []
    num.extend([4] * (h // 2 * w // 2))
    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])

    while sum(num):
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
            a_ac_v.sort(reverse=True)
            num.sort(reverse=True)
        else:
            break
    if num[0] == 0 and a_ac_v[0] == 0:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())

    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i & 1 for i in a_ac_v])
        if h == 1 and w == 1:
            if a_ac_v_parity_ac[1] == 1:
                print('Yes')
            else:
                print('No')
            sys.exit()
        else:
            if a_ac_v_parity_ac[1] == 1:
                print('No')
            else:
                print('Yes')
            sys.exit()

    num = []
    num.extend([4] * (h // 2 * w // 2))
    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])

    while a_ac_v[0]:
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
            a_ac_v.sort(reverse=True)
            num.sort(reverse=True)
        else:
            break
    if num[0] == 0 and a_ac_v[0] == 0:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())

    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i & 1 for i in a_ac_v])
        if h == 1 and w == 1:
            if a_ac_v_parity_ac[1] == 1:
                print('Yes')
            else:
                print('No')
            sys.exit()
        else:
            if a_ac_v_parity_ac[1] == 1:
                print('No')
            else:
                print('Yes')
            sys.exit()

    num = []
    num.extend([4] * (h // 2 * w // 2))
    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])
    while num[0]:
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
        else:
            break
        a_ac_v.sort(reverse=True)
        num.sort(reverse=True)
    if num[0] == 0 and a_ac_v[0] == 0:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())

    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i&1 for i in a_ac_v])
        if a_ac_v_parity_ac[1] == 1:
            print('Yes')
        else:
            print('No')
        sys.exit()

    num = []
    num.extend([4] * (h // 2 * w // 2))
    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])
    while sum(num):
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
        else:
            break
        a_ac_v.sort(reverse=True)
        num.sort(reverse=True)
    if sum(num):
        print('No')
    else:
        if sum(a_ac_v) == 0:
            print('Yes')
        else:
            print('No')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    h, w = map(int, input().split())
    a = []
    for _ in range(h):
        a.extend(list(input()))
    a_ac = Counter(a)
    a_ac_v = list(a_ac.values())

    if h == 1 or w == 1:
        a_ac_v_parity_ac = Counter([i&1 for i in a_ac_v])
        if a_ac_v_parity_ac[1] == 1:
            print('Yes')
        else:
            print('No')
        sys.exit()

    num = []
    num.extend([4] * (h // 2 * w // 2))
    if h&1:
        num.extend([2] * (w // 2))
    if w&1:
        num.extend([2] * (h // 2))
    if h&1 and w&1:
        num.extend([1])
    while sum(num):
        if a_ac_v[0] >= num[0]:
            a_ac_v[0] -= num[0]
            num[0] = 0
        else:
            break
        a_ac_v.sort(reverse=True)
        num.sort(reverse=True)
    if sum(num):
        print('No')
    else:
        print('Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
a = ''

for i in range(h):
    a += input()
from collections import Counter
c = Counter(a)

if h==1 or w==1:
    odd = 0
    for i in c:
        if c[i]%2==1:
            odd += 1
    print('Yes' if odd<=1 else 'No')
    exit()

x = 0
y = 0
z = 0
for i in c:
    x += c[i]//4*2
    y += c[i]%4
    c[i] = c[i]%4
    if c[i]%2==1:
        z += 1

print('Yes' if y<=x and z<=1 else 'No')

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        res=0
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
            elif cnt[i]%4==2:
                res+=1
        else:
            if res>h:
              print("No")
            else:
              print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w//2:
                print("No")
            else:
                print("Yes")
    else:
        res1=0
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res1+=1
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1       
        if res1==1 and res<=(h-1)//2+(w-1)//2:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        res=0
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
            elif cnt[i]%4==2:
                res+=1
        else:
            if res>h:
              print("No")
            else:
              print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w:
                print("No")
            else:
                print("Yes")
    else:
        res1=0
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res1+=1
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1       
        if res1==1 and res<=h+w:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        res=0
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
            elif cnt[i]%4==2:
                res+=1
        else:
            if res>h:
              print("No")
            else:
              print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w:
                print("No")
            else:
                print("Yes")
    else:
        res1=0
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res1+=1
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1       
        if res1==1 :
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        res=0
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
            elif cnt[i]%4==2:
                res+=1
        else:
            if res>h:
              print("No")
            else:
              print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w:
                print("No")
            else:
                print("Yes")
    else:
        res1=0
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res1+=1
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1       
        if res1==1 and res<=h:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w=map(int,input().split())
a=[input() for _ in range(h)]
cnt={}
for i in range(h):
    ar=a[i]
    for j in ar:
        if j in cnt:
            cnt[j]+=1
        else:
            cnt[j]=1
if h%2==0:
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0:
                print("No")
                break
        else:
            print("Yes")
    else:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%4!=2:
                print("No")
                break
        else:
            print("Yes") 
else:
    res=0
    if w%2==0:
        for i in cnt:
            if cnt[i]%4!=0 and cnt[i]%2==0:
                res+=1
            elif cnt[i]%2!=0:
                print("No")
                break
        else:
            if res>w:
                print("No")
            else:
                print("Yes")
    else:
        for i in cnt:
            if cnt[i]%4!=0  and cnt[i]%2==1:
                res+=1
        if res!=1:
            print("No")
        else:
            print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline
h,w = map(int,input().split())
def check(a):
  b = Counter(a)
  n = 0
  for i in b.values():
    if i%2==1:
      n += 1
      if n > 1:
        return False
  return True
a = []
for i in range(h):
  a.append("".join(sorted(input().strip())))
  if not check(a[i]):
    print("No")
    exit()
if check(a):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())

def solve():
    H, W = MAP()
    a = [input() for _ in range(H)]
    d = defaultdict(int)

    for i in range(H):
        for j in range(W):
            d[a[i][j]] += 1
    
    mod4 = 0
    mod2 = 0
    for x in d.values():
        mod4 += x % 4
        mod2 += x % 2
    
    ans = "No"
    if H % 2 == 0:
        if W % 2 == 0:
            if mod4 == 0:
                ans = "Yes"
        else:
            if mod4 == H and mod2 == 0:
                ans = "Yes"
    elif H % 2 == 1:
        if W % 2 == 0:            
            if mod4 == W and mod2 == 0:
                ans = "Yes"
        else:
            if mod4 == W+H-1 and mod2 == 1:
                ans = "Yes"

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)
from heapq import heappop, heappush, heapify


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [-int(i) for v, i in counts]

    def pop(): return -heappop(cands)
    def push(x): return heappush(cands, -x)

    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue

        elif h > 1 and h > 1:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                el.append(4)
            if p % 4 == 2:
                el.append(2)
            h -= 2
            w -= 2

    el = sorted(el)[::-1]
    flag = True
    for e in el:
        c = pop()
        if c < e:
            flag = False
            break
        push(c - e)

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)
import heapq


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [-1 * int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            heapq.heappush(el, -1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            heapq.heappush(el, -2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            heapq.heappush(el, -2)
            h -= 2
            continue

        elif h > 1 and h > 1:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                heapq.heappush(el, -4)
            if p % 4 == 2:
                heapq.heappush(el, -2)
            h -= 2
            w -= 2

    heapq.heapify(cands)
    while el:
        e = -1 * heapq.heappop(el)
        c = -1 * heapq.heappop(cands)
        heapq.heappush(cands, -(c - e))

    if sum(cands) != 0:
        print("No")
    else:
        print("Yes")
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)
import heapq


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [-1 * int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            heapq.heappush(el, -1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            heapq.heappush(el, -2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            heapq.heappush(el, -2)
            h -= 2
            continue

        elif h > 1 and h > 1:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                heapq.heappush(el, -4)
            if p % 4 == 2:
                heapq.heappush(el, -4)
            h -= 2
            w -= 2

    heapq.heapify(cands)
    while el:
        e = -1 * heapq.heappop(el)
        c = -1 * heapq.heappop(cands)
        heapq.heappush(cands, -(c - e))

    if sum(cands) != 0:
        print("No")
    else:
        print("Yes")
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue

        elif h > 1 and h > 1:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                el.append(4)
            if p % 4 == 2:
                el.append(2)
            h -= 2
            w -= 2

    el = sorted(el)[::-1]
    for e in el:
        for i in range(len(cands)):
            if cands[i] >= e:
                cands[i] -= e
                break
        cands = sorted(cands)[::-1]
    for c in cands:
        if c > 0:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue

        elif h > 1 and h > 1:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                el.append(4)
            if p % 4 == 2:
                el.append(2)
            h -= 2
            w -= 2

    el = sorted(el)[::-1]
    for e in el:
        for i in range(len(cands)):
            if cands[i] >= e:
                cands[i] -= e
                break
    for c in cands:
        if c > 0:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()