Problem:


Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter("".join(a))
    d = Counter(c.values())
    if (h % 2 == 0) and (w % 2 == 0):
        return "Yes" if list(d.keys()) == [4] else "No"
    if 3 in d:
        return "No"
    if (h % 2 == 1) and (w % 2 == 1):
        if (1 in d) and (d[1] != 1):
            return "No"
        if (2 in d) and d[2] > sum((x-1) // 2 for x in [h,w]):
            return "No"
    else:
        if 1 in d:
            return "No"
        if (2 in d) and d[2] > sum((x % 2) * (x // 2) for x in [h,w]):
            return "No"
    return "Yes"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter("".join(a))
    d = Counter(c.values())
    if (h % 2 == 0) and (w % 2 == 0):
        return "Yes" if list(d.keys()) == [4] else "No"
    if 3 in d:
        return "No"
    if (h % 2 == 1) and (w % 2 == 1):
        if (1 in d) and (d[1] != 1):
            return "No"
    else:
        if 1 in d:
            return "No"
    if (2 in d) and d[2] > sum((x % 2) * (x // 2) for x in [h,w]):
        return "No"
    return "Yes"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter("".join(a))
    d = Counter(c.values())
    if (h % 2 == 0) and (w % 2 == 0):
        return "Yes" if list(d.keys()) == [4] else "No"
    if 3 in d:
        return "No"
    if (1 in d) and (d[1] != 1):
        return "No"
    if (2 in d) and d[2] > sum((x % 2) * (x // 2) for x in [h,w]):
        return "No"
    return "Yes"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter("".join(a))
    d = Counter(c.values())
    if (h % 2 == 0) and (w % 2 == 0):
        return "Yes" if d.keys() == [4] else "No"
    if 3 in d:
        return "No"
    if (1 in d) and (d[1] != 1):
        return "No"
    if (2 in d) and d[2] > sum((x % 2) * (x // 2) for x in [h,w]):
        return "No"
    return "Yes"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def ok(a):
    c = Counter(a)
    return (sum(num % 2 for num in c.values()) - len(a) % 2) == 0

def solve(h, w, a):
    b = ["".join([a[r][c] for r in range(h)]) for c in range(w)]
    return "Yes" if ok(a) and ok(b) else "No"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def ok(a):
    c = Counter(a)
    return (sum(num % 2 for num in c.values()) - len(a) % 2) == 0

def solve(h, w, a):
    for r in range(h):
        if not ok(a[r]):
            return "No"
    return "Yes" if ok(a) else "No"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter(a)
    ok = (sum(num % 2 for num in c.values()) - h % 2) == 0
    return "Yes" if ok else "No"

h, w = map(int, input().split())
a = [input() for r in range(h)]
print(solve(h, w, a))

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    h,  w = getList()
    cnt = defaultdict(int)
    for i in range(h):
        for k, v in Counter(getS()).items():
            cnt[k] += v

    yon, ni, ichi = 0,0,0
    for v in cnt.values():
        yon += v // 4
        v %= 4
        ni += v // 2
        v %= 2
        ichi += v

    if h % 2 == 1 and w % 2 == 1:
        if ichi != 1:
            print("No")
            return
        if ni > h // 2 + w // 2:
            print("No")
            return
        print("Yes")
        return

    if h % 2 == 0 and w % 2 == 0:
        if ichi or ni:
            print("No")
            return
        print("Yes")
        return

    if h % 2 == 1:
        # print(yon, ni, ichi)
        if ichi or ni != w // 2:
            print("No")
            return
        print("Yes")
        return

    if ichi or ni != h // 2:
        print("No")
        return

    print("Yes")
    return

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    h,  w = getList()
    cnt = defaultdict(int)
    for i in range(h):
        for k, v in Counter(getS()).items():
            cnt[k] += v

    yon, ni, ichi = 0,0,0
    for v in cnt.values():
        yon += v // 4
        v %= 4
        ni += v // 2
        v %= 2
        ichi += v

    if h % 2 == 1 and w % 2 == 1:
        if ichi != 1:
            print("No")
            return
        if ni != h // 2 + w // 2:
            print("No")
            return
        print("Yes")
        return

    if h % 2 == 0 and w % 2 == 0:
        if ichi or ni:
            print("No")
            return
        print("Yes")
        return

    if h % 2 == 1:
        # print(yon, ni, ichi)
        if ichi or ni != w // 2:
            print("No")
            return
        print("Yes")
        return

    if ichi or ni != h // 2:
        print("No")
        return

    print("Yes")
    return

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools,heapq
input = sys.stdin.readline

def m(n):
    return -1*n

a = []
H,W=list(map(int,input().split()))
for _ in range(H):
    s = input().rstrip()
    a += list(s)
ac = collections.Counter(a)
acval = sorted(ac.values())

#両方偶数
if H%2 == 0 and W%2==0:
    flag = [4]*((H*W)//4)

#どっちか奇数
elif H%2 ==1 and W %2 == 0:
    flag = [2]*(W//2)+[4]*((H*W-W)//4)
elif W%2 ==1 and H %2 == 0:
    flag = [2]*(H//2)+[4]*((H*W-H)//4)

#両方奇数
if H%2 == 1 and W%2==1:
    flag = [1]+[2]*(H//2)+[2]*(W//2)+[4]*((H*W-1-(W-1)-(H-1))//4)
flag.sort()
acval = list(map(m,acval))
heapq.heapify(acval)
print(acval,flag)
while acval:
    tmpq = -1*heapq.heappop(acval)
    tmpf = flag.pop()
    if tmpq < tmpf:
        print('No')
        exit()
    else:
        tmp = tmpq - tmpf
        if tmp !=0:
            heapq.heappush(acval,-1*tmp)
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools,heapq
input = sys.stdin.readline

def m(n):
    return -1*n

a = []
H,W=list(map(int,input().split()))
for _ in range(H):
    s = input().rstrip()
    a += list(s)
ac = collections.Counter(a)
acval = sorted(ac.values())

#両方偶数
if H%2 == 0 and W%2==0:
    flag = [4]*((H*W)//4)

#どっちか奇数
elif H%2 ==1 and W %2 == 0:
    flag = [2]*(W//2)+[4]*((H*W-W)//4)
elif W%2 ==1 and H %2 == 0:
    flag = [2]*(H//2)+[4]*((H*W-H)//4)

#両方奇数
if H%2 == 1 and W%2==1:
    flag = [1]+[2]*(H//2)+[2]*(W//2)+[4]*((H*W-1-W//2-H//2)//4)
flag.sort()
acval = list(map(m,acval))
heapq.heapify(acval)

while acval:
    tmpq = -1*heapq.heappop(acval)
    tmpf = flag.pop()
    if tmpq < tmpf:
        print('No')
        exit()
    else:
        tmp = tmpq - tmpf
        if tmp !=0:
            heapq.heappush(acval,-1*tmp)
print('Yes')

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())

d = {}
for _ in range(h):
    a = input()
    for i in range(w):
        a1 = a[i]
        if a1 in d:
            d[a1] += 1
        else:
            d[a1] = 1

if h % 2 == 0 and w % 2 == 0:
    for v in d.values():
        if v % 4 != 0:
            print('No')
            exit()
    print('Yes')

elif h % 2 == 0 or w % 2 == 0:
    cnt_4 = (h // 2) * (w // 2)
    if h % 2 == 0:
        cnt_2 = h // 2
    else:
        cnt_2 = w // 2
    tmp_2 = 0
    tmp_4 = 0
    for v in d.values():
        if v % 4 == 0:
            tmp_4 += v // 4
        elif v % 2 == 0:
            tmp_4 += v // 4
            tmp_2 += 1
        else:
            print('No')
            exit()
    if tmp_4 >= cnt_4 and cnt_2 == tmp_2 + (tmp_4 - cnt_4) * 2:
        print('Yes')
    else:
        print('No')

else:
    cnt_4 = (h // 2) * (w // 2)
    cnt_2 = h // 2 + w // 2
    cnt_1 = 1
    tmp_1 = 0
    tmp_2 = 0
    tmp_4 = 0

    for v in d.values():
        if v % 4 == 0:
            tmp_4 += v // 4
        elif v % 2 == 0:
            tmp_4 += v // 4
            tmp_2 += 1
        else:
            tmp_1 += 1
    if tmp_4 >= cnt_4 and cnt_2 <= tmp_2 + (tmp_4 - cnt_4) * 2 and cnt_1 == tmp_1 + tmp_2 + (tmp_4 - cnt_4) * 2 - cnt_2:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
from collections import Counter
def check():
  H, W = map(int, input().split())
  A = []
  for i in range(H):
    A.extend(list(input()))
  c = Counter(A)
  one = 0
  two = 0
  for v in c.values():
    if v%2==1:
      one += 1
    if v%4==2:
      two += 1
  if H%2==0 and W%2==0:
    if one+two>0:
      return 'No'
  if H%2==0 and W%2==1:
    if two>H//2 or one>0:
      return 'No'
  if H%2==1 and W%2==0:
    if two>W//2 or one>0:
      return 'No'
  if H%2==0 and W%2==0:
    if two>W//2+H//2 or one>1:
      return 'No'
  return 'Yes'
print(check())

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W = map(int, input().split())
C = []
for i in range(H):
  C = C + (list(input()))
  
C = Counter(C)
if len(C) == 1:
  print("Yes")
  exit(0)
  
V = sorted(C.values())

"""
H:odd, W:odd
  H+W-1 が奇数一つと2の倍数で組成
  残りH*W - sakkino が4の倍数で組成
H:even, W:odd
  H が2の倍数で組成
  残りが4の倍数で組成
H:even, W:even
  すべて4の倍数で組成
"""
oddFlg = True if H%2 == 1 and W%2 == 1 else False
res = 1 if H%2 == 1 and W%2 == 1 else 0
res += H if W%2 == 1 else 0
res += W if H%2 == 1 else 0

als = H*W - res

A2 = []
A4 = []
for i in range(len(V)):
  v = V[i]
  if v == 0:
    continue
  elif v % 2 == 1:
    if oddFlg:
      oddFlg = False
      res -= v
    else:
      print("No")
      exit(0)
  elif v%4 == 0:
    A4.append(v)
  else:
    A2.append(v)
  
"""
print(res)
print(A2)
print(A4)
"""

for a in A2:
  res -= 2
  A4.append(a-2)
  if res < 0:
    print("No")
    exit(0)
    
res += als
if res == sum(A4):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

"""
H:odd, W:odd
  H+W-1 が奇数一つと2の倍数で組成
  残りH*W - sakkino が4の倍数で組成
H:even, W:odd
  H が2の倍数で組成
  残りが4の倍数で組成
H:even, W:even
  すべて4の倍数で組成
"""
oddFlg = True if H%2 == 1 and W%2 == 1 else False
res = 1 if H%2 == 1 and W%2 == 1 else 0
res += H if W%2 == 1 else 0
res += W if H%2 == 1 else 0

als = H*W - res

A2 = []
A4 = []
for i in range(len(V)):
  v = V[i]
  if v == 0:
    continue
  elif v % 2 == 1:
    if oddFlg:
      oddFlg = False
      res -= v
    else:
      print("No")
      exit(0)
  elif v%4 == 0:
    A4.append(v)
  else:
    A2.append(v)
  
"""
print(res)
print(A2)
print(A4)
"""

for a in A2:
  res -= 2
  A4.append(a-2)
  if res < 0:
    print("No")
    exit(0)
    
res += als
if res == sum(A4):
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
alp_d = {chr(ord('a') + i): 0 for i in range(26)}

for _ in range(h):
    s = list(input())
    for si in s:
        alp_d[si] += 1
        alp_d[si] %= 4

r4 = [0]*4
for v in alp_d.values():
    r4[v] += 1

if w%2 == 0 and h%2 == 0:
    if r4[1] == 0 and r4[2] == 0 and r4[3] == 0 and r4[0] > 0:
        print('Yes')
    else:
        print('No')

elif w%2 == 0 and h%2 != 0:
    if r4[1] == 0 and r4[2] <= w//2 and r4[3] == 0:
        print('Yes')
    else:
        print('No')

elif h%2 == 0 and w%2 != 0:
    if r4[1] == 0 and r4[2] <= h//2 and r4[3] == 0:
        print('Yes')
    else:
        print('No')       

else:
    cnt2 = (w-1)//2 + (h-1)//2
    if r4[1] == 1 and r4[2] == cnt2 and r4[3] == 0:
        print('Yes')
    elif r4[1] == 0 and r4[2] == cnt2-1 and r4[3] == 1:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
alp_d = {chr(ord('a') + i): 0 for i in range(26)}

for _ in range(h):
    s = list(input())
    for si in s:
        alp_d[si] += 1
        alp_d[si] %= 4

r4 = [0]*4
for v in alp_d.values():
    r4[v] += 1

if w%2 == 0 and h%2 == 0:
    if r4[1] == 0 and r4[2] == 0 and r4[3] == 0 and r4[0] > 0:
        print('Yes')
    else:
        print('No')

elif w%2 == 0 and h%2 != 0:
    if r4[1] == 0 and r4[2] == w//2 and r4[3] == 0:
        print('Yes')
    else:
        print('No')

elif h%2 == 0 and w%2 != 0:
    if r4[1] == 0 and r4[2] == h//2 and r4[3] == 0:
        print('Yes')
    else:
        print('No')       

else:
    cnt2 = (w-1)//2 + (h-1)//2
    if r4[1] == 1 and r4[2] == cnt2 and r4[3] == 0:
        print('Yes')
    elif r4[1] == 0 and r4[2] == cnt2-1 and r4[3] == 1:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

H, W = inm()


def solve():
    sc = Counter()
    for i in range(H):
        s = ins()
        for c in s:
            sc[c] += 1
    sv = sorted(sc.values())
    odds = 0
    twos = 0
    for x in sv:
        if x % 2 == 1:
            odds += 1
        if x == 2:
            twos += 1
    if (H * W) % 2 == 1:
        if odds != 1:
            return False
    else:
        if odds != 0:
            return False
    maxtwos = 0
    if H % 2 == 1:
        maxtwos += W // 2
    if W % 2 == 1:
        maxtwos += H // 2
    if twos > maxtwos:
        return False

    return True


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

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase


def new_val(cnt, base, target):
    diff = min(cnt // base, target)
    cnt -= diff * base
    target -= diff
    return cnt, target


def main():
    h, w = map(int, input().split())
    count = {s: 0 for s in ascii_lowercase}
    for _ in range(h):
        row = input()
        for j in range(w):
            count[row[j]] += 1
    target_4 = (h // 2) * (w // 2)
    target_1 = (h % 2) * (w % 2)
    target_2 = w // 2 * h % 2 + h // 2 + w % 2
    ans = "Yes"
    for chr, cnt in count.items():
        count[chr], target_4 = new_val(cnt, 4, target_4)
    if target_4:
        ans = "No"
    for chr, cnt in count.items():
        count[chr], target_2 = new_val(cnt, 2, target_2)
    if target_2:
        ans = "No"
    if sum(count.values()) == target_1:
        ans = "No"
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())


table = {}
for i in range(h):
    a = input()
    for c in a:
        if c not in table:
            table[c] = 1
        else:
            table[c] += 1

mod0 = []
mod1 = []
mod2 = []
mod3 = []
for key, value in table.items():
    if value % 4 == 0:
        mod0.append(key)
    elif value % 4 == 1:
        mod1.append(key)
    elif value % 4 == 2:
        mod2.append(key)
    else:
        mod3.append(key)


if w % 2 == 0 and h % 2 == 0:
    if len(mod1) > 0 or len(mod2) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")

elif w % 2 == 0 or h % 2 == 0:
    if h % 2 == 0:
        w, h = h, w

    if w // 2 < len(mod2):
        print("No")
    elif len(mod1) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")
else:
    if (len(mod1) == 1 or len(mod3) == 1) and len(mod2) <= h + w - 2:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())


table = {}
for i in range(h):
    a = input()
    for c in a:
        if c not in table:
            table[c] = 1
        else:
            table[c] += 1

mod0 = []
mod1 = []
mod2 = []
mod3 = []
for key, value in table.items():
    if value % 4 == 0:
        mod0.append(key)
    elif value % 4 == 1:
        mod1.append(key)
    elif value % 4 == 2:
        mod2.append(key)
    else:
        mod3.append(key)


if w % 2 == 0 and h % 2 == 0:
    if len(mod1) > 0 or len(mod2) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")

elif w % 2 == 0 or h % 2 == 0:
    if h % 2 == 0:
        w, h = h, w

    if w // 2 < len(mod2):
        print("No")
    elif len(mod1) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")
else:
    if (len(mod1) == 1 or len(mod3) == 1) and len(mod2) <= (w // 2) + (h // 2):
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())


table = {}
for i in range(h):
    a = input()
    for c in a:
        if c not in table:
            table[c] = 1
        else:
            table[c] += 1

mod0 = []
mod1 = []
mod2 = []
mod3 = []
for key, value in table.items():
    if value % 4 == 0:
        mod0.append(key)
    elif value % 4 == 1:
        mod1.append(key)
    elif value % 4 == 2:
        mod2.append(key)
    else:
        mod3.append(key)


if w % 2 == 0 and h % 2 == 0:
    if len(mod1) > 0 or len(mod2) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")

elif w % 2 == 0 or h % 2 == 0:
    if h % 2 == 0:
        w, h = h, w

    if w // 2 < len(mod2):
        print("No")
    elif len(mod1) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")
else:
    if len(mod1) != 1 and len(mod3) != 1:
        print("No")
    elif len(mod2) > (w // 2) * (h // 2):
        print("No")
    elif len(mod3) > 0:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())


table = {}
for i in range(h):
    a = input()
    for c in a:
        if c not in table:
            table[c] = 1
        else:
            table[c] += 1

mod0 = []
mod1 = []
mod2 = []
mod3 = []
for key, value in table.items():
    if value % 4 == 0:
        mod0.append(key)
    elif value % 4 == 1:
        mod1.append(key)
    elif value % 4 == 2:
        mod2.append(key)
    else:
        mod3.append(key)


if w % 2 == 0 and h % 2 == 0:
    if len(mod1) > 0 or len(mod2) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")

elif w % 2 == 0 or h % 2 == 0:
    if h % 2 == 0:
        w, h = h, w

    if w // 2 < len(mod2):
        print("No")
    elif len(mod1) > 0 or len(mod3) > 0:
        print("No")
    else:
        print("Yes")
else:
    if len(mod1) != 1:
        print("No")
    elif len(mod0) < (w // 2) * (h // 2):
        print("No")
    elif len(mod3) > 0:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

H, W = map(int, sys.stdin.readline().split())

string = ""
for _ in range(H):
    string += sys.stdin.readline().strip()

counter = Counter(string)
e_one = 0
e_two = 0
e_four = 0
for key, count in counter.items():
    tmp = count // 4
    e_four += tmp
    count -= 4 * tmp
    tmp = count // 2
    e_two += tmp
    count -= 2 * tmp
    e_one += count

one = 0
two = 0
four = 0
# 奇数の場合、中心の文字は1つしかなくても回文になる
if H % 2 == 0 and W % 2 == 0:
    four = H * W // 4
elif H % 2 == 1 and W % 2 == 1:
    one = 1
    two = H // 2 + W // 2
    four = (H * W - (one + two)) // 4
else:
    # 縦を奇数とする
    if W % 2 == 1:
        H, W = W, H

    two = W // 2
    four = (H * W - two) // 4

# print("exists", e_four, e_two, e_one)
# print("needs", four, two, one)

if four > e_four:
    print("No")
else:
    e_four -= four
    e_two += e_four * 2
    if two > e_two:
        print("No")
    else:
        e_two -= two
        e_one += e_two * 2
        if e_one == one:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]
    n, m = [int(x) for x in input().split()]
    d = defaultdict(int)
    for i in range(n):
        s = input()
        for e in s:
            d[e] += 1
    odd = 0
    q = 0
    for c in d:
        odd += d[c] % 2
        q += d[c] // 4

    if n % 2 and m % 2:
        if odd == 1:
            print("Yes")
        else:
            print("No")
    elif n % 2 != m % 2:
        ev = n
        if m % 2 == 0: ev = m
        if q * 4 < n * m - ev or odd:
            print("No")
        else:
            print("Yes")
    else:
        if q * 4 != n * m:
            print("No")
        else:
            print("Yes")


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""
a b a
d c d
a b a
"""

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int, input().split())
s = ''.join([''.join(input().split()) for _ in range(h)])

ctr = collections.Counter(s)
if h % 2 == 0 and w % 2 == 0:
    four, two = h*w//4, 0
elif h % 2 == 0:
    four, two = (h*w-h)//4, h//2
elif w % 2 == 0:
    four, two = (h*w-w)//4, w//2
else:
    four, two = (h*w-h-w+1), (h+w-1)//2

cf = sum([c//4 for c in ctr.values()])
ct = sum([(c%4)//2 for c in ctr.values()])

if cf >= four and ct + 2*(cf-four) >= two:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import collections
h,w = map(int, input().split())
s = ''.join([''.join(input().split()) for _ in range(h)])

ctr = collections.Counter(s)
if h % 2 == 0 and w % 2 == 0:
    four, two = h*w//4, 0
elif h % 2 == 0:
    four, two = (h*w-h)//4, h//2
elif w % 2 == 0:
    four, two = (h*w-w)//4, w//2
else:
    four, two = (h*w-h-w+1), (h+w-1)//2

cf = sum([c//4 for c in ctr.values()])
ct = sum([(c%4)//2 for c in ctr.values()])

if cf >= four and ct >= 2*(cf-four) + two:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

for i in range(H):
    c = Counter(A[i])
    ans = 0
    for j in c:
        if c[j] % 2 == 1:
            ans += 1
        if H % 2 == 1 and ans > 1:
            print("No")
            exit()
        elif H % 2 == 0 and ans > 0:
            print("No")
            exit()

for i in range(W):
    ans = 0
    d = defaultdict(int)
    for j in range(H):
        d[A[j][i]] += 1
    for l in d:
        if d[l] % 2 == 1:
            ans += 1
        if H % 2 == 1 and ans > 1:
            print("No")
            exit()
        elif H % 2 == 0 and ans > 0:
            print("No")
            exit()
print("Yes")