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())
dd = defaultdict(int)
for _ in range(h):
    string = input()
    for s in string:
        dd[s] += 1
        
one = 0
two = 0
for _, value in dd.items():
    if value % 2 == 1:
        one += 1
        value -= 1
    if value % 4 == 2:
        two += 1
        
if h % 2 == 0 and w % 2 == 0:
    if one == two == 0:
        print("Yes")
    else:
        print("No")
elif h % 2 == 1 and w % 2 == 1:
    if one > 1:
        print("No")
    elif two > h // 2 + w // 2:
        print("No")
    else:
        print("Yes")
else:
    if one > 0:
        print("No")
    elif two > h // 2 + w // 2:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

h,w = lr()
a = [x for i in range(h) for x in sr()]
a_set = Counter(a)
n4 = 0
n2 = 0
n1 = 0
for _,v in a_set.items():
    if v%4 == 0:
        n4+=1
    elif v%2 == 0:
        n2+=1
    else:
        n1+=1
if h%2==0 and w%2==0:
    if n1 != 0:
        print('No')
        sys.exit()
    if n2 != 0:
        print('No')
        sys.exit()
elif h%2==0:
    if n1 != 0:
        print('No')
        sys.exit()
    if n2 != h//2:
        print('No')
        sys.exit()
elif w%2==0:
    if n1 != 0:
        print('No')
        sys.exit()
    if n2 > w//2:
        print('No')
        sys.exit()
else:
    if n1 != 1:
        print('No')
        sys.exit()
    if n2 > (w-1)//2+(h-1)//2:
        print('No')
        sys.exit()
print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    H, W = map(int, input().split())
    cnt = Counter()
    for _ in range(H):
        cnt.update(input())
    
    x = []
    for v in cnt.values():
        n = v%4
        if n:
            x.append(n)
    
    yes = "Yes"
    no = "No"

    c = (H&1)+(W&1)

    if c == 0:
        if x:
            return no
        return yes
    
    elif c == 1:
        if H&1:
            a = W
        else:
            a = H
        if sum(x) > a:
            return no
        for y in x:
            if y&1:
                return no
        return yes

    else:
        o = 0
        for y in x:
            if y&1:
                o += 1
        
        if o == 1:
            return yes
        
        return no

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    H, W = map(int, input().split())
    cnt = Counter()
    for _ in range(H):
        cnt.update(input())
    
    x = []
    for v in cnt.values():
        n = v%4
        if n:
            x.append(n)
    
    print(x)
    
    yes = "Yes"
    no = "No"

    c = (H&1)+(W&1)

    if c == 0:
        if x:
            return no
        return yes
    
    elif c == 1:
        if H&1:
            a = W
        else:
            a = H
        if sum(x) > a:
            return no
        for y in x:
            if y&1:
                return no
        return yes

    else:
        o = 0
        for y in x:
            if y&1:
                o += 1
        
        if o == 1:
            return yes
        
        return no

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    H, W = map(int, input().split())
    cnt = Counter()
    for _ in range(H):
        cnt.update(input())
    
    x = []
    for v in cnt.values():
        n = v%4
        if n:
            x.append(n)
    
    yes = "Yes"
    no = "No"

    c = (H&1)+(W&1)

    if c == 0:
        if x:
            return no
        return yes
    
    elif c == 1:
        for y in x:
            if y&1:
                return no
        return yes

    else:
        o = 0
        for y in x:
            if y&1:
                o += 1
        
        if o == 1:
            return yes
        
        return no

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve():
    H, W = map(int, input().split())
    cnt = Counter()
    for _ in range(H):
        cnt.update(input())
    
    x = []
    for v in cnt.values():
        n = v%4
        if v:
            x.append(n)
    
    yes = "Yes"
    no = "No"

    c = (H&1)+(W&1)

    if c == 0:
        if x:
            return no
        return yes
    
    elif c == 1:
        for y in x:
            if y&1:
                return no
        return yes

    else:
        o = 0
        for y in x:
            if y&1:
                o += 1
        
        if o == 1:
            return yes
        
        return no

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
lis = []
for i in range(H):
    a = list(input())
    lis += a

lis = list(Counter(lis).values())
length = len(lis)

check_4 = 0
check_2 = 0
check_kisu = 0
for i in lis:
    if i % 4 == 0:
        check_4 += 1
    elif i % 2 == 0:
        check_2 += 1
    else:
        if check_kisu:
            print('No')
            exit()
        else:
            check_kisu = i

if (H % 2 == 0) & (W % 2 == 0):
    if (check_kisu != 0) | (check_2 != 0):
        print('No')
        exit()

elif (H % 2 == 1) & (W % 2 == 1):
    if H * W == check_kisu:
        print('Yes')
        exit()

    if (H + W - 1 < check_kisu + 2 * check_2) | ((H + W - 1 - check_kisu - 2 * check_2) % 4 != 0):
        print('No')
        exit()

elif H % 2 == 1:
    if (W < 2 * check_2) | ((W - 2 * check_2) % 4 != 0):
        print('No')
        exit()


elif W % 2 == 1:
    if (H < 2 * check_2) | ((H - 2 * check_2) % 4 != 0):
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
lis = []
for i in range(H):
    a = list(input())
    print(a)
    lis += a


lis = list(Counter(lis).values())



length = len(lis)

print(lis)

check_4 = 0
check_2 = 0
check_kisu = 0
for i in lis:
    if i % 4 == 0:
        check_4 += 1
    elif i % 2 == 0:
        check_2 += 1
    else:
        if check_kisu:
            print('No')
            exit()
        else:
            check_kisu = i

if (H % 2 == 0) & (W % 2 == 0):
    if (check_kisu != 0) | (check_2 != 0):
        print('No')
        exit()

elif (H % 2 == 1) & (W % 2 == 1):
    if H * W == check_kisu:
        print('Yes')
        exit()

    if (H + W - 1 < check_kisu + 2 * check_2) | ((H + W - 1 - check_kisu - 2 * check_2) % 4 != 0):
        print('No')
        exit()

elif H % 2 == 1:
    if (W < 2 * check_2) | ((W - 2 * check_2) % 4 != 0):
        print('No')
        exit()


elif W % 2 == 1:
    if (H < 2 * check_2) | ((H - 2 * check_2) % 4 != 0):
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
lis = []
for i in range(H):
    a = list(input())
    lis += a

lis = list(Counter(lis).values())
length = len(lis)

check_4 = 0
check_2 = 0
check_kisu = 0
for i in lis:
    if i % 4 == 0:
        check_4 += 1
    elif i % 2 == 0:
        check_2 += 1
    else:
        if check_kisu:
            print('No')
            exit()
        else:
            check_kisu = i

if (H % 2 == 0) & (W % 2 == 0):
    if (check_kisu != 0) | (check_2 != 0):
        print('No')
        exit()

elif (H % 2 == 1) & (W % 2 == 1):
    if (H + W - 1 < check_kisu + 2 * check_2) | ((H + W - 1 - check_kisu - 2 * check_2) % 4 != 0):
        print('No')
        exit()

elif H % 2 == 1:
    if (W < 2 * check_2) | ((W - 2 * check_2) % 4 != 0):
        print('No')
        exit()


elif W % 2 == 1:
    if (H < 2 * check_2) | ((H - 2 * check_2) % 4 != 0):
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

H, W = map(int, input().split())
lis = []
for i in range(H):
    a = list(input())
    lis += a

lis = list(Counter(lis).values())
length = len(lis)

check_4 = 0
check_2 = 0
check_kisu = 0
for i in lis:
    if i % 4 == 0:
        check_4 += 1
    elif i % 2 == 0:
        check_2 += 1
    else:
        if check_kisu:
            print('No')
            exit()
        else:
            check_kisu = i

if (H % 2 == 0) & (W % 2 == 0):
    if (check_kisu != 0) | (check_2 != 0):
        print('No')
        exit()

elif (H % 2 == 1) & (W % 2 == 1):
    if H + W - 1 < check_kisu + 2 * check_2:
        print('No')
        exit()

elif H % 2 == 1:
    if (W < 2 * check_2) | ((W - 2 * check_2) % 4 != 0):
        print('No')
        exit()


elif W % 2 == 1:
    if (H < 2 * check_2) | ((H - 2 * check_2) % 4 != 0):
        print('No')
        exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H,W = map(int,input().split())
d = defaultdict(int)
for _ in range(H):
    for c in list(input()):
        d[c] += 1
for i in range((H-1)//2+1):
    for j in range((W-1)//2+1):
        s = set([(x,y) for x,y in [(i,j),(i,W-1-j),(H-1-i,j),(H-1-i,W-1-j)] if 0<=x<H and 0<=y<W])
        f = True
        for k in d:
            if d[k] >= len(s):
                d[k] -= len(s)
                f = False
                break
        if f:
            print("No")
            exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H,W = map(int,input().split())
d = defaultdict(int)
for _ in range(H):
    for c in list(input()):
        d[c] += 1
for i in range((H-1)//2+1):
    for j in range((W-1)//2+1):
        s = set([(i,j),(i,W-1-j),(H-1-i,j),(H-1-i,W-1-j)])
        f = True
        for k in d:
            if d[k] >= len(s):
                d[k] -= len(s)
                f = False
                break
        if f:
            print("No")
            exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
H,W=list(map(int,input().split()))
l=[]
for i in range(H):
   l.append(list(input()))
from collections import Counter
l=sum(l,[])
l=Counter(l)
one=(H%2)*(W%2)
two=(H%2)*(W//2)+(W%2)*(H//2)
four=(H//2)*(W//2)
for i in l.values():
   j=i//4
   i=i%4
   four-=j
   if i==1:
      one-=1
   elif i==2:
      two-=1
   elif i==3:
      one-=1;two-=1
if set([0]) == set([one,two,four]):
   print("Yes")
else:
   two+=four*2
   four=0
   if set([0]) == set([one,two,four]):
      print("Yes")
   else:
      print("No")

Submission status: Wrong Answer

Solution code:
H,W=list(map(int,input().split()))
l=[]
for i in range(H):
   l.append(list(input()))
from collections import Counter
l=sum(l,[])
l=Counter(l)
one=(H%2)*(W%2)
two=(H%2)*(W//2)+(W%2)*(H//2)
four=(H//2)*(W//2)
for i in l.values():
   j=i//4
   i=i%4
   four-=j
   if i==1:
      one-=1
   elif i==2:
      two-=1
   elif i==3:
      one-=1;two-=1
if set([0]) == set([one,two,four]):
   print("Yes")
else:
   two+=four*2
   if set([0]) == set([one,two,four]):
      print("Yes")
   else:
      print("No")

Submission status: Wrong Answer

Solution code:
def main():
    H,W = map(int,input().split())
    a = [input() for _ in range(H)]
    list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    dict = {key:0 for key in list}
    #全てのキャピタルの個数を数える
    for i in range(H):
        for j in range(W):
            dict[a[i][j]] += 1
    #print(dict)
    #4の倍数の個数、2の倍数の個数、奇数の個数を数える
    num4 = 0
    num2 = 0
    num_even = 0
    for i in dict.values():
        if i == 0:
            continue
        elif i%2 == 1:
            num_even += 1
            even_num = i
        elif i%4 == 0:
            num4 += 1
        else:
            num2 += 1
    if num_even > 1:
        return('No')
    #print(num2,num4,num_even)
    #各場合分け
    if (H%2 == 0) and (W%2 == 0):
        if num2 == 0 and num_even == 0:
            return('Yes')
        else:
            return('No')
    elif (H%2 == 0) and (W%2 == 1):
        if num_even != 0:
            return ('No')
        elif num2 == W/2:
            return ('Yes')
        elif num2 < W/2:
            if (W-num2*2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        else:
            if (num2*2 - W)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        #elif H%4 == 0:
        #    if num2%2 == 0:
        #        return('Yes')
        #    else:
        #        return('No')
        #else:
        #    if num2%2 == 1:
        #        return('Yes')
        #    else:
        #        return('No')
    elif (H%2 == 1) and (W%2 == 0):
        if num_even != 0:
            return ('No')
        elif num2 == W/2:
            return ('Yes')
        elif num2 < W/2:
            if (W-num2*2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        else:
            if (num2*2 - W)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        #elif W%4 == 0:
        #    if num2%2 == 0:
        #        return('Yes')
        #    else:
        #        return('No')
        #else:
        #    if num2%2 == 1:
        #        return('Yes')
        #    else:
        #        return('No')
    else:
        if num_even != 1:
            return ('No')
        tmp = even_num - 1
        if tmp%4 == 0:
            M = H + W - 1 - 1
        else:
            M = H+W-1-3

        if num2 == M/2:
            return ('Yes')
        elif num2 < M/2:
            if (M - 2*num2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        elif num2 > M/2:
            if (num2-M/2)%2 == 0:
                return('Yes')
            else:
                return ('No')

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    H,W = map(int,input().split())
    a = [input() for _ in range(H)]
    list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    dict = {key:0 for key in list}
    #全てのキャピタルの個数を数える
    for i in range(H):
        for j in range(W):
            dict[a[i][j]] += 1
    print(dict)
    #4の倍数の個数、2の倍数の個数、奇数の個数を数える
    num4 = 0
    num2 = 0
    num_even = 0
    for i in dict.values():
        if i == 0:
            continue
        elif i%2 == 1:
            num_even += 1
            even_num = i
        elif i%4 == 0:
            num4 += 1
        else:
            num2 += 1
    if num_even > 1:
        return('No')
    print(num2,num4,num_even)
    #各場合分け
    if (H%2 == 0) and (W%2 == 0):
        if num2 == 0 and num_even == 0:
            return('Yes')
        else:
            return('No')
    elif (H%2 == 0) and (W%2 == 1):
        if num_even != 0:
            return ('No')
        elif num2 == W/2:
            return ('Yes')
        elif num2 < W/2:
            if (W-num2*2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        else:
            if (num2*2 - W)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        #elif H%4 == 0:
        #    if num2%2 == 0:
        #        return('Yes')
        #    else:
        #        return('No')
        #else:
        #    if num2%2 == 1:
        #        return('Yes')
        #    else:
        #        return('No')
    elif (H%2 == 1) and (W%2 == 0):
        if num_even != 0:
            return ('No')
        elif num2 == W/2:
            return ('Yes')
        elif num2 < W/2:
            if (W-num2*2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        else:
            if (num2*2 - W)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        #elif W%4 == 0:
        #    if num2%2 == 0:
        #        return('Yes')
        #    else:
        #        return('No')
        #else:
        #    if num2%2 == 1:
        #        return('Yes')
        #    else:
        #        return('No')
    else:
        if num_even != 1:
            return ('No')
        tmp = even_num - 1
        if tmp%4 == 0:
            M = H + W - 1 - 1
        else:
            M = H+W-1-3

        if num2 == M/2:
            return ('Yes')
        elif num2 < M/2:
            if (M - 2*num2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        elif num2 > M/2:
            if (num2-M/2)%2 == 0:
                return('Yes')
            else:
                return ('No')

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    H,W = map(int,input().split())
    a = [input() for _ in range(H)]
    list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    dict = {key:0 for key in list}
    #全てのキャピタルの個数を数える
    for i in range(H):
        for j in range(W):
            dict[a[i][j]] += 1
    #4の倍数の個数、2の倍数の個数、奇数の個数を数える
    num4 = 0
    num2 = 0
    num_even = 0
    for i in dict.values():
        if i == 0:
            continue
        elif i%2 == 1:
            num_even += 1
            even_num = i
        elif i%4 == 0:
            num4 += 1
        else:
            num2 += 1
    if num_even > 1:
        return('No')

    #各場合分け
    if (H%2 == 0) and (W%2 == 0):
        if num2 == 0 and num_even == 0:
            return('Yes')
        else:
            return('No')
    elif (H%2 == 0) and (W%2 == 1):
        if num_even != 0:
            return ('No')
        elif H%4 == 0:
            if num2%2 == 0:
                return('Yes')
            else:
                return('No')
        else:
            if num2%2 == 1:
                return('Yes')
            else:
                return('No')
    elif (H%2 == 1) and (W%2 == 0):
        if num_even != 0:
            return ('No')
        elif W%4 == 0:
            if num2%2 == 0:
                return('Yes')
            else:
                return('No')
        else:
            if num2%2 == 1:
                return('Yes')
            else:
                return('No')
    else:
        if num_even != 1:
            return ('No')
        tmp = even_num - 1
        if tmp%4 == 0:
            M = H + W - 1 - 1
        else:
            M = H+W-1-3

        if num2 == M/2:
            return ('Yes')
        elif num2 < M/2:
            if (M - 2*num2)%4 == 0:
                return ('Yes')
            else:
                return ('No')
        elif num2 > M/2:
            if (num2-M/2)%2 == 0:
                return('Yes')
            else:
                return ('No')

print(main())

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
S = []
for _ in range(H):
  S.append(input())

# check palindrome

def main():
  num1 = (H*W) % 2 
  num2 = (W % 2)*H//2 + (H % 2)*W//2
  num4 = ((H*W) - num1 - num2*2)//4
  #print(num1,num2,num4)
  d = {}
  for i in range(H):
    for j in range(W):
      ch = S[i][j]
      if ch in d:
        d[ch] += 1
      else:
        d[ch] = 1

  check1 = sum([val % 2 for key,val in d.items()])
  #print(check1)
  if check1 != num1:
    return False
  
  check4 = 0
  for key,val in d.items():
    if val >= 4:
      check4 += val // 4
      d[key] = val % 4
    
  #print(check4)
  if check4 != num4:
    return False
  
  check2 = sum([val//2 for key,val in d.items()])
  #print(check2)
  if check4 != num2:
    return False
      
  return True
      
  
ans = main()
if ans:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
S = []
for _ in range(H):
  S.append(input())

# check palindrome

def main():
  num1 = (H*W) % 2 
  num2 = H//2 + W//2
  num4 = ((H*W) - num1 - num2*2)//4
  d = {}
  for i in range(H):
    for j in range(W):
      ch = S[i][j]
      if ch in d:
        d[ch] += 1
      else:
        d[ch] = 1

  check1 = sum([val % 2 for key,val in d.items()])
  check2 = len([1 for key,val in d.items() if val == 2])
  check4 = len([1 for key,val in d.items() if val == 4])

  if check1 <= num1 and check2 <= num2 and check4 <= num4:
    return True
      
  return False
      
  
ans = main()
if ans:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
S = []
for _ in range(H):
  S.append(input())

# check palindrome

def main():
  num1 = (H*W) % 2 
  num2 = H//2 + W//2
  num4 = ((H*W) - num1 - num2)//4
  d = {}
  for i in range(H):
    for j in range(W):
      ch = S[i][j]
      if ch in d:
        d[ch] += 1
      else:
        d[ch] = 1

  check1 = sum([val % 2 for key,val in d.items()])
  check2 = len([1 for key,val in d.items() if val == 2])
  check4 = len([1 for key,val in d.items() if val == 4])
  if check1 > num1 or check2 > num2 or check4 > num4:
    return False
      
  return True
      
  
ans = main()
if ans:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
S = []
for _ in range(H):
  S.append(input())

# check palindrome

def main():
  oddnum = (H*W) % 2
  d = {}
  for i in range(H):
    for j in range(W):
      ch = S[i][j]
      if ch in d:
        d[ch] += 1
      else:
        d[ch] = 1

  check = sum([val % 2 for key,val in d.items()])
  if check != oddnum:
    return False
      
  return True
      
  
ans = main()
if ans:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w=map(int,input().split())
#a=[list(input()) for i in range(w)]
a=[]
flag=True
for i in range(h):
  a+=list(input())
ca=Counter(a)
if h%2==0 and w%2==0:
  for i in ca:
    if ca[i]%4!=0:
      flag=False
      print('No')
      break
  if flag:
    print('Yes')

elif h%2!=0 and w%2!=0:
  yon=(h-1)*(w-1)
  ni=h*w-yon-1
  iti=1
  for i in ca:
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      elif iti>0:
        iti-=1
        ca[i]-=1
      else:
        flag=False
        break
  if yon==0 and ni==0 and iti==0 and flag==True:
    print('Yes')
  else:
    print('No')
else:
  if h%2==0:
    yon=(h)*(w-1)
  else:
    yon=(h-1)*(w)
  ni=h*w-yon
  for i in ca:
    while ca[i]>0:
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      else:
        break
  if yon==0 and ni==0:
    print('Yes')
  else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w=map(int,input().split())
#a=[list(input()) for i in range(w)]
a=[]
flag=True
for i in range(h):
  a+=list(input())
ca=Counter(a)
if h%2==0 and w%2==0:
  for i in ca:
    if ca[i]%4!=0:
      flag=False
      print('No')
      break
  if flag:
    print('Yes')

elif h%2!=0 and w%2!=0:
  yon=(h-1)*(w-1)
  ni=h*w-yon-1
  iti=1
  for i in ca:
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      elif iti>0:
        iti-=1
        ca[i]-=1
      else:
        break
  if yon==0 and ni==0 and iti==0:
    print('Yes')
  else:
    print('No')
else:
  if h%2==0:
    yon=(h)*(w-1)
  else:
    yon=(h-1)*(w)
  ni=h*w-yon
  for i in ca:
    while ca[i]>0:
      if ca[i]%4==0 and yon>0:
        ca[i]-=4
        yon-=4
      elif ca[i]%2==0 and ni>0:
        ni-=2
        ca[i]-=2
      else:
        break
  if yon==0 and ni==0:
    print('Yes')
  else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w = inpl()
cnt = defaultdict(int)
for _ in range(h):
    for x in input():
        cnt[x] += 1
one = two = 0
for va in cnt.values():
    now = va%4
    if now == 1 or now == 3:
        one += 1
    if now == 2 or now == 3:
        two += 1
res = False
if h%2 == 0 and w%2 == 0:
    if not one and not two:
        res = True
elif h%2 and w%2:
    if one == 1 and two <= (w-1)//2 + (h-1)//2:
        res = True
else:
    now = h if w%2 else w
    if not one and two <= w//2:
        res = True
print('Yes' if res else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w = inpl()
cnt = defaultdict(int)
for _ in range(h):
    for x in input():
        cnt[x] += 1
one = two = 0
for va in cnt.values():
    now = va%4
    if now == 1 or now == 3:
        one += 1
    if now == 2 or now == 3:
        two += 1
res = False
if h%2 == 0 and w%2 == 0:
    if not one and not two:
        res = True
elif h%2 and w%2:
    if one == 1 and two <= (w-1)//2 + (h-1)//2 and two%2 == 0:
        res = True
else:
    now = h if w%2 else w
    if not one and two <= w//2 and two%2 == 0:
        res = True
print('Yes' if res else 'No')

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w = inpl()
cnt = defaultdict(int)
for _ in range(h):
    for x in input():
        cnt[x] += 1
one = two = 0
for va in cnt.values():
    now = va%4
    if now == 1 or now == 3:
        one += 1
    if now == 2 or now == 3:
        two += 1
res = False
if h%2 == 0 and w%2 == 0:
    if not one and not two:
        res = True
elif h%2 and w%2:
    if one == 1 and two == (w-1)//2 + (h-1)//2:
        res = True
else:
    now = h if w%2 else w
    if not one and two == w//2:
        res = True
print('Yes' if res else '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 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')