Problem:


Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())
if H % 2 != 0 and W % 2 != 0:  # どちらかが偶数であれば要素が1つのグループは存在しない
    g1 = 1
else:
    g1 = 0

g2 = int(H/2) + int(W/2)
g4 = int(H/2) * int(W/2)
flag = True

for _ in range(g1):
    for i in range(len(count_list)):
        if count_list[i] % 4 in [1, 3]:
            count_list[i] -= 1
            break

for _ in range(g2):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2:
            count_list[i] -= 2
            count_list = sorted(count_list, reverse=True)
            break
for _ in range(g4):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 0:
            count_list[i] -= 4
            count_list = sorted(count_list, reverse=True)
            break

if any(count_list):
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())
if H % 2 == 0 or W % 2 == 0:  # どちらかが偶数であれば要素が1つのグループは存在しない
    g1 = 0
else:
    g1 = 1

g2 = int(H/2) + int(W/2)
g4 = int(H/2) * int(W/2)
flag = True

for _ in range(g1):
    for i in range(len(count_list)):
        if count_list[i] % 4 in [1, 3]:
            count_list[i] -= 1

for _ in range(g2):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2:
            count_list[i] -= 2

for _ in range(g4):
    for i in range(len(count_list)):
        if count_list[i] % 4 == 0:
            count_list[i] -= 4

if any(count_list):
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())
if H % 2 == 0 or W % 2 == 0:  # どちらかが偶数であれば要素が1つのグループは存在しない
    g1 = 0
else:
    g1 = 1

g2 = int(H/2) + int(W/2)
g4 = int(H/2) * int(W/2)
flag = True
while sum(count_list) != 0:
    tmp = sum(count_list)
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2 and g2 > 0:
            count_list[i] -= 2
            g2 -= 1
        elif count_list[i] % 4 == 0 and count_list[i] > 0 and g4 > 0:
            count_list[i] -= 4
            g4 -= 1
        elif count_list[i] % 4 in [1, 3] and g1 > 0:
            count_list[i] -= 1
            g1 -= 1

    if tmp == sum(count_list):
        flag = False
        break

if flag and g4 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())
if H % 2 == 0 or W % 2 == 0:  # どちらかが偶数であれば要素が1つのグループは存在しない
    g1 = 0
else:
    g1 = 1

g2 = int(H/2) + int(W/2)
g4 = int(H/2) * int(W/2)
flag = True
while sum(count_list) != 0:
    tmp = sum(count_list)
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2 and g2 > 0:
            count_list[i] -= 2
            g2 -= 1
        elif count_list[i] % 4 == 0 and count_list[i] > 0 and g4 > 0:
            count_list[i] -= 4
            g4 -= 1
        elif count_list[i] % 2 == 1 and g1 > 0:
            count_list[i] -= 1
            g1 -= 1

    if tmp == sum(count_list):
        flag = False
        break

if flag and g1 == 0 and g2 == 0 and g4 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
S = ""
for _ in range(H):
    a = str(input())
    S += a

C = Counter(S)
count_list = list(C.values())
if H % 2 == 0 or W % 2 == 0:  # どちらかが偶数であれば要素が1つのグループは存在しない
    g1 = 0
else:
    g1 = 1

g2 = int(H/2) + int(W/2)
g4 = int(H/2) * int(W/2)
flag = True
while sum(count_list) != 0:
    tmp = sum(count_list)
    for i in range(len(count_list)):
        if count_list[i] % 4 == 2 and g2 > 0:
            count_list[i] -= 2
            g2 -= 1
        elif count_list[i] % 4 == 0 and count_list[i] > 0 and g4 > 0:
            count_list[i] -= 4
            g4 -= 1
        elif count_list[i] % 2 == 1 and g1 > 0:
            count_list[i] -= 1
            g1 -= 1

    if tmp == sum(count_list):
        flag = False
        break

if flag and g4 == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections
H, W = map(int, input().split())
table = collections.defaultdict(int)
for i in range(H):
    s = input()
    for x in s:
        table[x] += 1
count = [0, 0, 0]
for x in table:

    if table[x] % 4 == 0:
        count[0] += 1
    elif table[x] % 4 == 2:
        count[1] += 1
    elif table[x] % 4 == 1:
        count[2] += 1

if H % 2 == 0 and W % 2 == 0:
    if count[1] > 0 or count[2] > 0:
        print("No")
    else:
        print("Yes")
elif H % 2 == 0 or W % 2 == 0:
    if count[2] > 1:
        print("No")
    else:
        if H % 2 == 1 and count[1] > W:
            print("No")
        elif W % 2 == 1 and count[1] > H:
            print("No")
        else:
            print("Yes")
else:
    if count[2] != 1:
        print("No")
    elif count[1] > (H-1) + (W-1):
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

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

cA = Counter(A)

if H % 2 == 1 and W % 2 == 1:
  one = 1
  two = H // 2 + W // 2
  four = (H // 2) * (W // 2)
else:
  one = 0
  two = (H * (W % 2) + W * (H % 2)) // 2
  four = (H // 2) * (W // 2)

# print(one, two, four)

vals =[val for val in cA.values()]

# print(vals)

# one
while one > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if m == 1 or m == 3:
      vals[i] -= 1
      break # 
  one -= 1

# print(vals)    

# four
while four > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if vals[i] != 0 and m == 0:
      vals[i] -= 4
      break
  four -= 1

# print(vals)      
        
# two
while two > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 2)
    if vals[i] != 0 and m == 0:
      vals[i] -= 2
      break
  two -= 1
          
# print(vals)

if all([val == 0 for val in vals]):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

cA = Counter(A)

if H % 2 == 1 and W % 2 == 1:
  one = 1
  two = H // 2 + W // 2
  four = (H // 2) * (W // 2)
else:
  one = 0
  two = H * (W % 2) // 2 + W * (H % 2) // 2
  four = (H // 2) * (W // 2) * 4

# print(one, two, four)

vals =[val for val in cA.values()]

# print(vals)

# one
while one > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if m == 1 or m == 3:
      vals[i] -= 1
      break # 
  one -= 1

# print(vals)    

# two
while two > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if m == 2:
      vals[i] -= 2
      break
  two -= 1
          
# print(vals)

# four
while four > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if vals[i] != 0 and m == 0:
      vals[i] -= 4
      break
  four -= 1

# print(vals)      
        
if all([val == 0 for val in vals]):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

cA = Counter(A)

if H % 2 == 1 and W % 2 == 1:
  one = 1
  two = H // 2 + W // 2
  four = (H // 2) * (W // 2)
else:
  one = 0
  two = H * (W % 2) // 2 + W * (H % 2) // 2
  four = (H // 2) * (W // 2)

# print(one, two, four)

vals =[val for val in cA.values()]

# print(vals)

# one
while one > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if vals[i] != 0 and (m == 1 or m == 3):
      vals[i] -= 1
      break # 
  one -= 1

# print(vals)    

# two
while two > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if vals[i] != 0 and m == 2:
      vals[i] -= 2
      break
  two -= 1
          
# print(vals)

# four
while four > 0:
  for i in range(len(vals)):
    _, m = divmod(vals[i], 4)
    if vals[i] != 0 and m == 0:
      vals[i] -= 4
      break
  four -= 1

# print(vals)      
        
if all([val == 0 for val in vals]):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
row = {}
import sys
for i in range(h):
    aij = input()
    if aij in row:
        row[aij] += 1
    else:
        row[aij] = 1

tst = h%2
tst2 = 0
for k in row:
    tst2 += row[k]%2
if tst2>tst:
    print("No")
    sys.exit()
#dictionaryのkeyの順序が保存されないためと仮定
row_ = [k for k in row]
A = []
clm = {}
for j in range(w):
    aji = []
    for a in row_:
        aji.append(a[j])
    A.append(''.join(aji))

for a in A:
    if a in clm:
        clm[a] += 1
    else:
        clm[a] = 1

tst = w%2
tst2 = 0

for k in clm:
    tst2 += clm[k]%2
if tst2>tst:
    print("No")
    sys.exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
row = {}
import sys
for i in range(h):
    aij = input()
    if aij in row:
        row[aij] += 1
    else:
        row[aij] = 1

tst = h%2
tst2 = 0
for k in row:
    tst2 += row[k]%2
if tst2>tst:
    print("No")
    sys.exit()

A = []
clm = {}
for j in range(w):
    aji = []
    for a in row:
        aji.append(a[j])
    A.append(''.join(aji))

for a in A:
    if a in clm:
        clm[a] += 1
    else:
        clm[a] = 1

tst = w%2
tst2 = 0

for k in clm:
    tst2 += clm[k]%2
if tst2>tst:
    print("No")
    sys.exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
        break
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-max(0,2*(((a1-1)//4)-(((h-1)//2)*((w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-max(0,2*(((a3-3)//4)-(((h-1)//2)*((w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==2:
    print()
  elif a_3==2:
    print()
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
        break
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-(max(0,2*(((a1-1)//4)-(((h-1)//2)+(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-(max(0,2*(((a3-3)//4)-(((h-1)//2)+(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==2:
    print()
  elif a_3==2:
    print()
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
        break
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-(max(0,2*(((a1-1)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-(max(0,2*(((a3-3)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==2:
    print()
  elif a_3==2:
    print()
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
        break
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-(max(0,2*(((a_1-1)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-(max(0,2*(((a_3-3)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==2:
    print()
  elif a_3==2:
    print()
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-(max(0,2*(((a_1-1)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-(max(0,2*(((a_3-3)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==2:
    print()
  elif a_3==2:
    print()
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
graph=[[]*w for i in range(h)]
for i in range(h):
  graph[i]=list(input())
A=0
B=0
C=0
D=0
E=0
F=0
G=0
H=0
I=0
J=0
K=0
L=0
M=0
N=0
O=0
P=0
Q=0
R=0
S=0
T=0
U=0
V=0
W=0
X=0
Y=0
Z=0
for i in range(h):
  A=A+graph[i].count("a")
for i in range(h):
  B=B+graph[i].count("b")
for i in range(h):
  C=C+graph[i].count("c")
for i in range(h):
  D=D+graph[i].count("d")
for i in range(h):
  E=E+graph[i].count("e")
for i in range(h):
  F=F+graph[i].count("f")
for i in range(h):
  G=G+graph[i].count("g")
for i in range(h):
  H=H+graph[i].count("h")
for i in range(h):
  I=I+graph[i].count("i")
for i in range(h):
  J=J+graph[i].count("j")
for i in range(h):
  K=K+graph[i].count("k")
for i in range(h):
  L=L+graph[i].count("l")
for i in range(h):
  M=M+graph[i].count("m")
for i in range(h):
  N=N+graph[i].count("n")
for i in range(h):
  O=O+graph[i].count("o")
for i in range(h):
  P=P+graph[i].count("p")
for i in range(h):
  Q=Q+graph[i].count("q")
for i in range(h):
  R=R+graph[i].count("r")
for i in range(h):
  S=S+graph[i].count("s")
for i in range(h):
  T=T+graph[i].count("t")
for i in range(h):
  U=U+graph[i].count("u")
for i in range(h):
  V=V+graph[i].count("v")
for i in range(h):
  W=W+graph[i].count("w")
for i in range(h):
  X=X+graph[i].count("x")
for i in range(h):
  Y=Y+graph[i].count("y")
for i in range(h):
  Z=Z+graph[i].count("z")
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]
if w%2==0 and h%2==0:
  for i in range(26):
    if List[i]%4==0:
      if i==25:
        print("Yes")
      else:
        continue
    else:
      print("No")
      break
elif w%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(w//2)%2 and a_2<=(w//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
elif h%2==0 :
  a_2=0
  a_4=0
  for i in range(26):
    if List[i]%4==1 or List[i]%4==3:
      print("No")
      break
    elif List[i]%4==0:
      a_4=a_4+1
    else:
      a_2=a_2+1
  if a_2%2==(h//2)%2 and a_2<=(h//2):
    print("Yes")
  elif List[i]%4==1 or List[i]%4==3:
    print()
  else:
    print("No")
else:
  a_1=0
  a_2=0
  a_3=0
  a_4=0
  for i in range(26):
    if List[i]%4==1:
      a1=List[i]
      a_1=a_1+1
      if a_1==2:
        print("No")
        break
    elif List[i]%4==2:
      a_2=a_2+1
    elif List[i]%4==3:
      a3=List[i]
      a_3=a_3+1
      if a_3==2:
        print("No")
    else:
      a_4=a_4+1
  if a_1==1 and a_3==0:
    if a_2%2==(((h-1)//2)+((w-1)//2))%2 and a_2<=((h-1)//2)+((w-1)//2)-(max(0,2*(((a_1-1)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  elif a_1==0 and a_3==1:
    if a_2%2==(((h-1)//2)+((w-1)//2)+1)%2 and a_2<=((h-1)//2)+((w-1)//2)-1-(max(0,2*(((a_3-3)//4)-(((h-1)//2)*(w-1)//2)))):
      print("Yes")
    else:
      print("No")
  else:
    print("No")