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

# sys.setrecursionlimit(100000)


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        elif h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        elif w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue

        elif h > 2 and h > 2:
            p = ((h + w) * 2) - 4
            for _ in range(p // 4):
                el.append(4)
            if p % 4 == 2:
                el.append(2)
            h -= 2
            w -= 2

    el = sorted(el)[::-1]
    for e in el:
        for i in range(len(cands)):
            if cands[i] >= e:
                cands[i] -= e
                break
    for c in cands:
        if c > 0:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

# sys.setrecursionlimit(100000)


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        if h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        if w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue
        p = ((h + w) * 2) - 4
        for _ in range(p // 4):
            el.append(4)
        if p % 4 == 2:
            el.append(2)
        if h > 1:
            h = h - 2
        else:
            h = h - 1
        if w > 1:
            w -= 2
        else:
            w -= 1

    el = sorted(el)[::-1]
    for e in el:
        for i in range(len(cands)):
            if cands[i] >= e:
                cands[i] -= e
                break
    for c in cands:
        if c > 0:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    h, w = [int(i) for i in input().strip().split()]
    s = ""
    for _ in range(h):
        s += input().strip()
    counts = Counter(s).most_common()
    cands = [int(i) for v, i in counts]
    el = []
    while h > 0 and w > 0:
        if h == 1 and w == 1:
            el.append(1)
            h -= 1
            w -= 1
            continue
        if h == 1 and w > 1:
            el.append(2)
            w -= 2
            continue
        if w == 1 and h > 1:
            el.append(2)
            h -= 2
            continue
        p = ((h + w) * 2) - 4
        for _ in range(p // 4):
            el.append(4)
        if p % 4 == 2:
            el.append(2)
        if h > 1:
            h = h - 2
        else:
            h = h - 1
        if w > 1:
            w -= 2
        else:
            w -= 1

    el = sorted(el)[::-1]
    for e in el:
        for i in range(len(cands)):
            if cands[i] >= e:
                cands[i] -= e
                break
    for c in cands:
        if c > 0:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()

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):
    row = input()
    for c in row:
        alp_d[c] += 1

cnt_rem4 = [0]*4
more4 = 0
for v in alp_d.values():
    if v == 0:
        continue
    cnt_rem4[v%4] += 1
    if v >= 4:
        more4 += 1

# print(cnt_rem4)

if h%2 == 1 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 1 and more4 >= ((h-1)//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 0:
    if h*w//4 == more4:
        print('Yes')
    else:
        print('No')


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


elif h%2 == 0 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[2] <= (h//2):
        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):
    row = input()
    for c in row:
        alp_d[c] += 1

cnt_rem4 = [0]*4
more4 = 0
for v in alp_d.values():
    if v == 0:
        continue
    cnt_rem4[v%4] += 1
    if v >= 4:
        more4 += 1

# print(cnt_rem4)

if h%2 == 1 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 1 and cnt_rem4[0] >= ((h-1)//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 0:
    if h*w//4 == cnt_rem4[0]:
        print('Yes')
    else:
        print('No')


elif h%2 == 1 and w%2 == 0:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= ((h-1)//2)*(w//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= (h//2)*((w-1)//2):
        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):
    row = input()
    for c in row:
        alp_d[c] += 1

cnt_rem4 = [0]*4
more4 = 0
for v in alp_d.values():
    if v == 0:
        continue
    cnt_rem4[v%4] += 1
    if v >= 4:
        more4 += 1

# print(cnt_rem4)

if h%2 == 1 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 1 and more4 >= ((h-1)//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 0:
    if h*w//4 == more4:
        print('Yes')
    else:
        print('No')


elif h%2 == 1 and w%2 == 0:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and more4 >= ((h-1)//2)*(w//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and more4 >= (h//2)*((w-1)//2):
        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):
    row = input()
    for c in row:
        alp_d[c] += 1

cnt_rem4 = [0]*4
for v in alp_d.values():
    if v == 0:
        continue
    cnt_rem4[v%4] += 1


if h%2 == 1 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 1 and cnt_rem4[0] >= ((h-1)//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 0:
    if h*w//4 == cnt_rem4[0]:
        print('Yes')
    else:
        print('No')


elif h%2 == 1 and w%2 == 0:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= ((h-1)//2)*(w//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= (h//2)*((w-1)//2):
        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):
    row = input()
    for c in row:
        alp_d[c] += 1

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


if h%2 == 1 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 1 and cnt_rem4[0] >= ((h-1)//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 0:
    if h*w//4 == cnt_rem4[0]:
        print('Yes')
    else:
        print('No')


elif h%2 == 1 and w%2 == 0:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= ((h-1)//2)*(w//2):
        print('Yes')
    else:
        print('No')


elif h%2 == 0 and w%2 == 1:
    if cnt_rem4[1] + cnt_rem4[3] == 0 and cnt_rem4[0] >= (h//2)*((w-1)//2):
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
import heapq

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


def solve():
    h, w = inl()
    counter = Counter()
    for i in range(h):
        for c in ins():
            counter[c] += 1

    p4 = (h // 2) * (w // 2)
    p1 = 1 if h % 2 == 1 and w % 2 == 1 else 0
    p2 = (h * w - p4 - p1) // 2

    xs = [-x for x in counter.values()]
    hp = []
    for x in xs:
        heapq.heappush(hp, x)

    while hp:
        x = heapq.heappop(hp)
        k = -x
        if p4 > 0:
            if k < 4:
                return False
            m = k // 4
            p = min(m, p4)
            p4 -= p
            k -= p * 4
            if k > 0:
                heapq.heappush(hp, -k)
        elif p2 > 0:
            if k < 2:
                return False
            m = k // 2
            p = min(m, p2)
            p2 -= p
            k -= p * 2
            if k > 0:
                heapq.heappush(hp, -k)
        elif p1 > 0:
            if k == 1:
                return True
            return False
    return True


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

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W = map(int, input().split())
    count = Counter()
    for _ in range(H):
        count += Counter(input())

    four = (H // 2) * (W // 2)
    two = (H // 2) * (H % 2) + (W // 2) * (W % 2)
    one = H % 2 * W % 2

    for value in count.values():
        while value >= 4 and four > 0:
            value -= 4
            four -= 1
        while value >= 2 and two > 0:
            value -= 2
            two -= 1
        while value >= 1 and one > 0:
            value -= 1
            one -= 1

    if four == two == one == 0:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    h, w = map(int, readline().split())
    c = Counter()
    res = [[""] * w for _ in range(h)]

    for _ in range(h):
        s = list(input())
        c.update(s)

    d = Counter()

    if h % 2 == 1 and w % 2 == 1:
        d[1] = 1
        d[2] = (h - 1) // 2 + (w - 1) // 2
    elif h % 2 == 1 and w % 2 == 0:
        d[1] = 0
        d[2] = w // 2
    elif h % 2 == 0 and w % 2 == 1:
        d[1] = 0
        d[2] = h // 2
    d[4] = (h * w - d[1] - 2 * d[2]) // 4

    keys = c.keys()

    if d[1] > 0:
        for key in keys:
            if c[key] % 2 == 1:
                c[key] -= 1
                d[1] -= 1

    if d[2] > 0:
        for key in keys:
            if c[key] % 4 == 2:
                c[key] -= 2
                d[2] -= 1

    if d[4] > 0:
        for key in keys:
            if c[key] % 4 == 0:
                d[4] -= c[key] // 4
                c[key] = 0

    while d[2] > 0:
        d[2] -= 2
        d[4] += 1

    if d[1] == d[2] == d[4] == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    h, w = map(int, readline().split())
    c = Counter()
    res = [[""] * w for _ in range(h)]

    for _ in range(h):
        s = list(input())
        c.update(s)

    d = Counter()

    if h % 2 == 1 and w % 2 == 1:
        d[1] = 1
        d[2] = (h - 1) // 2 + (w - 1) // 2
    elif h % 2 == 1 and w % 2 == 0:
        d[1] = 0
        d[2] = w // 2
    elif h % 2 == 0 and w % 2 == 1:
        d[1] = 0
        d[2] = h // 2
    d[4] = (h * w - d[1] - 2 * d[2]) // 4

    keys = c.keys()

    if d[1] > 0:
        for key in keys:
            if c[key] % 2 == 1:
                c[key] -= 1
                d[1] -= 1

    if d[2] > 0:
        for key in keys:
            if c[key] % 4 == 2:
                c[key] -= 2
                d[2] -= 1

    if d[4] > 0:
        for key in keys:
            if c[key] > 0 and c[key] % 4 == 0:
                d[4] -= c[key] // 4
                c[key] = 0

    while d[2] < 0:
        d[2] += 2
        d[4] -= 1

    while d[2] > 0:
        d[2] -= 2
        d[4] += 1

    if d[1] == d[2] == d[4] == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    h, w = map(int, readline().split())
    c = Counter()
    res = [[""] * w for _ in range(h)]

    for _ in range(h):
        s = list(input())
        c.update(s)

    d = Counter()

    if h % 2 == 1 and w % 2 == 1:
        d[1] = 1
        d[2] = (h - 1) // 2 + (w - 1) // 2
    elif h % 2 == 1 and w % 2 == 0:
        d[1] = 0
        d[2] = w // 2
    elif h % 2 == 0 and w % 2 == 1:
        d[1] = 0
        d[2] = h // 2
    d[4] = (h * w - d[1] - 2 * d[2]) // 4

    keys = c.keys()

    if d[1] > 0:
        for key in keys:
            if c[key] % 2 == 1:
                c[key] -= 1
                d[1] -= 1

    if d[2] > 0:
        for key in keys:
            if c[key] % 4 == 2:
                c[key] -= 2
                d[2] -= 1

    if d[4] > 0:
        for key in keys:
            if c[key] > 0 and c[key] % 4 == 0:
                c[key] -= 4
                d[4] -= 1

    while d[2] < 0:
        d[2] += 2
        d[4] -= 1

    if d[1] == d[2] == d[4] == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    h, w = map(int, readline().split())
    c = Counter()
    res = [[""] * w for _ in range(h)]

    for _ in range(h):
        s = list(input())
        c.update(s)

    d = Counter()

    if h % 2 == 1 and w % 2 == 1:
        d[1] = 1
        d[2] = (h - 1) // 2 + (w - 1) // 2
    elif h % 2 == 1 and w % 2 == 0:
        d[1] = 0
        d[2] = w // 2
    elif h % 2 == 0 and w % 2 == 1:
        d[1] = 0
        d[2] = h // 2
    d[4] = (h * w - d[1] - d[2]) // 4

    keys = c.keys()

    if d[1] == 1:
        for key in keys:
            if c[key] % 2 == 1:
                c[key] -= 1
                d[1] -= 1

    if d[2] > 0:
        for key in keys:
            if c[key] % 4 == 2:
                c[key] -= 2
                d[2] -= 1

    if d[4] > 0:
        for key in keys:
            if c[key] > 0 and c[key] % 4 == 0:
                c[key] -= 4
                d[4] -= 1

    while d[2] < 0:
        d[2] += 2
        d[4] -= 1

    if d[1] == d[2] == d[4] == 0:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import defaultdict
h, w = map(int, input().split())
d = defaultdict(int)
for _ in range(h):
    row = list(input())
    for i in range(w):
        d[row[i]] += 1

if h % 2 == 0 and w % 2 == 0: #両方偶数
    # すべて4の倍数である必要
    for v in d.values():
        if v % 4 != 0:
            print('No')
            break
    else:
        print('Yes')
elif h % 2 == 1 and w % 2 == 1:
    #両方奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == (longer + 1) // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == (longer + 1) // 2:
            break
    one = 0
    for v in d.values():
        if v != 0:
            if v == 1 and one == 0:
                one += 1
            else:
                print('No')
                break
    else:
        if one == 1:
            print('Yes')
        else:
            print('No')
else: # 偶数・奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == (longer + 1) // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == (longer + 1) // 2:
            break
    for v in d.values():
        if v != 0:
            print('No')
            break
    else:
        print('Yes')

# 1つの行において列数が偶数の場合、例えば左端にaを置いた場合は、
# その列がpalindromeになる関係上必然的に右端にもaを置くことになる。
# さらに、列方向に見た場合は、今置いた2つのaに対して、一番下の行で
# それぞれを置く必要がある。したがって、1つのaを置くと、芋づる式に
# 合計４つのaを置くことになる。
# 行・列のどちらかが奇数の場合は、その真ん中のところにおいては
# 2個でよかったり、3個でよかったり、1個で良かったりするので、場合分けする。

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import defaultdict
h, w = map(int, input().split())
d = defaultdict(int)
for _ in range(h):
    row = list(input())
    for i in range(w):
        d[row[i]] += 1

def check(d):
    while sum(d.values()) != 0:
        for k, v in d.items():
            if v >= 4:
                d[k] -= 4
                break
        else:
            print('No')
            return
    else:
        print('Yes')

if h % 2 == 0 and w % 2 == 0: #両方偶数
    # すべて4の倍数である必要
    for v in d.values():
        if v % 4 != 0:
            print('No')
            break
    else:
        print('Yes')
elif h % 2 == 1 and w % 2 == 1:
    #両方奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == longer // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == longer // 2:
            break
    one = 0
    for v in d.values():
        if v != 0:
            if v == 1 and one == 0:
                one += 1
            else:
                print('No')
                break
    else:
        if one == 1:
            print('Yes')
        else:
            print('No')
else: # 偶数・奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == longer // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == longer // 2:
            break
    for v in d.values():
        if v != 0:
            print('No')
            break
    else:
        print('Yes')

# 1つの行において列数が偶数の場合、例えば左端にaを置いた場合は、
# その列がpalindromeになる関係上必然的に右端にもaを置くことになる。
# さらに、列方向に見た場合は、今置いた2つのaに対して、一番下の行で
# それぞれを置く必要がある。したがって、1つのaを置くと、芋づる式に
# 合計４つのaを置くことになる。
# 行・列のどちらかが奇数の場合は、その真ん中のところにおいては
# 2個でよかったり、3個でよかったり、1個で良かったりするので、場合分けする。

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2017-quala/tasks/code_festival_2017_quala_c
from collections import defaultdict
h, w = map(int, input().split())
d = defaultdict(int)
for _ in range(h):
    row = list(input())
    for i in range(w):
        d[row[i]] += 1

def check(d):
    while sum(d.values()) != 0:
        for k, v in d.items():
            if v >= 4:
                d[k] -= 4
                break
        else:
            print('No')
            return
    else:
        print('Yes')

if h % 2 == 0 and w % 2 == 0: #両方偶数
    # すべて4の倍数である必要
    for v in d.values():
        if v % 4 != 0:
            print('No')
            break
    else:
        print('Yes')
elif h % 2 == 1 and w % 2 == 1:
    #両方奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == longer // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == longer // 2:
            break
    one = 0
    for v in d.values():
        if v != 0:
            if v == 1 and one == 0:
                one += 1
            else:
                print('No', 11, one)
                break
    else:
        if one == 1:
            print('Yes')
        else:
            print('No')
else: # 偶数・奇数
    longer, shorter = max(h, w), min(h, w)
    c = 0
    for k, v in d.items():
        if v >= 4:
            d[k] -= 4 * (v // 4)
            c += v // 4
        if c == longer // 2:
            break
    c = 0
    for k, v in d.items():
        if v >= 2:
            d[k] -= 2 * (v // 2)
            c += v // 2
        if c == longer // 2:
            break
    for v in d.values():
        if v != 0:
            print('No')
            break
    else:
        print('Yes')

# 1つの行において列数が偶数の場合、例えば左端にaを置いた場合は、
# その列がpalindromeになる関係上必然的に右端にもaを置くことになる。
# さらに、列方向に見た場合は、今置いた2つのaに対して、一番下の行で
# それぞれを置く必要がある。したがって、1つのaを置くと、芋づる式に
# 合計４つのaを置くことになる。
# 行・列のどちらかが奇数の場合は、その真ん中のところにおいては
# 2個でよかったり、3個でよかったり、1個で良かったりするので、場合分けする。

Submission status: Wrong Answer

Solution code:
from collections import Counter

H,W,*n = open(0).read().split()
H = int(H)
W = int(W)
c = Counter([])
for x in n:
    c += Counter(x)
if H % 2 == 0 and W % 2 == 0:
    for x in c.values():
        if x % 4 != 0:
            print('No')
            break
    else:
        print('Yes')
elif H % 2 == 1 and W % 2 == 1:
    center = 0
    cross = H + W - 1
    corner = (H*W) - cross
    corner_filled = 0
    cross_filled = 0
    for x in c.values():
        if x == 1:
            if center == 0:
                center = 1
            else:
                print('No')
                break
        elif x % 4 == 0:
            if corner_filled + x < corner:
                corner_filled += x
            elif corner_filled == corner:
                if cross_filled + x > cross:
                    print('No')
                    break
                else:
                    cross_filled += x
            else:
                cross_filled += corner_filled + x - corner
                corner_filled = corner
        elif x % 2 == 0:
            temp = (x//4) * 4
            if corner_filled + temp < corner:
                corner_filled += temp
            elif corner_filled == corner:
                if cross_filled + temp > cross:
                    print('No')
                    break
                else:
                    cross_filled += temp
            else:
                cross_filled += corner_filled + temp - corner
                corner_filled = corner
            if cross_filled + 2 > cross:
                print('No')
                break
            else:
                cross_filled += 2
        else:
            print('No')
            break
    else:
            print('Yes')
else:
    if H % 2 == 1:
        center = W
        sides = (H*W) - W
    else:
        center = H
        sides = (H*W) - H
    center_filled = 0
    sides_filled = 0
    for x in c.values():
        if x % 4 == 0:
            if sides_filled + x < sides:
                sides_filled += x
            elif sides_filled == sides:
                if center_filled + x > center:
                    print('No')
                    break
                else:
                    center_filled += x
            else:
                center_filled += sides_filled + x - sides
                sides_filled = sides
        elif x % 2 == 0:
            temp = (x//4) * 4
            if sides_filled + temp < sides:
                sides_filled += temp
            elif sides_filled == sides:
                if center_filled + temp > center:
                    print('No')
                    break
                else:
                    center_filled += temp
            else:
                center_filled += sides_filled + temp - sides
                sides_filled = sides
            if center_filled + 2 > center:
                print('No')
                break
            else:
                center_filled += 2
        else:
            print('No')
            break
    else:
            print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter(list("".join(a)))
    d = Counter(list(map(lambda num: num % 4, c.values())))
    if (h % 2 == 0) and (w % 2 == 0):
        return "Yes" if list(d.keys()) == [4] else "No"
    if (h % 2 == 1) and (w % 2 == 1):
        for k in [1, 3]:
            if k in d:
                if d[k] != 1:
                    return "No"
        if 1 in d and 3 in d:
            return "No"
        if (2 in d) and d[2] > sum(x // 2 for x in [h,w]):
            return "No"
    else:
        for k in [1, 3]:
            if k in d:
                return "No"
        if (2 in d) and d[2] > sum((x % 2 == 0) * (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:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    H, W = LI()
    a = []
    for _ in range(H):
        a.append(SS())
    a = ''.join(a)

    # not 中央行 and not 中央列 は4n個
    # 中央行 xor 中央列 は2n個
    # 中央行 and 中央列 は1個
    cnt = collections.Counter(a)
    l = list(cnt.values())

    s4 = sum([i for i in l if i % 4 == 0])
    s2 = sum([i for i in l if i % 4 == 2])
    s1 = sum([i for i in l if i % 4 == 1])
    c4 = len([i for i in l if i % 4 == 0])
    c2 = len([i for i in l if i % 4 == 2])
    c1 = len([i for i in l if i % 4 == 1])
    # print(cnt)
    # print(s4, s2, s1)
    # print(c4, c2, c1)
    if H % 2 == W % 2 == 0:
        if s4 == len(a):
            print('Yes')
        else:
            print('No')
    elif H % 2 == W % 2 == 1:
        if s1 == 1 and s2 == H + W - 2 and s4 + s2 + s1 == len(a):
            print('Yes')
        else:
            print('No')
    else:
        if H % 2 == 1:
            if c2 == W // 2 and c4 + c2 == len(cnt):
                print('Yes')
            else:
                print('No')
        if W % 2 == 1:
            if c2 == H // 2 and c4 + c2 == len(cnt):
                print('Yes')
            else:
                print('No')
            
if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve(h, w, a):
    c = Counter(list("".join(a)))
    d = Counter(list(map(lambda num: num % 4, 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 // 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 == 0) * (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(list("".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 // 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 == 0) * (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"
        if (2 in d) and d[2] > sum(x // 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 == 0) * (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))