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:
# def makelist(n, m):
#     return [[0 for i in range(m)] for j in range(n)]

yes = "Yes"
no = "No"

H, W = map(int, input().split())
alph = [0]*26

for i in range(H):
    for c in input():
        alph[ord(c) - 97] += 1

four = 0
two = 0
one = 0
ans = no
for e in alph:
    four += e // 4
    e %= 4
    two += e // 2
    one += e % 2
        
if (H, W) == (1, 1):
    ans = yes
else:
    if H % 2 == 0 and W % 2 == 0:
        if one > 0 or two > 0:
            ans = no
        else:
            ans = yes
    elif H % 2 == 0 and W % 2 == 1:
        if one == 1 and two == H // 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 0:
        if one == 1 and two == W // 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 1:
        if one == 1 and two == H // 2 + W // 2:
            ans = yes

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    H, W = map(int, raw_input().split())
    a = [list(raw_input()) for i in xrange(H)]
    if H < W: H, W = W, H

    cnt = [0]*26
    for ai in a:
        for c in ai:
            cnt[ord(c) - ord("a")] += 1

    if H % 2 == 0 and W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 4:
                return False
        return True
    elif H % 2 == 0 or W % 2 == 0:
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                return False
            cnt[i] /= 2

        Y = H if W % 2 == 0 else W
        X = W/2 if W % 2 == 0 else H/2
        p = sum(cnt[i]/2 for i in xrange(26)) 
        return X*(Y - 1) >= 2*p
        
    else:
        odd = 0
        for i in xrange(26):
            if cnt[i] % 2 != 0:
                odd += 1
        return odd == 1

    return True

print "Yes" if solve() else "No"

Submission status: Wrong Answer

Solution code:
def func() :
    n,m=map(int,input().split())
    a={}
    for i in range(n) :
        s = input()
        for c in list(s):
            if c in a:
                a[c]+=1
            else :
                a[c] = 1
    res = 0
    for k,v in a.items() :
        if v%2==1 :
            res += 1
            if res > 1 :
                print('No')
                return
    c4 = (n//2*2)*(m//2*2)
    d4 = sum(filter(lambda x : x//4*4 , a.values()))
    if c4>d4 :
        print('No')
        return
    print('Yes')
func()

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
#     return [[0 for i in range(m)] for j in range(n)]

yes = "Yes"
no = "No"

H, W = map(int, input().split())
alph = [0]*26

for i in range(H):
    for c in input():
        alph[ord(c) - 97] += 1

four = 0
two = 0
one = 0
ans = no
for e in alph:
    four += e // 4
    e %= 4
    two += e // 2
    one += e % 2
        
if (H, W) == (1, 1):
    ans = yes
else:
    if H % 2 == 0 and W % 2 == 0:
        if one > 0 or two > 0:
            ans = no
        else:
            ans = yes
    elif H % 2 == 0 and W % 2 == 1:
        if one == 1 and two == H % 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 0:
        if one == 1 and two == W % 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 1:
        if one == 1 and two == H % 2 + W % 2:
            ans = yes

print(ans)

Submission status: Wrong Answer

Solution code:
from  collections import Counter
H, W = map(int, input().split())
A = []
C = Counter()
for i in range(H):
    A.append(input())
    C += Counter(A[i])

if(H%2 == 0 and W%2 == 0):
    for l in C:
        if(C[l]%4):
            print("No")
            exit()
elif(H%2 and W%2):
    t = 0
    for l in C:
        if(C[l]%2):
            t += 1
            C[l] -= 1
    if(t == 0 or t > 1):
        print("No")
        exit()

    t = 0
    for l in C:
        if(C[l]%4):
            t += 1

    if(((H+W-2)//2-t)%4 == 0):
        print("Yes")
        exit()
    else:
        print("No")
        exit()
else:
    t = 0
    r = H*(W%2) + W*(H%2)
    for l in C:
        if(C[l]%2):
            print("No")
            exit()
        elif(C[l]%4):
            t += 1
    if((r//2-t)%4):
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
#     return [[0 for i in range(m)] for j in range(n)]

yes = "Yes"
no = "No"

H, W = map(int, input().split())
alph = [0]*26

for i in range(H):
    for c in input():
        alph[ord(c) - 97] += 1

four = 0
two = 0
one = 0
ans = no
for e in alph:
    four += e // 4
    e %= 4
    two += e // 2
    one += e % 2
        
if (H, W) == (1, 1):
    ans = yes
else:
    if H % 2 == 0 and W % 2 == 0:
        if one > 0 or two > 0:
            ans = no
        else:
            ans = yes
    elif H % 2 == 0 and W % 2 == 1:
        if one == 1 and two == W % 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 0:
        if one == 1 and two == H % 2:
            ans = yes
    elif H % 2 == 1 and W % 2 == 1:
        if one == 1 and two == H % 2 + W % 2:
            ans = yes

print(ans)

Submission status: Wrong Answer

Solution code:
def func() :
    n,m=map(int,input().split())
    a={}
    for i in range(n) :
        s = input()
        for c in list(s):
            if c in a:
                a[c]+=1
            else :
                a[c] = 1
    res = 0
    for k,v in a.items() :
        if v%2==1 :
            res += 1
            if res > 1 :
                print('No')
                return
    c4 = (n//2*2)*(m//2*2)
    d4 = sum(filter(lambda x : x//4*4 , a.values()))
    if c4!=d4 :
        print('No')
        return
    c4 = (n%2)*m + n*(m%2) - n*m%2
    d4 = sum(filter(lambda x : (x%4)//2*2 , a.values()))
    if c4!=d4 :
        print('No')
        return
    print('Yes')
func()

Submission status: Wrong Answer

Solution code:
import string

[H,W]=map(int, raw_input().split())
lw=string.ascii_lowercase
A=[0 for i in range(26)]
for i in range(H):
    st=raw_input()
    for c in st:
        A[lw.index(c)]+=1

K=(W/2)*(H/2)
L=(W*(H%2)+H*(W%2)-2*(H%2)*(W%2))/2
M=(H%2)*(W%2)

f=0
ans=["No","Yes"]
P=sum([a/4 for a in A])
#print P,K
if P>=K:
    P-=K
    K=0
    Q=P*4+sum([(a%4)/2 for a in A])
    #print Q,L
    if Q>=L:
        Q-=L
        L=0
        R=Q*2+sum([a%2 for a in A])
        #print R,M
        if R>=M:
            f=1
print ans[f]

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from collections import defaultdict

def II(): return int(input())
def ILI(): return list(map(int, input().split()))


def read():
    H, W = ILI()
    count = defaultdict(int)
    for __ in range(H):
        sen = str(input())
        for i in range(W):
            count[sen[i]] += 1
    return H, W, count


def solve(H, W, count):
    odd = 0
    num_2 = 0
    num_4 = 0
    for val in count.values():
        div_2, mod_2 = divmod(val, 2)
        odd += mod_2
        div_2 *= 2
        div_4, mod_4 = divmod(div_2, 4)
        num_4 += div_4 * 4
        num_2 += mod_4
    ans = 0
    if H % 2 == 0:
        if W % 2 == 0:
            if odd == 0:
                if W == 2 and H == 2:
                    if num_4 == 4:
                        ans = "Yes"
                    else:
                        ans = "No"
                else:
                    ans = "Yes"
            else:
                ans = "No"
        else:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > H:
                    ans = "No"
                else:
                    ans = "Yes"
    else:
        if W % 2 == 0:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > W:
                    ans = "No"
                else:
                    ans = "Yes"
        else:
            if odd != 1:
                ans = "No"
            else:
                if num_2 > (W - 1) + (H - 1):
                    ans = "No"
                else:
                    ans = "Yes"
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, one_num, c):
    t = 0
    f = 0
    o = 0
    for k, v in c.items():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                o += 1
                v -= 1
    if one_num == 1:
        return o == 1 and f * 4 >= four_num
    else:
        return f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

ok = True
if H == W == 1:
    pass
elif H == 1 or W == 1:
    HW = H * W
    odd_num = sum(1 for x in c.values() if x % 2)
    even_num = HW - odd_num
    if HW % 2 == 1 and odd_num != 1: ok = False
    elif HW % 2 == 0 and odd_num != 0: ok = False
elif H % 2 == W % 2 == 0:
    # all 4
    for k, v in c.items():
        if v % 4 != 0:
            ok = False
            break
elif H % 2 == W % 2 == 1:
    two_num = H + W - 1
    one_num = 1
    four_num = H * W - two_num - one_num
    if not valid(two_num, four_num, one_num, c):
        ok = False
else:
    if W % 2 == 0:
        two_num = W
    else:
        two_num = H
    four_num = H * W - two_num
    if not valid(two_num, four_num, 0, c):
        ok = False
    
if ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
H,W=map(int,input().split())
if H==1 or W==1:
  print("Yes")
  sys.exit()
L=[0]*26
for i in range(H):
  A=str(input())
  for j in range(W):
    L[ord(A[j])-97]+=1

fours0=0
fours1=0
fours2=0
fours3=0
for i in range(26):
  if L[i]%4==0:
    fours0+=1
  if L[i]%4==1:
    fours1+=1
  if L[i]%4==2:
    fours2+=1
  if L[i]%4==3:
    fours3+=1

if W%2==0 and H%2==0:
  if fours1+fours2+fours3==0: print("Yes")
  else: print("No")
elif W%2==1 and H%2==1:
  if fours1+fours3<=1 and fours1+fours3+2*fours2<=W+H-1:
    print("Yes")
  else: print("No")
elif W%2==1: 
  if fours1+fours3==0 and fours2*2<=H:
    print("Yes")
  else: print("No")
else: 
  if fours1+fours3==0 and fours2*2<=W:
    print("Yes")
  else: print("No")

Submission status: Wrong Answer

Solution code:
import sys
H,W=map(int,input().split())
if H==1 or W==1:
  print("Yes")
  sys.exit()
L=[0]*26
for i in range(H):
  A=str(input())
  for j in range(W):
    L[ord(A[j])-97]+=1

fours0=0
fours1=0
fours2=0
fours3=0
for i in range(26):
  if L[i]%4==0:
    fours0+=1
  if L[i]%4==1:
    fours1+=1
  if L[i]%4==2:
    fours2+=1
  if L[i]%4==3:
    fours3+=1

if W%2==0 and H%2==0:
  if fours1+fours2+fours3==0: print("Yes")
  else: print("No")
elif W%2==1 and H%1==1:
  if fours1+fours3<=1 and fours1+fours3+2*fours2<=W+H-1:
    print("Yes")
  else: print("No")
elif W%2==1: 
  if fours1+fours3==0 and fours2*2<=H:
    print("Yes")
  else: print("No")
else: 
  if fours1+fours3==0 and fours2*2<=W:
    print("Yes")
  else: print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

def valid(two_num, four_num, c):
    t = 0
    f = 0
    for k, v in c.items():
        while v > 0:
            if v >= 4:
                f += 1
                v -= 4
            if v >= 2:
                t += 1
                v -= 2
            if v == 1:
                return False
    return f * 4 >= four_num
        

H, W = map(int, input().split())
c = Counter()
for h in range(H):
    s = input()
    c.update(s)

ok = True
if H == W == 1:
    pass
elif H == 1 or W == 1:
    HW = H * W
    odd_num = sum(1 for x in c.values() if x % 2)
    even_num = HW - odd_num
    if HW % 2 == 1 and odd_num != 1: ok = False
    elif HW % 2 == 0 and odd_num != 0: ok = False
elif H % 2 == W % 2 == 0:
    # all 4
    for k, v in c.items():
        if v % 4 != 0:
            ok = False
            break
elif H % 2 == W % 2 == 1:
    two_num = H + W - 1
    four_num = H * W - two_num
    if not valid(two_num, four_num, c):
        ok = False
else:
    if W % 2 == 0:
        two_num = W
    else:
        two_num = H
    four_num = H * W - two_num
    if not valid(two_num, four_num, c):
        ok = False
    
if ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

count = {}
for _ in range(H):
    for s in input().strip():
        count[s] = count.get(s, 0) + 1

req = [0] * 4

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

for r in [4, 2, 1]:
    for i in range(req[r-1]):
        for s in count:
            if count[s] >= r:
                count[s] -= r
                break
        else:
            print('No')
            exit(0)
print('Yes')

Submission status: Wrong Answer

Solution code:
def func() :
    n,m=map(int,input().split())
    a={}
    for i in range(n) :
        s = input()
        for c in list(s):
            if c in a:
                a[c]+=1
            else :
                a[c] = 1
    res = 0
    for k,v in a.items() :
        if v%2==1 :
            res += 1
            if res > 1 :
                print('No')
                return
    print('Yes')
func()

Submission status: Wrong Answer

Solution code:
import sys
H,W=map(int,input().split())
if H==1 or W==1:
  print("Yes")
  sys.exit()
L=[0]*26
for i in range(H):
  A=str(input())
  for j in range(W):
    L[ord(A[j])-97]+=1

fours0=0
fours1=0
fours2=0
fours3=0
for i in range(26):
  if L[i]%4==0:
    fours0+=1
  if L[i]%4==1:
    fours1+=1
  if L[i]%4==2:
    fours2+=1
  if L[i]%4==3:
    fours3+=1

if W%2==0 and H%2==0:
  if fours1+fours2+fours3==0: print("Yes")
  else: print("No")
elif W%2==1 and H%1==0:
  if fours1+fours3<=1 and fours1+fours3+2*fours2<=W+H-1:
    print("Yes")
  else: print("No")
elif W%2==1: 
  if fours1+fours3==0 and fours2*2<=H:
    print("Yes")
  else: print("No")
else: 
  if fours1+fours3==0 and fours2*2<=W:
    print("Yes")
  else: print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import Counter

def check():
    h, w = map(int, input().split()) 
    c = Counter()
    for i in range(h):
        c.update(input())
    cc = {}
    cc[4] = (h >> 1) * (w >> 1)
    cc[2] = (h & 1) * (w >> 1) + (w & 1) * (h >> 1)
    cc[1] = (h & 1) * (w >> 1)
    tt = list(c.values())
    for v in [4, 2, 1]:
        for i in range(len(tt)):
            k = min(cc[v], tt[i] // v)
            tt[i] -= k * v
            cc[v] -= k
    return sum(tt) == 0


print(["No", "Yes"][check()])

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
C = {}
for i in range(h):
    for c in input():
        C[c] = C.get(c, 0) + 1
if h == 1 or w == 1:
    odd = 0
    for c in C:
        if C[c] % 2 == 1:
            odd += 1
    if h % 2 == 1 and w % 2 == 1:
        if odd == 1:
            print('Yes')
        else:
            print('No')
    else:
        if odd == 0:
            print('Yes')
        else:
            print('No')
else:
    four = 0
    odd = 0
    for c in C:
        if C[c] % 2 == 1:
            odd += 1
        if C[c] >= 4:
            four += 1
    if h % 2 == 1 and w % 2 == 1:
        if four > 0 and odd == 1:
            print('Yes')
        else:
            print('No')
    else:
        if four > 0 and odd == 0:
            print('Yes')
        else:
            print('No')

Submission status: Time Limit Exceeded

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:
    while ca[i]>0:
      if iti<0:
        break
      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
  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: Time Limit Exceeded

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:
    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
      elif iti>0:
        iti-=1
        ca[i]-=1
  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: Accepted

Solution code:
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
 
cnt = {}
for ai in a:
    for c in ai:
        if c in cnt:
            cnt[c] += 1
        else:
            cnt[c] = 1
c1 = 0
c2 = 0
c4 = 0
for k, v in cnt.items():
    c4 += (v // 4) * 4
    c2 += ((v % 4) // 2) * 2
    c1 += v % 2
 
ans = "No"
if h % 2 == 0:
    if w % 2 == 0:
        if c4 == h * w:
            ans = "Yes"
    else:
        if c2 <= h and c1 == 0:
            ans = "Yes"
else:
    if w % 2 == 0:
        if c2 <= w and c1 == 0:
            ans = "Yes"
    else:
        if c2 <= h + w - 1 and c1 == 1:
            ans = "Yes"
print(ans)

Submission status: Accepted

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 h % 2 == 0:
        tmp = h // 2
    else:
        tmp = w // 2
    if one > 0:
        print("No")
    elif two > tmp:
        print("No")
    else:
        print("Yes")

Submission status: Accepted

Solution code:
from collections import Counter
H, W = map(int, input().split())
s = ''
for i in range(H):
  s += str(input())
  
c = Counter(s)
m = c.most_common()
cnt = 0
l = []
for n in m:
  cnt += n[1] // 4
  l.append(n[1]%4)
if cnt < (H//2)*(W//2):
  print('No')
else:
  cnt = 0
  for i in l:
    if i % 2 == 1:
      cnt += 1
  if cnt <= 1:
    print('Yes')
  else:
    print('No')

Submission status: Accepted

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: Accepted

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:
        if sum(x) > H+W-1:
            return no
        o = 0
        for y in x:
            if y&1:
                o += 1
        
        if o == 1:
            return yes
        
        return no

print(solve())

Submission status: Accepted

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 % 4

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: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7
def intread():
    return int(sysread())
def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())


def run():
    H, W = mapline()
    dic = [0] * 27
    for i in range(H):
        S = input()
        for s in S:
            dic[ord(s) - 96] += 1
    permited2 = 0
    permited1 = 0
    if H % 2 and W % 2:
        permited2 += H // 2 + W // 2
        permited1 += 1
    else:
        if H % 2:
            permited2 += W // 2
        elif W % 2:
            permited2 += H // 2
    permited4 = (H * W - permited2 * 2 - permited1) // 4
    for val in dic:
        done = True
        while val and done:
            done = False
            if val >= 4 and permited4:
                val -= 4
                permited4 -= 1
                done= True
            elif val >= 2 and permited2:
                val -= 2
                permited2 -= 1
                done = True
            elif val == 1 and permited1:
                val -= 1
                permited1 -= 1
                done = True
            if not done:
                print('No')
                return
    print('Yes')


if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
import heapq

h,w=map(int,input().split())

a=[input() for _ in range(h)]

alphabet={}

for i in range(h):
    for j in range(w):
        if a[i][j] in alphabet:
            alphabet[a[i][j]]+=1
        else:
            alphabet[a[i][j]]=1

same=[-x for x in alphabet.values()]
heapq.heapify(same)

cnt={}

for i in range(h):
    for j in range(w):
        hh=min(i,h-i-1)
        ww=min(j,w-j-1)

        if (hh,ww) in cnt:
            cnt[(hh,ww)]+=1
        else:
            cnt[(hh,ww)]=1

cntv=sorted(cnt.values())

while cntv:
    c=heapq.heappop(same)
    c=-c
    ccnt=cntv.pop()

    if ccnt>c:
        print('No')
        exit(0)
    elif ccnt<c:
        heapq.heappush(same,ccnt-c)

print('Yes')

Submission status: Accepted

Solution code:

from collections import Counter, defaultdict
def resolve():
    H, W = map(int, input().split())
    S = Counter()
    for _ in range(H):
        S.update(list(input()))

    dd = defaultdict(int)
    for k, v in S.items():
        q, r = divmod(v, 4)
        dd[4] += q
        qq, rr = divmod(r, 2)
        dd[2] += qq
        dd[1] += rr

    ans = "Yes"

    if H % 2 == 0 and W % 2 == 0:
        if dd[2] != 0 or dd[1] != 0:
            ans = "No"
    elif H % 2 == 0:
        if dd[1] != 0 or dd[2] > H // 2:
            ans = "No"
    elif W % 2 == 0:
        if dd[1] != 0 or dd[2] > W // 2:
            ans = "No"
    else:
        if dd[1] != 1:
            ans = "No"
        else:
            if dd[2] > H // 2 + W // 2:
                ans = "No"

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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 sorted(d.items(),key=lambda x:x[1]):
            if d[k] >= len(s):
                d[k] -= len(s)
                f = False
                break
        if f:
            print("No")
            exit(0)
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
count = [0]*26
l = [input() for i in range(h)]
for i in l:
    for j in i:
        count[ord(j)-ord("a")] += 1

if h%2:
    if w%2:
        c = 0
        c2 = 0
        for i in count:
            if i%4:
                if i%2:
                    c2 += 1
                else:
                    c += 1
        if c <= w//2+h//2 and c2 == 1:
            print("Yes")
        else:
            print("No")
    
    else:
        c = 0
        for i in count:
            if i%4:
                if i%2:
                    print("No")
                    exit()
                else:
                    c += 1
        if c <= w//2:
            print("Yes")
        else:
            print("No")
else:
    if w%2:
        c = 0
        for i in count:
            if i%4:
                if i%2:
                    print("No")
                    exit()
                else:
                    c += 1
        if c <= h//2:
            print("Yes")
        else:
            print("No")
            
    else:
        for i in count:
            if i%4:
                print("No")
                exit()
        print("Yes")