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:
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
  print(E)
  if (a+1)//2 <len(E):
    print('No')
    exit()
  for i in range(a//2+1):
    if E==[1]+[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')

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 (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')

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 (a+1)//2<len(E):
    print('No')
    exit()
  for i in range(a//2+1):
    if E==[1]+[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==1:
    a=W
  else:
    a=H
  print(E)
  if a//2<len(E):
    print('No')
    exit()
  for i in range(a):
    if E==[2]*(2*i):
      print('Yes')
      exit()
  print('No')

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
  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==1:
    a=W
  else:
    a=H
  for i in range(a):
    if E==[2]*i:
      print('Yes')
      exit()
  print('No')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a_row = [input() for _ in range(H)]
a_col = [''.join([a_row[j][i] for j in range(H)]) for i in range(W)]

def count(a):
    cnt = 0
    s = set()
    for i in a:
        if i in s:
            s.remove(i)
            cnt += 1
        else:
            s.add(i)
    return cnt

if count(a_row) != H//2 or count(a_col) != W//2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a_row = [input() for _ in range(H)]
cnt = 0
s = set()
for row in a_row:
    if row in s:
        s.remove(row)
        cnt += 1
    else:
        s.add(row)
if cnt != H//2:
    print('No')
    exit()
a_col = [''.join([a_row[j][i] for j in range(H)]) for i in range(W)]
cnt = 0
s = set()
for col in a_col:
    if col in s:
        s.remove(col)
        cnt += 1
    else:
        s.add(col)
if cnt != W//2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a = [list(input()) for _ in range(H)]
a_row = sorted(a)
cnt = 0
stack = []
for i in range(H):
    if stack and stack[-1] == a_row[i]:
        stack.pop()
        cnt += 1
    else:
        stack.append(a_row[i])
if cnt != H//2:
    print('No')
    exit()
a_col = sorted([[a[j][i] for j in range(H)] for i in range(W)])
cnt = 0
stack = []
for i in range(W):
    if stack and stack[-1] == a_col[i]:
        stack.pop()
        cnt += 1
    else:
        stack.append(a_col[i])
if cnt != W//2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a = [input() for _ in range(H)]
a_row = sorted(a)
for i in range(1, H, 2):
    if a_row[i] != a_row[i-1]:
        print('No')
        exit()
a_col = sorted([[a[j][i] for j in range(H)] for i in range(W)])
for i in range(1, W, 2):
    if a_col[i] != a_col[i-1]:
        print('No')
        exit()
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import defaultdict

def main():
    H,W = map(int,input().split())
    d = defaultdict(int)
    for _ in range(H):
        s = input()
        for st in s:
            num = ord(st)-ord("a")
            d[num] += 1
    
    use = list(d.values())
    m = [0,0,0]
    for num in use:
        m[0] += num//4
        num -= num//4*4
        m[1] += num//2
        m[2] += num%2

    if (H%2 == 0 and W%2 == 0):
        if H*W == 4*m[0]:
            print("Yes")
        else:
            print("No")
    elif (H*W)%2 == 1:
        if m[2] != 1:
            print("No")
        else:
            if m[0]*4+m[1]*2 == H*W-1:
                print("Yes")
            else:
                print("No")
    else:
        if m[2] != 0:
            print("No")
        else:
            if H%2 == 1:
                if m[1] <= W//2:
                    print("Yes")
                else:
                    print("No")
            else:
                if m[1] <= H//2:
                    print("Yes")
                else:
                    print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import defaultdict

def main():
    H,W = map(int,input().split())
    d = defaultdict(int)
    for _ in range(H):
        s = input()
        for st in s:
            num = ord(st)-ord("a")
            d[num] += 1
    
    use = list(d.values())
    m = [0,0,0]
    for num in use:
        m[0] += num//4
        num -= num//4*4
        m[1] += num//2
        m[2] += num%2

    if (H%2 == 0 and W%2 == 0):
        if H*W == 4*m[0]:
            print("Yes")
        else:
            print("No")
    elif (H*W)%2 == 1:
        if m[2] != 1:
            print("No")
        else:
            if m[0]*4 == H*W-1:
                print("Yes")
            else:
                print("No")
    else:
        if m[2] != 0:
            print("No")
        else:
            if m[0]*4+m[1]*2 == H*W:
                print("Yes")
            else:
                print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import defaultdict

def main():
    H,W = map(int,input().split())
    d = defaultdict(int)
    for _ in range(H):
        s = input()
        for st in s:
            num = ord(st)-ord("a")
            d[num] += 1
    
    use = list(d.values())
    m = [0,0,0]
    for num in use:
        m[0] += num//4
        num -= num//4*4
        m[1] += num//2
        m[2] += num%2

    if (H%2 == 0 and W%2 == 0):
        if H*W == 4*m[0]:
            print("Yes")
        else:
            print("No")
    elif (H*W)%2 == 1:
        if m[2] != 1:
            print("No")
        else:
            if m[0]*4+m[1]*2 == H*W-1:
                print("Yes")
            else:
                print("No")
    else:
        if m[2] != 0:
            print("No")
        else:
            if m[0]*4+m[1]*2 == H*W:
                print("Yes")
            else:
                print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
A = []
for _ in range(H):
    A += [x for x in input()]
if H % 2 != 0 and W % 2 != 0:
    g1 = 1
else:
    g1 = 0
g2 = H // 2 + W // 2
g4 = (H // 2) * (W // 2)

counter = Counter(A)
cnt = 0
for k, v in counter.items():
    if cnt == g1:
        break

    if v % 4 == 1 or v % 4 == 3:
        counter[k] -= 1
        cnt += 1

cnt = 0
for k, v in counter.items():
    if cnt == g2:
        break

    if v % 4 == 2:
        counter[k] -= 2
        cnt += 1

cnt = 0
for k, v in counter.items():
    if cnt == g4:
        break

    if v % 4 == 0:
        counter[k] -= 4
        cnt += 1

for k, v in counter.items():
    if v != 0:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
A = []
for _ in range(H):
    A += [x for x in input()]
g1 = 1
g2 = H // 2 + W // 2
g4 = (H // 2) * (W // 2)
counter = Counter(A)
cnt = 0
for k, v in counter.items():
    if cnt == g1:
        break

    if v % 4 == 1 or v % 4 == 3:
        counter[k] -= 1
        cnt += 1


cnt = 0
for k, v in counter.items():
    if cnt == g2:
        break

    if v % 4 == 2:
        counter[k] -= 2
        cnt += 1


cnt = 0
for k, v in counter.items():
    if cnt == g4:
        break

    if v % 4 == 0:
        counter[k] -= 4
        cnt += 1

for k, v in counter.items():
    if v != 0:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
h,w=map(int,input().split())
a=list(sys.stdin.read())
c=Counter(a)
c["\n"]=0
n1=0
n2=0
if h*w%2:
  n1=1
if h%2:
  n2+=w//2
if w%2:
  n2+=h//2
n4=h//2+w//2
n1r,n2r,n4r=0,0,0
for i in c.values():
  n1r += i%2
  n2r += (i//2)%2
  n4r += i//4
if n1==n1r and n4r>=n4:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W=map(int, input().split())
A="".join(input() for _ in range(H))
C=Counter(A)
R=[0]*5
for v in C.values():
    R[4]+=v//4
    v-=v//4*4
    R[2]+=v//2
    v-=v//2*2
    R[1]+=v
b1=R[4]>=(H//2)*(W//2)
R[4]-=(H//2)*(W//2)
x2=(H&1)*(W//2)+(W&1)*(H//2)
R[2]+=R[4]*2
R[2]-=x2
b=R[4]==0 and R[2]==0 and R[1]==(H&1 and W&1)
print("YNeos"[not b::2])

Submission status: Wrong Answer

Solution code:
print("No")

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby
h,w = map(int,stdin.readline().rstrip().split())
s = ""
for i in range(h):
    s += stdin.readline().rstrip()
s = list(s)
s.sort()
lin = []
for key, value in groupby(s):
    lin.append(len(list(value)))
lin.sort(reverse=True)
task_4 = (h//2)*(w//2)
task_2 = (h%2)*(h//2)+(w%2)*(w//2)
task_1 = (h%2)*(w%2)
for i in range(task_4):
    if lin[0] >= 4:
        lin[0] -= 4
        lin.sort(reverse=True)
    else:
        print("No")
        exit()
for i in range(task_2):
    if lin[0] >= 2:
        lin[0] -= 2
        lin.sort(reverse=True)
    else:
        print("No")
        exit()
for i in range(task_1):
    if lin[0] >= 1:
        lin[0] -= 1
        lin.sort(reverse=True)
    else:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
counter = Counter()
for h in range(H):
    row = input()
    for a in row:
        counter[a] += 1
ans = True
max_a = 0
if (H * W) % 2 == 0:
    for k, v in counter.items():
        if v % 2 == 0:
            max_a = max(max_a, v)
            continue
        ans = False
        break
    
else:
    odd_count = 0
    for k, v in counter.items():
        if v % 2 == 0:
            max_a = max(max_a, v)
            continue
        odd_count += 1
    if odd_count != 1:
        ans = False
if ans and (max_a >= 4 or (H == 1 or W == 1)):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
# 要求されるカルテットを条件ごとに求めて、その後要求されるペアを求めているが２つWA

from collections import Counter
w = ''
H,W = map(int,input().split())
for _ in range(H):
    w += input()
freq = Counter(list(w))
#print(freq)

quartet = 0
pair = 0
if H%2 == 0 and W%2 == 0:
    for v in freq.values():
        if v%4 != 0:
            print('No')
            exit()
    print('Yes')
    exit()

elif H%2 == 0 and W%2 != 0:

    for k,v in freq.items():
        #if freq[k] %4 ==0:
        if freq[k]>=4:
            quartet += v//4
            freq[k] -= 4*(v//4)
    if quartet < (H//2)*(W/2):
        print('No')
        exit()
    else:
        pair += 2*(quartet-(H//2)*(W//2)) # 余分なカルテットをペアに分解

    for k,v in freq.items():
        if freq[k] >= 2:
            pair += v//2
            freq[k] -= 2*(v//2)
    if pair != H//2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()


elif H%2 != 0 and W%2 == 0:
    for k, v in freq.items():
        #if freq[k] % 4 == 0:
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        if freq[k] >= 2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != W // 2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()

else:
    for k, v in freq.items():
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        #if freq[k] % 2 == 0:
        if freq[k] >=2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != (W//2 + H//2):
        print('No')
        exit()
    else:
        print('Yes')
        exit()

Submission status: Wrong Answer

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

quartet = 0
pair = 0
if H%2 == 0 and W%2 == 0:
    for v in freq.values():
        if v%4 != 0:
            print('No')
            exit()
    print('Yes')
    exit()

elif H%2 == 0 and W%2 != 0:

    for k,v in freq.items():
        #if freq[k] %4 ==0:
        if freq[k]>=4:
            quartet += v//4
            freq[k] -= 4*(v//4)
    if quartet < (H//2)*(W/2):
        print('No')
        exit()
    else:
        pair += 2*(quartet-(H//2)*(W//2)) # 余分なカルテットをペアに分解

    for k,v in freq.items():
        if freq[k] >= 2:
            pair += v//2
            freq[k] -= 2*(v//2)
    if pair != H//2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()


elif H%2 != 0 and W%2 == 0:
    for k, v in freq.items():
        #if freq[k] % 4 == 0:
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        if freq[k] >= 2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != W // 2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()

else:
    for k, v in freq.items():
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        #if freq[k] % 2 == 0:
        if freq[k] >=2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != (W//2 + H//2):
        print('No')
        exit()
    else:
        print('Yes')
        exit()

Submission status: Wrong Answer

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

quartet = 0
pair = 0
if H%2 == 0 and W%2 == 0:
    for v in freq.values():
        if v%4 != 0:
            print('No')
            exit()

elif H%2 == 0 and W%2 != 0:

    for k,v in freq.items():
        #if freq[k] %4 ==0:
        if freq[k]>=4:
            quartet += v//4
            freq[k] -= 4*(v//4)
    if quartet < (H//2)*(W/2):
        print('No')
        exit()
    else:
        pair += 2*(quartet-(H//2)*(W//2)) # 余分なカルテットをペアに分解

    for k,v in freq.items():
        if freq[k] >= 2:
            pair += v//2
            freq[k] -= 2*(v//2)
    if pair != H//2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()


elif H%2 != 0 and W%2 == 0:
    for k, v in freq.items():
        #if freq[k] % 4 == 0:
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        if freq[k] >= 2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != W // 2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()

else:
    for k, v in freq.items():
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        #if freq[k] % 2 == 0:
        if freq[k] >=2:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != (W//2 + H//2):
        print('No')
        exit()
    else:
        print('Yes')
        exit()

Submission status: Wrong Answer

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

quartet = 0
pair = 0
if H%2 == 0 and W%2 == 0:
    for v in freq.values():
        if v%4 != 0:
            print('No')
            exit()

elif H%2 == 0 and W%2 != 0:

    for k,v in freq.items():
        #if freq[k] %4 ==0:
        if freq[k]>=4:
            quartet += v//4
            freq[k] -= 4*(v//4)
    if quartet < (H//2)*(W/2):
        print('No')
        exit()
    else:
        pair += 2*(quartet-(H//2)*(W//2)) # 余分なカルテットをペアに分解

    for k,v in freq.items():
        if freq[k] %2 == 0:
            pair += v//2
            freq[k] -= 2*(v//2)
    if pair != H//2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()


elif H%2 != 0 and W%2 == 0:
    for k, v in freq.items():
        #if freq[k] % 4 == 0:
        if freq[k] >= 4:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        if freq[k] % 2 == 0:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != W // 2:
        print('No')
        exit()
    else:
        print('Yes')
        exit()

else:
    for k, v in freq.items():
        if freq[k] % 4 == 0:
            quartet += v // 4
            freq[k] -= 4 * (v // 4)
    if quartet < (H // 2) * (W // 2):
        print('No')
        exit()
    else:
        pair += 2 * (quartet - (H // 2) * (W // 2))  # 余分なカルテットをペアに分解

    for k, v in freq.items():
        if freq[k] % 2 == 0:
            pair += v // 2
            freq[k] -= 2 * (v // 2)
    if pair != (W//2 + H//2):
        print('No')
        exit()
    else:
        print('Yes')
        exit()

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w=map(int,input().split())
cnt=Counter()
for i in range(h):
  c=Counter(input())
  cnt+=c
sum1=0
sum2=0
sum4=0
for i in cnt.values():
  sum4+=i//4
  sum2+=(i-sum4*4)//2
  sum1+=i-sum4*4-sum2*2
if h%2==0 and w%2==0:
  print('Yes' if sum1==0 and sum2==0 else 'No')
elif h%2==1 and w%2==1:
  print('Yes' if sum1==1 and sum4>=((w-1)*(h-1))/4 else 'No')
elif h%2==1 and w%2==0:
  print('Yes' if sum1==0 and sum2<=w/2 else 'No')
else:
  print('Yes' if sum1==0 and sum2<=h/2 else 'No')

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

mod1=0
for k in d:
    if d[k] % 4 == 0:
        mod4 += 1
    elif d[k] % 2 == 0:
        mod2 += 1
    elif d[k]%2==1:
        mod1+=1


am=H%2
bm=W%2

A1=0

if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
    A2=0
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4
    A2=H/2
elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4
    A2=W/2
else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4
    A2=W+H-2
    A1=1

#print(resid4,mod2)

res = False

#print(A2,mod2)
if A2>=mod2 and A1>=mod1:
    res=True

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

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

mod1=0
for k in d:
    if d[k] % 4 == 0:
        mod4 += d[k]
    elif d[k] % 2 == 0:
        mod2 += d[k]
    elif d[k]%2==1:
        mod1+=1


am=H%2
bm=W%2

A1=0

if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
    A2=0
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4
    A2=H
elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4
    A2=W
else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4
    A2=W+H-2
    A1=1

#print(resid4,mod2)

res = False

#print(mod2)
if A2>=mod2 and A1>=mod1:
    res=True

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

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

mod1=0
for k in d:
    if d[k] % 4 == 0:
        mod4 += d[k]
    elif d[k] % 2 == 0:
        mod2 += 1
    elif d[k]%2==1:
        mod1+=1


nodd = H % 2+W % 2

am=H%2
bm=W%2

A1=0

if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
    A2=0
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4
    A2=H
elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4
    A2=W
else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4
    A2=W+H-2
    A1=1

#print(resid4,mod2)

res = False

#print(mod2)
if A2>=mod2 and A1>=mod1:
    res=True

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

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

for k in d:
    if d[k] % 4 == 0:
        mod4 += d[k]
    elif d[k] % 2 == 0:
        mod2 += d[k]


nodd = H % 2+W % 2

am=H%2
bm=W%2


if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
    A2=0
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4
    A2=H
elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4
    A2=W
else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4
    A2=W+H-1-3

#print(A2)
res = False
if resid4 > 0:    
    mod2 -= resid4
else:
    mod2+=resid4
if mod2>=A2:
    res=True

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

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

for k in d:
    if d[k] % 4 == 0:
        mod4 += d[k]
    elif d[k] % 2 == 0:
        mod2 += d[k]


nodd = H % 2+W % 2

am=H%2
bm=W%2

res = False
if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
    A2=0
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4
    A2=H
elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4
    A2=W
else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4
    A2=W+H-2

#print(A2)
if resid4 > 0:    
    mod2 -= resid4
else:
    mod2+=resid4
if mod2>=A2:
    res=True

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

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))



H, W = acinput()

# d=[0]*26
d = {}

for i in range(H):

    tmp = list(input())
    for c in tmp:
        try:
            d[c] += 1
        except:
            d[c] = 1
mod2 = mod4 = 0

for k in d:
    if d[k] % 4 == 0:
        mod4 += d[k]
    elif d[k] % 2 == 0:
        mod2 += d[k]


nodd = H % 2+W % 2

am=H%2
bm=W%2

res = False
if am==0 and bm==0:
    resid4 = np.floor((H)*(W))-mod4
elif am==0 and bm==1:
    resid2 = np.floor(W-1)-mod2
    resid4 = np.floor((H-1)*(W))-mod4

elif am==1 and bm==0:
    resid2 = np.floor(W-1)-mod2    
    resid4=np.floor((H-1)*(W))-mod4

else:
    resid2 = np.floor(W-1)+H-1-mod2
    resid4 = np.floor((H-1)*(W-1))-mod4

if resid4 > 0:    
    mod2 -= resid4
else:
    mod2+=resid4
if mod2>=W-1:
    res=True

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

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    h,w = inpm()
    a = inplT(h)
    dic = defaultdict(int)
    for i in range(h):
        for j in range(w):
            x = ord(a[i][j])
            dic[x] += 1
    aaa = 0
    q = 0
    for e in dic:
        if dic[e] != 1 and dic[e]%2 == 1:
            aaa += 1
            q = dic[e]
            if aaa == 2:
                print('No')
                return
    if h%2 == 0 and w%2 == 0:
        for e in dic:
            if dic[e]%4 != 0:
                print('No')
                return
    if (h%2 == 0 and w%2 == 1) or (h%2 == 1 and w%2 == 0):
        cnt = 0
        if aaa:
            print('No')
            return
        else:
            for e in dic:
                if dic[e]%2 != 0:
                    print('No')
                    return
                if dic[e]%4 == 2:
                    cnt += 1
            if h%2 == 0:
                key = h//2
            else:
                key = w//2
            if (key-cnt) < 0 or (key-cnt)%2 != 0:
                print('No')
                return
    if h%2 == 1 and w%2 == 1:
        cnt1 = 0
        cnt2 = 0
        if aaa:
            for e in dic:
                if dic[e] == 1:
                    cnt1 += 1
                elif dic[e]%4 == 2:
                    cnt2 += 1
            if cnt1 != 1:
                print('No')
                return
            
            key = (h//2) + (w//2)
            if (key-cnt2) < 0 or (key-cnt2)%2 != 0:
                print('No')
                return
        else:
            for e in dic:
                if dic[e] == 1:
                    cnt1 += 1
                elif dic[e]%4 == 2:
                    cnt2 += 1
            if cnt1 != 1:
                print('No')
                return
            q -= 1
            if q%4 == 2:
                cnt2 += 1
            key = (h//2) + (w//2)
            if (key-cnt2) < 0 or (key-cnt2)%2 != 0:
                print('No')
                return
    print('Yes')

if __name__ == "__main__":
    main()