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:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = max(H,W)
        mmm = mm//2
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)== 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m >= mmm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")
    
    else:
        q = 0
            
        if ((H%2) == 1):
            seigen = (H//2+1)*W
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
        for k in range(s):
            q += (Q[k] % 2)
        if (q == 0)and(s<=seigen):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        m = 0
        mm = max(H,W)//2 +1
        for k in range(s):
            q += (Q[k] % 2)
            if (Q[k] % 4)== 0:
                m += 1
        if (q == 1)and(s<=seigen)and (m >= mm):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")
    
    else:
        q = 0
            
        if ((H%2) == 1):
            seigen = (H//2+1)*W
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
        for k in range(s):
            q += (Q[k] % 2)
        if (q == 0)and(s<=seigen):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
from math import ceil
H,W = list(map(int,input().split()))
counter = Counter()
for i in range(H):
    counter.update(input())

center_A = min(H,W) if max(H,W)%2 else 0
center_B = max(H,W) if min(H,W)%2 else 0

for count in counter.most_common():
    if count[1]%2:
        center_A -= 1
        if min(H,W) == 1:
            center_B -= count[1]
        else:
            center_B -= count[1]%4
        if center_A < 0 or center_B < 0:
            print("No")
            break
    else:
        if (count[1]//2)%2:
            if min(H,W) == 1:
                center_B -= count[1]
            else:
                center_B -= count[1]%4
            if center_B < 0:
                print("No")
                break

else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if s == 1:
    print("Yes")
else:
    if ((H*W)%2 == 1):
        seigen = (H//2+1)*(W//2 +1)
        q = 0
        for k in range(s):
            q += (Q[k] % 2)
        if (q == 1)and(s<=seigen):
            print("Yes")
        else:
            print("No")
    elif ((H%2) == 0)and((W %2) ==0):
        q = 0
        for k in range(s):
            q += (Q[k] % 4)
        if ((s % 2) == 0)and(q == 0):
            print("Yes")
        else:
            print("No")
    
    else:
        q = 0
            
        if ((H%2) == 1):
            seigen = (H//2+1)*W
        elif ((W%2) == 1):
            seigen = (W//2+1)*H
        for k in range(s):
            q += (Q[k] % 2)
        if (q == 0)and(s<=seigen):
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W = list(map(int,input().split()))
counter = Counter()
for i in range(H):
    counter.update(input())

center_A = min(H,W) if max(H,W)%2 else 0
center_B = max(H,W) if min(H,W)%2 else 0

for count in counter.most_common():
    if count[1]%2:
        center_A -= 1
        center_B -= count[1]%4
        if center_A < 0 or center_B < 0:
            print("No")
            break
    else:
        if (count[1]//2)%2:
            center_B -= count[1]%4
            if center_B < 0:
                print("No")
                break

else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
import collections
H,W = list(map(int , input().split()))
D = []
for j in range(H):
    x=list(input())
    D.extend(x)
s = len((set(D)))
c = collections.Counter(D)
Q = []

for i in range(s):
    Q.append(c.most_common()[i][1])
if ((H*W)%2 == 1):
    seigen = (H//2+1)*(W//2 +1)
    q = 0
    for k in range(s):
        q += (Q[k] % 2)
    if (q == 1)and(s<=seigen):
        print("Yes")
    else:
        print("No")
elif ((H%2) == 0)and((W %2) ==0):
    q = 0
    for k in range(s):
        q += (Q[k] % 4)
    if ((s % 2) == 0)and(q == 0):
        print("Yes")
    else:
        print("No")

else:
    q = 0
        
    if ((H%2) == 1):
        seigen = (H//2+1)*W
    elif ((W%2) == 1):
        seigen = (W//2+1)*H
    for k in range(s):
        q += (Q[k] % 2)
    if (q == 0)and(s<=seigen):
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W = list(map(int,input().split()))
counter = Counter()
for i in range(H):
    counter.update(input())

center_A = min(H,W) if max(H,W)%2 else 0
center_B = max(H,W) if min(H,W)%2 else 0

for count in counter.most_common():
    if count[1]%2:
        center_A -= 1
        center_B -= count[1]
        if center_A < 0 or center_B < 0:
            print("No")
            break
    else:
        if (count[1]//2)%2:
            center_B -= count[1]//2
            if center_B < 0:
                print("No")
                break

else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int,input().split())
li=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        li.append(a[j])
c=Counter(li)

if(H%2==0 and W%2==0):
    for x,y in c.items():
        if(y%4!=0):
            print('No')
            exit()
    print('Yes')
elif(H%2==1 and W%2==1):
    f=(W//2)*(H//2)
    t=(H//2)+(W//2)
    o=1
    fc,tc,oc=0,0,0
    for x,y in c.items():
        fc+=y//4
        r=y%4
        tc+=r//2
        oc+=r%2
    if(fc==f and tc==t and oc==o):
        print('Yes')
    else:
        print('No')
else:
    if(H%2==1 and W%2==0):
        H,W=W,H
    f=(W//2)*(H//2)
    t=H//2
    fc,tc,oc=0,0,0
    for x,y in c.items():
        fc+=y//4
        r=y%4
        tc+=r//2
        oc+=r%2

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W = list(map(int,input().split()))
counter = Counter()
for i in range(H):
    counter.update(input())

center_A = min(H,W) if max(H,W)%2 else 0
center_B = max(H,W) if min(H,W)%2 else 0
for count in counter.most_common():
    if count[1]%2:
        center_A -= 1
        center_B -= 1
        if center_A < 0 or center_B < 0:
            print("No")
            break
    else:
        if (count[1]//2)%2:
            center_B -= 1
            if center_B < 0:
                print("No")
                break

else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())

num = [0]*5

for h in range(-(-H//2)):
    for w in range(-(-W//2)):
        tmp = 1
        if h < H//2:
            tmp *= 2
        if w < W//2:
            tmp *= 2
        num[tmp] += 1

s = ""
for _ in range(H):
    s += input()

c = [list(t) for t in Counter(s).most_common()]

four = 0
for x in c:
    four += x[1]//4
    x[1] %= 4

two = 0
for x in c:
    two += x[1]//2
    x[1] %= 2

one = 0
for x in c:
    one += x[1]
    x[1] %= 1

if four == num[4] and two == num[2] and one == num[1]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


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

    cnt = {}
    for i in range(H):
        for j in range(W):
            a = A[i][j]
            if a not in cnt:
                cnt[a] = 1
            else:
                cnt[a] += 1

    if H % 2 == 0 and W % 2 == 0:
        res = True
        for v in cnt.values():
            if v % 4 != 0:
                res = False
                break
        if res:
            print("Yes")
        else:
            print("No")
    elif H % 2 != 0 and W % 2 != 0:
        g1 = 1
        g2 = H // 2 + W // 2
        g3 = H // 2 * W // 2
        for _ in range(g1):
            for c, v in cnt.items():
                if v % 4 == 1 or v % 4 == 3:
                    cnt[c] -= 1
                    break
        for _ in range(g2):
            for c, v in cnt.items():
                if v % 4 == 2:
                    cnt[c] -= 2
                    break
        for _ in range(g3):
            for c, v in cnt.items():
                if v % 4 == 0:
                    cnt[c] -= 4
                    break
        res = True
        for c, v in cnt.items():
            if v > 0:
                res = False
                break
        if res:
            print("Yes")
        else:
            print("No")
    else:
        if W % 2 == 0:
            H, W = W, H
        c = {0: 0, 1: 0, 2: 0, 3: 0}
        for v in cnt.values():
            c[v % 4] += 1
        res = True
        if c[1] or c[3]:
            res = False
        if c[2] > H // 2:
            res = False
        if res:
            print("Yes")
        else:
            print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'

        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 2
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 2
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        else:
            cnt0 = 0
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 2
                elif i % 4 == 3:
                    cnt2 += 2
                    cnt1 += 1
                else:
                    cnt0 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2) and cnt2 % 2 == 0 and cnt0 % 4 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'

        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H//2 and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W//2 and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        else:
            cnt0 = 0
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1 or i % 4 == 3:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 1
                else:
                    cnt0 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2)//2 and cnt2 % 2 == 0 and cnt0 % 4 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'

        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        else:
            cnt0 = 0
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1 or i % 4 == 3:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 1
                else:
                    cnt0 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2) and cnt2 % 2 == 0 and cnt0 % 4 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'

        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H//2 and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W//2 and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'

        else:
            cnt0 = 0
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1 or i % 4 == 3:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 1
                else:
                    cnt0 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2) and cnt2 % 2 == 0 and cnt0 % 4 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'
        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'
        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'
        else:
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2) and cnt2 % 2 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        gg = Counter([])
        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg.update(Counter(g))

        if H % 2 == 0 and W % 2 == 0:
            for i in gg.values():
                if i % 4 != 0:
                    return 'No'
            return 'Yes'
        elif H % 2 == 0 and W % 2 == 1:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= H and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'
        elif H % 2 == 1 and W % 2 == 0:
            cnt = 0
            for i in gg.values():
                if i % 4 == 2:
                    cnt += 1
                elif i % 4 == 1 or i % 4 == 3:
                    return 'No'

            if cnt <= W and cnt % 2 == 0:
                return 'Yes'
            else:
                return 'No'
        else:
            cnt1 = 0
            cnt2 = 0
            for i in gg.values():
                if i % 4 == 1:
                    cnt1 += 1
                elif i % 4 == 2:
                    cnt2 += 1
            if cnt1 == 1 and (cnt2 <= (H + W - 2) and cnt2 % 2 == 0):
                return 'Yes'
            else:
                return 'No'

    print(main())

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    def main():

        H,W=map(int,input().split())
        grid=[[_ for _ in input()] for _ in range(H)]
        for g in grid:
            gg=Counter(g)
            cnt=0
            for i in gg.values():
                if i%2==1:
                    cnt+=1
            if not ((cnt==1 and W%2==1) or (cnt==0 and W%2==0)):
                return 'No'

        grid=zip(*grid)
        W=H
        for g in grid:
            gg=Counter(g)
            cnt=0
            for i in gg.values():
                if i%2==1:
                    cnt+=1
            if not ((cnt==1 and W%2==1) or (cnt==0 and W%2==0)):
                return 'No'
        return 'Yes'
    print(main())







resolve()

Submission status: Wrong Answer

Solution code:
import collections
import sys
H,W = map(int,input().split())
l = []
for i in range(H):
  s = input()
  l += [s[i] for i in range(W)] 

c = collections.Counter(l)
lc = list(c.values())

#1*n or n*1の時
if H == 1 or W == 1:
  lc = list(map(lambda x:x%2,lc))
  if (H+W)%2 == 0:
    for i in range(len(lc)):
      if lc[i] == 1:
        lc[i] = lc[i] - 1
        break
#2*2以上のとき
else:
  #4でわる
  yon = H*W - (H*(H%2 == 1)+W*(W%2 == 1)-1*(H%2 == 1 and W%2 == 1))
  yon = yon//4
  i = 0
  cnt = 0
  while cnt != yon and i!=len(lc):
    if lc[i] >= 4:
      lc[i] = lc[i]-4
      cnt += 1
    else:
      i += 1
  if yon > cnt:
    print("No")
    sys.exit()    
  #2でわる
  lc = list(map(lambda x:x%2,lc))
  if H%2 == 1 and W%2 == 1:
    for i in range(len(lc)):
      if lc[i] == 1:
        lc[i] = lc[i] - 1
        break
###      
ans = ["No","Yes"][sum(lc)==0]         
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
import sys
H,W = map(int,input().split())
l = []
for i in range(H):
  s = input()
  l += [s[i] for i in range(W)] 

c = collections.Counter(l)
lc = list(c.values())

#1*n or n*1の時
if H == 1 or W == 1:
  lc = list(map(lambda x:x%2,lc))
  if (H+W)%2 == 0:
    for i in range(len(lc)):
      if lc[i] == 1:
        lc[i] = lc[i] - 1
        break
#2*2以上のとき
else:
  #4でわる
  yon = H*W - (H*(H%2 == 1)+W*(W%2 == 1)-1*(H%2 == 1 and W%2 == 1))
  yon = yon//4
  i = 0
  cnt = 0
  while cnt != yon and i!=len(lc):
    if lc[i] >= 4:
      lc[i] = lc[i]-4
      cnt += 1
    else:
      i += 1
  if yon > cnt:
    print("No")
    sys.exit()    
  #2でわる
  lc = list(map(lambda x:x%2,lc))
  for i in range(len(lc)):
    if lc[i] == 1:
      lc[i] = lc[i] - 1
      break
###      
ans = ["No","Yes"][sum(lc)==0]         
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
H,W = map(int,input().split())
l = []
for i in range(H):
  s = input()
  l += [s[i] for i in range(W)] 

c = collections.Counter(l)
lc = c.values()

#1*n or n*1の時
if H == 1 or W == 1:
  lc = list(map(lambda x:x%2,lc))
  if (H+W)%2 == 0:
    for i in range(len(lc)):
      if lc[i] == 1:
        lc[i] = lc[i] - 1
        break
#2*2以上のとき
#4でわる
else:
  lc = list(map(lambda x:x%4,lc))
  if H%2 == 1:
    for i in range(len(lc)):
      if lc[i]>=2:
        lc[i] = lc[i] - 2
  if W%2 == 1:
    for i in range(len(lc)):
      if lc[i]>=2:
        lc[i] = lc[i] - 2  
  if H%2 == 1 and W%2 == 1:
    for i in range(len(lc)):
      if lc[i]>=1:
        lc[i] = lc[i] - 1
        break

ans = ["No","Yes"][sum(lc)==0]         
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    H, W = MI()
    alpha = [0] * len(l_alp)

    for i in range(H):
        s = S()
        for j in range(W):
            alpha[ord(s[j])-97] += 1

    for i in range((H+1)//2):
        for j in range((W+1)//2):
            V = 4
            if H % 2 != 0 and i == (H+1)//2-1:
                V //= 2
            if W % 2 != 0 and j == (W+1)//2-1:
                V //= 2
            f = False

            if V == 2:
                for k in range(len(alpha)):
                    if alpha[k] % 2 == 0 and alpha[k] % 4 == 2:
                        f = True
                        alpha[k] -= V
                        break

            if not f:
                for k in range(len(alpha)):
                    if alpha[k] >= V:
                        f = True
                        alpha[k] -= V
                        break
            # print(i+1, j+1, V, alpha)
            if not f:
                print('No')
                return
    print(alpha)
    print('Yes')

if __name__ == '__main__':
    main()