Problem:


Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import Counter
def solve():
    h,w=map(int,input().split())
    s = ""
    for _ in range(h):
        s+=input()
    c = Counter(s)
    cnt1 = 0
    cnt2 = 0
    for i in c.values():
        if i%4==1:
            cnt1+=1
        elif i%4==2:
            cnt2+=2
        elif i%4==3:
            cnt2+=2
            cnt1+=1
    if h%2 and w%2:
        cnt1-=1
        cnt2-=(h+w-2)
    elif h%2:
        cnt2-=w
    elif w%2:
        cnt2-=h
    if cnt1==0 and cnt2==0:
        print('Yes')
    else:
        print('No')
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

grid = "".join([input() for _ in range(H)])
c = Counter(grid)
cnt = []
for v in c.values():
    heapq.heappush(cnt, -v)

# まず四隅埋める
while H > 1 and W > 1:
    num = -heapq.heappop(cnt)
    if num < 4:
        print("No")
        exit()
    num -= 4
    H -= 2
    W -= 2
    if num > 0:
        heapq.heappush(cnt, -num)

# 残りの1列を見る（奇数列がある場合）
if H == 1 and W == 1:
    if -sum(cnt) != 1:
        print("No")
        exit()

if max(H, W) % 2 == 0:
    if any(v % 2 == 1 for v in cnt):
        print("No")
        exit()

if max(H, W) % 2 == 1:
    if sum(v % 2 for v in cnt) != 1:
        print("No")
        exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W=map(int, input().split())
    A=[list(input().rstrip()) for _ in range(H)]
    from itertools import chain
    from collections import Counter
    C=Counter(chain.from_iterable(A))
    t4=[(v//4)*4 for v in C.values()]
    t2=[(v//2)*2 for v in C.values()]
    if sum(t4) >= (H-1)*(W-1):
        if sum(t2) >= H*W-1:
            print('Yes')
            return
    print('No')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
cntA = Counter()
for _ in range(H):
    cntA += Counter(list(input()))

if H % 2 + W % 2 == 0:
    if all(c % 4 == 0 for c in cntA.values()):
        print('Yes')
    else:
        print('No')
    exit()

if H % 2 + W % 2 == 1:
    two = (H if H % 2 == 0 else W) // 2
    cntTwo = 0
    for c in cntA.values():
        if c % 4 == 0:
            continue
        if c % 2 == 0:
            cntTwo += 1
            continue
        break
    else:
        if cntTwo <= two:
            print('Yes')
            exit()
    print('No')
    exit()

cntTwo = 0
cntOdd = 0
for c in cntA.values():
    if c % 4 == 0:
        continue
    if c % 2 == 0:
        cntTwo += 1
        continue
    cntOdd += 1

if cntOdd == 1 and cntTwo <= H + W - 2:
    print('Yes')
    exit()

print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
cntA = Counter()
for _ in range(H):
    cntA += Counter(list(input()))

if H % 2 + W % 2 == 0:
    if all(c % 4 == 0 for c in cntA.values()):
        print('Yes')
    else:
        print('No')
    exit()

if H % 2 + W % 2 == 1:
    two = H if H % 2 == 0 else W
    cntTwo = 0
    for c in cntA.values():
        if c % 4 == 0:
            continue
        if c % 2 == 0:
            cntTwo += 1
            continue
        break
    else:
        if cntTwo <= two:
            print('Yes')
            exit()
    print('No')
    exit()

cntTwo = 0
cntOdd = 0
for c in cntA.values():
    if c % 4 == 0:
        continue
    if c % 2 == 0:
        cntTwo += 1
        continue
    cntOdd += 1

if cntOdd == 1 and cntTwo <= H + W - 2:
    print('Yes')
    exit()

print('No')

Submission status: Wrong Answer

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

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

D=[0]*5
if h%2==0 and w%2==0:
    D[4]=(h*w)//4
elif (h%2==0 and w%2==1) or (h%2==1 and w%2==0):
    if h%2==1:
        D[2]=w//2
        D[4]=w*(h-1)//4
    else:
        D[2]=h//2
        D[4]=h*(w-1)//4
else:
    D[2]=(h+w-2)//2
    D[4]=(h-1)*(w-1)//2
    D[1]=1

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes" if P[0]==0 else "No")

Submission status: Wrong Answer

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

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

D=[0]*5
if h%2==0 and w%2==0:
    D[4]=(h*w)//4
elif (h%2==0 and w%2==1) or (h%2==1 and w%2==0):
    if h%2==1:
        D[2]=w//2
        D[4]=w*(h-1)//4
    else:
        D[2]=h//2
        D[4]=h*(w-1)//4
else:
    D[2]=(h+w-2)//2
    D[4]=(h-1)*(w-1)//2
    D[1]=1

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes")

Submission status: Wrong Answer

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

word={}
A=[list(input()) for _ in range(h)]
for i in A:
    for j in i:
        if j not in word:word[j]=1
        else:word[j] +=1

D=[0]*5
if h%2==0 and w%2==0:
    D[4]=(h*w)//2
elif (h%2==0 and w%2==1) or (h%2==1 and w%2==0):
    if h%2==1:
        D[2]=w//2
        D[4]=w*(h-1)//4
    else:
        D[2]=h//2
        D[4]=h*(w-1)//4
else:
    D[2]=(h+w-2)//2
    D[4]=(h-1)*(w-1)//2
    D[1]=1

P=[0]*5
for i in word.values():
    P[4] +=i//4;i %=4
    P[2] +=i//2;i %=2
    P[1] +=i

for i in (4,2,1):
    if D[i]==P[i]:continue
    elif D[i]<P[i]:
        P[i//2] +=(P[i]-D[i])*2
    else:print("No");exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w = map(int,input().split())
A = [input() for _ in range(h)]
s = []
for a in A:
    for c in a:
        s.append(c)
c = Counter(s)

if h==1 and w==1:
    print('Yes')
else:
    if h==1 or w==1:
        lm = 2
    else:
        lm = 4
    o = 0
    flg = False
    for v in c.values():
        if v>=lm:
            flg = True
        if v%2!=0:
            o += 1
    if not flg:
        print('No')
    elif w*h%2!=0:
        print('Yes' if o<=1 else 'No')
    else:
        print('Yes' if o==0 else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w = map(int,input().split())
A = [input() for _ in range(h)]
s = []
for a in A:
    for c in a:
        s.append(c)
c = Counter(s)
if h==1 and w==1:
    print('Yes')
else:
    if h==1 or w==1:
        lm = 2
    else:
        lm = 4
    o = 0
    flg = False
    for v in c.values():
        if v>=lm:
            flg = True
        if v%2!=0:
            o += 1
    if w*h%2!=0:
        print('Yes' if o<=1 else 'No')
    else:
        print('Yes' if o==0 else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w = map(int,input().split())
if h==1 and w==1:
    print('Yes')
    exit()
lst = []
for _ in range(h):
    s = input()
    for c in s:
        lst.append(c)
c = Counter(lst)
cnt = 0
if h==1 or w==1:
    lim = 2
else:
    lim = 4
flg = False
for v in c.values():
    if v>=lim:
        flg = True
    if v%2!=0:
        cnt += 1
if not flg:
    print('No')
else:
    if h*w%2==0:
        print('Yes' if cnt==0 else 'No')
    else:
        print('Yes' if cnt%2==1 else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w = map(int,input().split())
if h==1 and w==1:
    print('Yes')
    exit()
lst = []
for _ in range(h):
    s = input()
    for c in s:
        lst.append(c)
c = Counter(lst)
cnt = 0

if h==1 or w==1:
    lim = 2
else:
    lim = 4
flg = False
for v in c.values():
    if v>=lim:
        flg = True
    if v%2!=0:
        cnt += 1
if not flg:
    print('No')
else:
    if h*w%2==0:
        print('Yes' if cnt==0 else 'No')
    else:
        print('Yes' if cnt==1 else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter

h, w = map(int, input().split())
a = []
for _ in range(h):
    aa = list(input())
    a += aa

c = Counter(a)

hq, hr = divmod(h, 2)
wq, wr = divmod(w, 2)
d = {1: hr * wr,
     2: hq * wr + hr * wq,
     4: hq * wq}

for v in c.values():
    q4, r4 = divmod(v, 4)
    if q4 > d[4]:
        r4 += 2 * (q4 - d[4])
        q4 = d[4]
    d[4] -= q4
    q2, r2 = divmod(r4, 2)
    d[2] -= q2
    d[1] -= r2

if any(d.values()):
    ans = "No"
else:
    ans = "Yes"

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

h, w = map(int, input().split())
a = []
for _ in range(h):
    aa = list(input())
    a += aa

c = Counter(a)

hq, hr = divmod(h, 2)
wq, wr = divmod(w, 2)
d = {1: hr * wr,
     2: hq * wr + hr * wq,
     4: hq * wq}

for v in c.values():
    q4, r4 = divmod(v, 4)
    if q4 > d[4]:
        r4 += q4 - d[4]
        q4 = d[4]
    d[4] -= q4
    q2, r2 = divmod(r4, 2)
    d[2] -= q2
    d[1] -= r2

if any(d.values()):
    ans = "No"
else:
    ans = "Yes"

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
    elif H&1:
        g2 = W//2 
    elif W&1:     
        g2 = H//2 
    g4 = (H//2) * (W//2)

    for _ in range(g1):
        for key, value in counter.items():
            if value%4 == 1 or value%4 == 3:
                counter[key] -= 1
                break            

    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 
            
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value > 0:
                counter[key] -= 2
                break
                
            if value%4 == 0 and value > 0:
                counter[key] -= 2
                break
            


    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            return
    else:
        print("Yes")
    
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
    elif H&1:
        g2 = W//2 
    elif W&1:     
        g2 = H//2 
    g4 = (H//2) * (W//2)

    for _ in range(g1):
        for key, value in counter.items():
            if value%4 == 1 or value%4 == 3:
                counter[key] -= 1
                break            
    
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value > 0:
                counter[key] -= 2
                break
                
            if value%4 == 0 and value > 0:
                counter[key] -= 2
                break
            
    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 

    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            return
    else:
        print("Yes")
    
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
        g4 = (H//2) * (W//2)
    elif H&1:
        g2 = W//2 
        g4 = (H//2) * (W//2)
    elif W&1:     
        g2 = H//2 
        g4 = (H//2) * (W//2)
    else:
        g4 = (H//2) * (W//2)
    
    for _ in range(g1):
        for key, value in counter.items():
            if value%4 == 1 or value%4 == 3:
                counter[key] -= 1
                break            
    
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value >0 :
                counter[key] -= 2
                break
            
    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 

    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            break
    else:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
        g4 = (H//2) * (W//2)
    elif H&1:
        g2 = W//2 
        g4 = (H//2) * (W//2)
    elif W&1:     
        g2 = H//2 
        g4 = (H//2) * (W//2)
    else:
        g4 = (H//2) * (W//2)
    
    for _ in range(g1):
        for key, value in counter.items():
            if value%2 == 1 or value%2 == 3:
                counter[key] -= 1
                break            
    
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value >0 :
                counter[key] -= 2
                break
            
    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 

    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            break
    else:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
        g4 = (H//2) * (W//2)
    elif H&1:
        g2 = W//2 
        g4 = (H//2) * (W//2)
    elif W&1:     
        g2 = H//2 
        g4 = (H//2) * (W//2)
    else:
        g4 = (H//2) * (W//2)
    
    for _ in range(g1):
        for key, value in counter.items():
            if value%2 == 1 and value > 0:
                counter[key] -= 1
                break            
        else:
            print('No')
            return
    
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value >0 :
                counter[key] -= 2
                break
        else:
            print('No')
            return       
            
    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 
        else:
            print('No')
            return
            
    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            break
    else:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    H,W = MAP()
    A = [input() for _ in range(H)]

    counter = defaultdict(int)
    for i in range(H):
        for j in range(W):
            counter[A[i][j]] += 1

    g1 = 0
    g2 = 0
    g4 = 0

    if H&1 and W&1:
        g1 = 1
        g2 = H//2 + W//2
        g4 = (H//2) * (W//2)
    elif H&1:
        g2 = W//2 
        g4 = (H//2) * (W//2)
    elif W&1:     
        g2 = H//2 
        g4 = (H//2) * (W//2)
    else:
        g4 = (H//2) * (W//2)
    
    for _ in range(g1):
        for key, value in counter.items():
            if value%2 == 1 and value > 0:
                counter[key] -= 1
                break            
    
    for _ in range(g2):
        for key, value in counter.items():
            if value%4 == 2 and value >0 :
                counter[key] -= 2
                break
            
    for _ in range(g4):
        for key, value in counter.items():
            if value%4 == 0 and value > 0:
                counter[key] -= 4
                break 
    
    for key,value in counter.items():
        if value == 0:
            continue
        else:
            print("No")
            break
    else:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from math import sin,pi

def main():
    h,w = map(int,input().split())
    dic = {}
    for _ in range(h):
        a = list(input())
        for s in a:
            if s in dic:
                dic[s] += 1
            else:
                dic[s] = 1
    
    ans = 'Yes'
    if h%2 == 0 and w%2 == 0:
        for v in dic.values():
            if v%4 != 0:
                ans = 'No'
                break
    
    elif h%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += cnt//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                ans = 'No'
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    elif w%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                ans = 'No'
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    else:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                v -= 1
                if v%4 == 0:
                    cnt += v//4
                else:
                    cnt += (v - 2)//4
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    print(ans)
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from math import sin,pi

def main():
    h,w = map(int,input().split())
    dic = {}
    for _ in range(h):
        a = list(input())
        for s in a:
            if s in dic:
                dic[s] += 1
            else:
                dic[s] = 1
    
    ans = 'Yes'
    if h%2 == 0 and w%2 == 0:
        for v in dic.values():
            if v%4 != 0:
                ans = 'No'
                break
    
    elif h%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += cnt//4
            elif v%2 == 0:
                cnt += 1
                cnt += (v - 2)//4
            else:
                ans = 'No'
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    elif w%2 == 0:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                ans = 'No'
                break
        
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    else:
        cnt = 0
        for v in dic.values():
            if v%4 == 0:
                cnt += v//4
            elif v%2 == 0:
                cnt += (v - 2)//4
            else:
                v -= 1
                if v%4 == 0:
                    cnt += v//4
                else:
                    cnt += (v - 2)//4
        if cnt < (h//2) * (w//2):
            ans = 'No'
    
    print(ans)
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
al = [chr(ord('a') + i) for i in range(26)]

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

cnt = [0] * 26
for h in range(H):
    for w in range(W):
        cnt[al.index(A[h][w])] += 1
cnt = [i for i in cnt if i != 0]
cnt.sort(reverse=True)

if H == 1 or W == 1:
    count = (H*W) // 2
    while count > 0:
        count -= 1
        cnt[0] -= 2
        cnt.sort(reverse=True)
    cnt = [i for i in cnt if i != 0]
    if sum(cnt) == 1 and len(cnt) == 1:
        ans = "Yes"
    else:
        ans = "No"
else:
    if H % 2 == 1 and W % 2 == 1:
        count = (H//2) * (W//2)
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        count = (H//2) + (W//2)
        while count > 0:
            count -= 1
            cnt[0] -= 2
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        if sum(cnt) == 1 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
    elif H % 2 == 0 and W % 2 == 0:
        count = (H//2) * (W//2)
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = list(set(cnt))
        if cnt[0] == 0 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
    else:
        count = (H//2) * (W//2)
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        cnt.sort(reverse=True)
        count = (H%2) * (W//2) + (H//2) * (W%2)
        while count > 0:
            count -= 1
            cnt[0] -= 2
            cnt.sort(reverse=True)
        cnt =list(set(cnt))
        if cnt[0] == 0 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
al = [chr(ord('a') + i) for i in range(26)]

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

cnt = [0] * 26
for h in range(H):
    for w in range(W):
        cnt[al.index(A[h][w])] += 1
if H == 1 or W == 1:
    check = 0
    for i in range(26):
        if cnt[i] % 2 == 1:
            check += 1
    if check == 1:
        ans = "Yes"
    else:
        ans = "No"
else:
    cnt = [i for i in cnt if i != 0]
    cnt.sort(reverse=True)
    if H % 2 == 1 and W % 2 == 1:
        count = H//2 * W//2
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        count = H//2 + W//2
        while count > 0:
            count -= 1
            cnt[0] -= 2
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        if cnt[0] == 1 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
    elif H % 2 == 0 and W % 2 == 0:
        count = H//2 * W//2
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = list(set(cnt))
        if cnt[0] == 0 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
    else:
        count = H//2 * W//2
        while count > 0:
            count -= 1
            cnt[0] -= 4
            cnt.sort(reverse=True)
        cnt = [i for i in cnt if i != 0]
        count = (H%2) * (W//2) + (H//2) * (W%2)
        while count > 0:
            count -= 1
            cnt[0] -= 2
            cnt.sort(reverse=True)
        cnt =list(set(cnt))
        if cnt[0] == 0 and len(cnt) == 1:
            ans = "Yes"
        else:
            ans = "No"
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

H,W=map(int,input().split())
S=""
for i in range(H):
    S+=input()
S=Counter(S)
c=0
b=0
a=0

for i in S:

    z=S[i]
    c+=z//4
    z%=4
    b+=z//2
    z%=2
    a+=z
c-=min(H//2,W//2)
if c<0:
    print("No")
    exit()
b+=c*2
b-=(H*W//2*2-min(H//2,W//2)*4 )//2

if b!=0:
    print("No")
    exit()
if a-H*W%2!=0:
    print("No")
    exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

H,W=map(int,input().split())
S=""
for i in range(H):
    S+=input()
S=Counter(S)
c=0
b=0
a=0

for i in S:

    z=S[i]
    c+=z//4
    z%=4
    b+=z//2
    z%=2
    a+=z
c-=min(H//2,W//2)
if c<0:
    print("No")
    exit()
b+=c*2
b-=(H*W//2*2-min(H//2,W//2)*4 )//2

if b<0:
    print("No")
    exit()
if a-H*W%2!=0:
    print("No")
    exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

H,W=map(int,input().split())
S=""
for i in range(H):
    S+=input()
S=Counter(S)
c=0
b=0
a=0

for i in S:

    z=S[i]
    c+=z//4
    z%=4
    b+=z//2
    z%=2
    a+=z
c-=min(H//2,W//2)
if c<0:
    print("No")
    exit()
b+=c*2
b-=(H*W-min(H//2,W//2)*4 )//2

if b<0:
    print("No")
    exit()
if a-H*W%2!=0:
    print("No")
    exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
import string

h, w = map(int, input().split())
Alpha = [x for x in string.ascii_lowercase]
Alpha_count = {x:0 for x in Alpha}

for _ in range(h):
    A = str(input())
    for a in A:
        Alpha_count[a] += 1

odd_count = 0
two_count = 0
four_count = 0

for a in Alpha:
    if Alpha_count[a]%2 == 1:
        odd_count += 1
    elif Alpha_count[a] == 2:
        two_count += 1
    else:
        four_count += int(Alpha_count[a] // 4)
        if Alpha_count[a] % 4 != 0:
            two_count += 1

if odd_count >= 2:
    ans = 'No'
elif h%2 == 0 and w%2 == 0:
    if four_count == int(h/2)*int(w/2):
        ans = 'Yes'
    else:
        ans = 'No'
elif h%2 == 0 and w%2 != 0:
    need_four_count = int(h/2)*int((w-1)/2)
    need_two_count = int(h/2)
    if four_count < need_four_count:
        ans = 'No'
    elif four_count == need_four_count:
        if two_count == need_two_count:
            ans = 'Yes'
        else:
            ans = 'No'
    elif four_count > need_four_count:
        if need_two_count == 2*(four_count - need_four_count) + two_count:
            ans = 'Yes'
        else:
            ans = 'No'
elif h%2 != 0 and w%2 == 0:
    need_four_count = int(w/2)*int((h-1)/2)
    need_two_count = int(w/2)
    if four_count < need_four_count:
        ans = 'No'
    elif four_count == need_four_count:
        if two_count == need_two_count:
            ans = 'Yes'
        else:
            ans = 'No'
    elif four_count > need_four_count:
        if need_two_count == 2*(four_count - need_four_count) + two_count:
            ans = 'Yes'
        else:
            ans = 'No'
else:
    if odd_count != 1:
        ans = 'No'
    else:
        need_four_count = int((h-1)/2)*int((w-1)/2)
        need_two_count = int((h-1)/2)+int((w-1)/2)
        if four_count < need_four_count:
            ans = 'No'
        elif four_count == need_four_count:
            if two_count == need_two_count:
                ans = 'Yes'
            else:
                ans = 'No'
        elif four_count > need_four_count:
            if need_two_count == 2*(four_count - need_four_count) + two_count:
                ans = 'Yes'
            else:
                ans = 'No'

print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
D={chr(code): 0 for code in range(ord('a'), ord('z') + 1)}
for i in range(H):
  A=list(input())
  for i in A:
    D[i]+=1
if H%2==0 and W%2==0:
  for i in D.values():
    if i%4!=0:
      print('No')
      exit()
  print('Yes')
elif H%2==1 and W%2==1:
  E=[]
  for i in D.values():
    if i%4!=0:
      E.append(i%4)
  E=sorted(E)
  a=H+W-1
  if H==1 or W==1:
    for i in range(a//2+1):
      if E==[1]+[2]*i:
        print('Yes')
        exit()
    print('No')
    exit()    
  if (a+1)//2 <len(E):
    print('No')
    exit()
  
  for i in range(a//4+1):
    if E==[1]+[2,2]*i:
      print('Yes')
      exit()
  print('No')
else:
  E=[]
  for i in D.values():
    if i%4!=0:
      E.append(i%4)
  E=sorted(E)
  if H%2==0:
    a=H
  else:
    a=W
  if a//2 <len(E):
    print('No')
    exit()
  for i in range(a//2+1):
    if E==[2]*i:
      print('Yes')
      exit()
  print('No')