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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

H, W = map(int, input().split())
S = [input() for _ in range(H)]

for h in range(H):
    d = defaultdict(int)
    for w in range(W):
        d[S[h][w]] += 1

    if sum([v % 2 for v in d.values()]) > 1:
        print("No")
        exit()

for w in range(W):
    d = defaultdict(int)
    for h in range(H):
        d[S[h][w]] += 1

    if sum([v % 2 for v in d.values()]) > 1:
        print("No")
        exit()

print("Yes")

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

H,W = map(int, input().split())
S = [input() for _ in range(H)]

# Aが一文字だけの場合
if H == 1 and W == 1:
    print("Yes")
    exit()

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[S[i][j]] += 1

# 普通の回分の場合
if H == 1 or W == 1:
    # 文字列が偶数個の場合は、それぞれの文字が偶数回出現していればOK
    if H % 2 == 0 or W % 2 == 0:
        f = True
        for i in d.values():
            if i % 2 != 0:
                f = False
        print("Yes" if f else "No")
    # 文字列が奇数個の場合、一つだけ奇数個の文字がある
    else:
        cnt_odd = 0
        for i in d.values():
            if i % 2 != 0:
                cnt_odd += 1
        print("Yes" if cnt_odd == 1 else "NO")
# 行列の回分の場合
# 奇数×奇数のとき、行列の縦と横の真ん中の線の交点に来る文字は奇数個でOK。交点以外の真ん中のライン上の文字は２の倍数個いる。それ以外の位置は４の倍数個いる。
# 奇数×偶数でもおんなじ感じ
# 偶数×偶数は全部４の倍数個ずつ必要
else:
    #
    cnt_m1 = 0
    cnt_m2 = 0
    cnt_m3 = 0
    for i in d.values():
        if i % 4 == 1:
            cnt_m1 += 1
        elif i % 4 == 2:
            cnt_m2 += 1
        elif i % 4 == 3:
            cnt_m3 += 1
            
    f = True
    if H % 2 == 1 and W % 2 == 1:
        if cnt_m1 + cnt_m3 != 1:
            f = False
        if cnt_m2 + cnt_m3 > (H-1)//2 + (W-1)//2:
            f = False

    elif H % 2 == 1 or W % 2 == 1:
        if cnt_m1 != 0:
            f = False
        if H % 2 == 1:
            if cnt_m2 > (W//2):
                f = False
        else:
            if cnt_m2 > (H//2):
                f = False
        if cnt_m3 != 0:
            f = False
    else:
        if cnt_m1 != 0:
            f = False
        if cnt_m2 > H//2 + W//2:
            f = False
        if cnt_m3 != 0:
            f = False

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

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

H,W = map(int, input().split())
S = [input() for _ in range(H)]

# Aが一文字だけの場合
if H == 1 and W == 1:
    print("Yes")
    exit()

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[S[i][j]] += 1

# 普通の回分の場合
if H == 1 or W == 1:
    # 文字列が偶数個の場合は、それぞれの文字が偶数回出現していればOK
    if H % 2 == 0 or W % 2 == 0:
        f = True
        for i in d.values():
            if i % 2 != 0:
                f = False
        print("Yes" if f else "No")
    # 文字列が奇数個の場合、一つだけ奇数個の文字がある
    else:
        cnt_odd = 0
        for i in d.values():
            if i % 2 != 0:
                cnt_odd += 1
        print("Yes" if cnt_odd == 1 else "NO")
# 行列の回分の場合
# 奇数×奇数のとき、行列の縦と横の真ん中の線の交点に来る文字は奇数個でOK。交点以外の真ん中のライン上の文字は２の倍数個いる。それ以外の位置は４の倍数個いる。
# 奇数×偶数でもおんなじ感じ
# 偶数×偶数は全部４の倍数個ずつ必要
else:
    #
    cnt_m1 = 0
    cnt_m2 = 0
    cnt_m3 = 0
    for i in d.values():
        if i % 4 == 1:
            cnt_m1 += 1
        elif i % 4 == 2:
            cnt_m2 += 1
        elif i % 4 == 3:
            cnt_m3 += 1
            
    f = True
    if H % 2 == 1 and W % 2 == 1:
        if cnt_m1 + cnt_m3 != 1:
            f = False
        if cnt_m2 + cnt_m3 > (H-1)//2 + (W-1)//2:
            f = False

    elif H % 2 == 1 or W % 2 == 1:
        if cnt_m1 != 0:
            f = False
        if H % 2 == 1:
            if cnt_m2 > (W//2):
                f = False
        else:
            if cnt_m2 > (H//2):
                f = False
        if cnt_m3 != 0:
            f = False
    else:
        if cnt_m1 != 0:
            f = False
        if cnt_m2 != 0:
            f = False
        if cnt_m3 != 0:
            f = False

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

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

H,W = map(int, input().split())
S = [input() for _ in range(H)]

# Aが一文字だけの場合
if H == 1 and W == 1:
    print("Yes")
    exit()

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[S[i][j]] += 1

# 普通の回分の場合
if H == 1 or W == 1:
    # 文字列が偶数個の場合は、それぞれの文字が偶数回出現していればOK
    if H % 2 == 0 or W % 2 == 0:
        f = True
        for i in d.values():
            if i % 2 != 0:
                f = False
        print("Yes" if f else "No")
    # 文字列が奇数個の場合、一つだけ奇数個の文字がある
    else:
        cnt_odd = 0
        for i in d.values():
            if i % 2 != 0:
                cnt_odd += 1
        print("Yes" if cnt_odd == 1 else "NO")
# 行列の回分の場合
# 奇数×奇数のとき、行列の縦と横の真ん中の線の交点に来る文字は奇数個でOK。交点以外の真ん中のライン上の文字は２の倍数個いる。それ以外の位置は４の倍数個いる。
# 奇数×偶数でもおんなじ感じ
# 偶数×偶数は全部４の倍数個ずつ必要
else:
    
    # 個数を4で割って１余るのは一個まで。3余った場合は、４で割って２余る個数と、１余る個数に＋１
    cnt_m1 = 0
    cnt_m2 = 0
    for i in d.values():
        if i % 4 == 1:
            cnt_m1 += 1
        elif i % 4 == 2:
            cnt_m2 += 1
        elif i % 4 == 3:
            cnt_m1 += 1
            cnt_m2 += 1
    f = True
    if H % 2 == 1 and W % 2 == 1:
        if cnt_m1 != 1:
            f = False
        if cnt_m2 > (H-1)//2 + (W-1)//2:
            f = False

    elif H % 2 == 1 or W % 2 == 1:
        if cnt_m1 != 0:
            f = False
        if H % 2 == 1:
            if cnt_m2 > (W//2):
                f = False
        else:
            if cnt_m2 > (H//2):
                f = False
    else:
        if cnt_m1 != 0:
            f = False
        if cnt_m2 != 0:
            f = False

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

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

H,W = map(int, input().split())
S = [input() for _ in range(H)]

# Aが一文字だけの場合
if H == 1 and W == 1:
    print("Yes")
    exit()

d = defaultdict(int)
for i in range(H):
    for j in range(W):
        d[S[i][j]] += 1

# 普通の回分の場合
if H == 1 or W == 1:
    # 文字列が偶数個の場合は、それぞれの文字が偶数回出現していればOK
    if H % 2 == 0 or W % 2 == 0:
        f = True
        for i in d.values():
            if i % 2 != 0:
                f = False
        print("Yes" if f else "No")
    # 文字列が奇数個の場合、一つだけ奇数個の文字がある
    else:
        cnt_odd = 0
        for i in d.values():
            if i % 2 != 0:
                cnt_odd += 1
        print("Yes" if cnt_odd == 1 else "NO")
# 行列の回分の場合
else:
    # 奇数 * 奇数の配列の場合、奇数個の文字が一つある
    # また、四隅がうまれるので、４つ以上ある文字が一種以上ある必要がある
    if H % 2 == 1 and W % 2 == 1:
        cnt_odd = 0
        cnt_more_than_4 = 0
        for i in d.values():
            if i % 2 == 1:
                cnt_odd += 1
            if i >= 4:
                cnt_more_than_4 += 1
        print("Yes" if (cnt_odd == 1 and cnt_more_than_4 >= 1) else "No")
    # 奇数 * 奇数でなければ、全部偶数個ある&四つ以上ある文字が一種以上ある
    else:
        cnt_more_than_4 = 0
        f = True
        for i in d.values():
            if i % 2 != 0:
                f = False
            if i >= 4:
                cnt_more_than_4 += 1
        print("Yes" if (f and cnt_more_than_4 >= 1) else "No")

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    cnt = [0]*26
    for _ in range(H):
        for s in ST():
            cnt[ord(s)-ord("a")] += 1
    
    cnt_cnt = defaultdict(int)
    for c in cnt:
        cnt_cnt[c%4] += 1


    #2個セットのは,2ペア集めると4の倍数個ある文字を採用することが出来るのでそれを考慮する

    if H%2==0 and W%2==0: #even even
        if cnt_cnt[0]==26:
            print("Yes")
        else:
            print("No")
    elif H&1 and W&1: #odd odd
        flag1 = (cnt_cnt[1]+cnt_cnt[3]==1)
        cnt_cnt[2]+=cnt_cnt[3] ## 3のやつは真ん中に一つと2個ペアに2つ使う
        flag2 = (cnt_cnt[2]%2==((H//2)+(W//2))%2 and cnt_cnt[2]<=(H//2)+(W//2))
        if flag2 and flag1:
            print("Yes")
        else:
            print("No")
    else: #others
        if cnt_cnt[1] or cnt_cnt[3]:
            print("No")
            return
        if H&1:
            if cnt_cnt[2]%2==(W//2)%2 and cnt_cnt[2]<=(W//2):
                print("Yes")
            else:
                print("No")
        else:
            if (cnt_cnt[2])%2==(H//2)%2 and (cnt_cnt[2])==(H//2):
                print("Yes")
            else:

                print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    cnt = [0]*26
    for _ in range(H):
        for s in ST():
            cnt[ord(s)-ord("a")] += 1
    
    cnt_cnt = defaultdict(int)
    for c in cnt:
        cnt_cnt[c%4] += 1


    #2個セットのは,2ペア集めると4の倍数個ある文字を採用することが出来るのでそれを考慮する

    if H%2==0 and W%2==0: #even even
        if cnt_cnt[0]==26:
            print("Yes")
        else:
            print("No")
    elif H&1 and W&1: #odd odd
        flag1 = (cnt_cnt[1]+cnt_cnt[3]==1)
        cnt_cnt[2]+=cnt_cnt[3] ## 3のやつは真ん中に一つと2個ペアに2つ使う
        flag2 = (cnt_cnt[2]%2==((H//2)+(W//2))%2)
        if flag2 and flag1:
            print("Yes")
        else:
            print("No")
    else: #others
        if cnt_cnt[1] or cnt_cnt[3]:
            print("No")
            return
        if H&1:
            if cnt_cnt[2]%2==(W//2)%2:
                print("Yes")
            else:
                print("No")
        else:
            if (cnt_cnt[2])%2==(H//2)%2:
                print("Yes")
            else:

                print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    cnt = [0]*26
    for _ in range(H):
        for s in ST():
            cnt[ord(s)-ord("a")] += 1
    
    cnt_cnt = defaultdict(int)
    for c in cnt:
        cnt_cnt[c%4] += 1


    #2個セットのは,2ペア集めると4の倍数個ある文字を採用することが出来るのでそれを考慮する

    if H%2==0 and W%2==0: #even even
        if cnt_cnt[0]==26:
            print("Yes")
        else:
            print("No")
    elif H&1 and W&1: #odd odd
        flag1 = (cnt_cnt[1]==1)
        flag2 = (cnt_cnt[2]%2==((H//2)+(W//2))%2)
        if flag2 and flag1:
            print("Yes")
        else:
            print("No")
    else: #others
        if cnt_cnt[1]:
            print("No")
            return
        if H&1:
            if cnt_cnt[2]%2==(W//2)%2:
                print("Yes")
            else:
                print("No")
        else:
            if (cnt_cnt[2])%2==(H//2)%2:
                print("Yes")
            else:

                print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    cnt = [0]*26
    for _ in range(H):
        for s in ST():
            cnt[ord(s)-ord("a")] += 1
    
    cnt_cnt = defaultdict(int)
    for c in cnt:
        cnt_cnt[c%4] += 1


    if H%2==0 and W%2==0: #even even
        if cnt_cnt[0]==26:
            print("Yes")
        else:
            print("No")
    elif H&1 and W&1: #odd odd
        flag1 = (cnt_cnt[1]==1)
        flag2 = (cnt_cnt[2]==(H//2)+(W//2))
        if flag2 and flag1:
            print("Yes")
        else:
            print("No")
    else: #others
        if cnt_cnt[1]:
            print("No")
            return
        if H&1:
            if cnt_cnt[2]==W//2:
                print("Yes")
            else:
                print("No")
        else:
            if cnt_cnt[2]==H//2:
                print("Yes")
            else:
                
                print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
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:
      print("No")
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")
      sys.exit()
    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")
  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")
      sys.exit()
    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")
  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")
        sys.exit()
    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")
        sys.exit()
    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")
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
a = []
dicta = {}
for i in range(H):
    inp = list(input())
    a.append(inp)
    for j in inp:
        if j not in dicta:
            dicta[j] = 1
        else:
            dicta[j] += 1

ans = 'Yes'
count4 = 0
count2 = 0
count1 = 0
for v in dicta.values():
    if v%4 == 0:
        count4 += 1
    if v%4 != 0 and v%2 == 0:
        count2 += 1
    if v%2 != 0:
        count1 += 1

if H%2 == W%2 == 0:
    for v in dicta.values():
        if count2 > 0 or count1 > 0:
            ans = 'No'
            break
elif H%2 != 0 and W%2 != 0:
    if count1 != 1:
        ans ='No'
    else:
        num4 = (H//2)*(W//2)
        num2 = (H//2)+(W//2)
        if num2 != count2:
            ans = 'No'
elif H%2 != 0 or W%2 != 0:
    if count1 != 0:
        ans ='No'
    else:
        num2 = (W//2)*((W+1)%2) + (H//2)*((H+1)%2)
        num4 = (W//2)*(H//2)
        if num2 != count2:
            ans = 'No'
        
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
a = [list(input()) for _ in range(H)]

if H > W:
    H, W = W, H

count = [0] * 26
for i in a:
    for j in i:
        count[ord(j) - ord('a')] += 1

# print (count)
lst = [0] * 5
for c in count:
    lst[4] += c // 4
    c %= 4
    lst[2] += c // 2
    c %= 2
    lst[1] += c

# print (lst)
flag = False
if H >= 2 and W >= 2:
    if (H // 2) * (W // 2) <= lst[4]:
        lst[2] += (lst[4] - (H // 2) * (W // 2)) * 2 
        # print ('A')
        # print (lst)
        if ((H % 2) * (W // 2)) + ((W % 2) * (H // 2)) == lst[2] and (H % 2) * (W % 2) == lst[1]:
            flag = True
            # print ('B')

if H == 1 and lst[1] == 1:
    flag = True

if flag:
    print ('Yes')
else:
    print ('No')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [input() for _ in range(H)]

def check(n4, n2):

  for i in range(27):
    while alpha[i] >= 4:
      alpha[i] -= 4
      n4 -= 1
      if n4 == 0:
        break
    if n4 == 0:
      break

  for i in range(27):
    while alpha[i] >= 2:
      alpha[i] -= 2
      n2 -= 1
      if n2 == 0:
        break
    if n2 == 0:
      break
  if n4 == n2 == 0:
    print("Yes")
  else:
    print("No")      

alpha = [0] * 27
for i in range(H):
  for j in range(W):
    alpha[ord(A[i][j]) - ord("a")] += 1

if H % 2 and W % 2:
  n4 = (H * W - (H + W) + 1) // 4
  n2 = (H + W - 2) // 2
elif H % 2 == 0 and W % 2 == 0:
  n4 = H * W // 4
  n2 = 0
else:
  if H % 2:
    H, W = W, H
  n4 = (W - 1) * H //4
  n2 = H // 2
check(n4, n2)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [input() for _ in range(H)]

def check(n4, n2):
  for a in alpha:
    while a >= 4:
      a -= 4
      n4 -= 1
      if n4 == 0:
        break
    if n4 == 0:
      break

  for a in alpha:
    while a >= 2:
      a -= 2
      n2 -= 1
      if n2 == 0:
        break
    if n2 == 0:
      break
  if n4 == n2 == 0:
    print("Yes")
  else:
    print("No")      

alpha = [0] * 27
for i in range(H):
  for j in range(W):
    alpha[ord(A[i][j]) - ord("a")] += 1
#print(alpha)

if H % 2 and W % 2:
  n4 = (H * W - (H + W) + 1) // 4
  n2 = (H + W - 2) // 2
elif H % 2 == 0 and W % 2 == 0:
  n4 = H * W // 4
  n2 = 0
else:
  if H % 2:
    H, W = W, H
  n4 = (W - 1) * H //4
  n2 = H // 2

#print(n4, n2)
check(n4, n2)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
cntA = {}
for _ in range(H):
  for a in input():
    if a not in cntA:
      cntA[a] = 1
    else:
      cntA[a] += 1

cnts = sorted(cntA.values())
len_cnts = len(cnts)
if H>2 and H==W and len_cnts:
  print('Yes')
elif (H>1 and W>1) and len_cnts >= max(H,W):
  print('No')
else:
  num_odd = 0
  for cnt in cnts:
    if cnt%2==1:
      num_odd += 1
      if num_odd>=2:
        print('No')
        exit()
  print('Yes')

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
cntA = {}
for _ in range(H):
  for a in input():
    if a not in cntA:
      cntA[a] = 1
    else:
      cntA[a] += 1

cnts = sorted(cntA.values())
len_cnts = len(cnts)
if (H>1 and W>1) and len_cnts >= max(H,W):
  print('No')
else:
  num_odd = 0
  for cnt in cnts:
    if cnt%2==1:
      num_odd += 1
      if num_odd>=2:
        print('No')
        exit()
  print('Yes')

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
cntA = {}
for _ in range(H):
  for a in input():
    if a not in cntA:
      cntA[a] = 1
    else:
      cntA[a] += 1

cnts = sorted(cntA.values())
if cnts == [2,2]:
  print('No')
else:
  num_odd = 0
  for cnt in cnts:
    if cnt%2==1:
      num_odd += 1
      if num_odd>=2:
        print('No')
        exit()
  print('Yes')

Submission status: Wrong Answer

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

flag = True
count_odd = 0
count_two = 0
if h == 1 and w == 1:
    pass
elif h == 1 or w == 1:
    for i in range(26):
        if count[i] % 2 != 0:
            count_odd += 1
            if count_odd > 1:
                flag = False
elif h % 2 == 0 and w % 2 == 0:
    # 全て4の倍数であればよい
    for i in range(26):
        if count[i] % 4 != 0:
            flag = False
elif h % 2 != 0 and w % 2 == 0:
    for i in range(26):
        if count[i] % 2 != 0:
            count_odd += 1
            if count_odd > 1:
                flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > h + w - 2:
                flag = False
elif h % 2 != 0:
    for i in range(26):
        if count[i] % 2 != 0:
            flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > w:
                flag = False
elif w % 2 != 0:
    for i in range(26):
        if count[i] % 2 != 0:
            flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > h:
                flag = False

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

Submission status: Wrong Answer

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

flag = True
count_odd = 0
count_two = 0

if h % 2 == 0 and w % 2 == 0:
    # 全て4の倍数であればよい
    for i in range(26):
        if count[i] % 4 != 0:
            flag = False
elif h % 2 != 0 and w % 2 == 0:
    for i in range(26):
        if count[i] % 2 != 0:
            count_odd += 1
            if count_odd > 1:
                flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > h + w - 2:
                flag = False
elif h % 2 != 0:
    for i in range(26):
        if count[i] % 2 != 0:
            flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > w:
                flag = False
elif w % 2 != 0:
    for i in range(26):
        if count[i] % 2 != 0:
            flag = False
        elif count[i] % 4 != 0:
            count_two += 2
            if count_two > h:
                flag = False

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

H, W = rl()
l = []
for _ in range(H):
    l.extend(list(rs()))

c = Counter(l)
count = W//2 * H
pair = 0
four = 0
for x in c.values():
    four += x // 4
    pair += x // 2

f = H // 2 * (W // 2)
if f > four:
    print('No')
    exit()

pair -= f * 2 #残りのペア
p = W // 2 if H&1 == 0 else 0
p += H // 2 if W&1 == 0 else 0
if p > pair:
    print('No')
    exit()

print('Yes')
# 16

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
table = [0] * 26
a = ord("a")
for _ in range(H):
  s = list(input())[: -1]
  for j in range(W):
    table[ord(s[j]) - a] += 1
#print(table)
t = []
for i in range(26):
  if table[i] > 0: t.append(table[i])

x = 0
y = 0
c = 0
if H % 2 and (W % 2):
  c = 1
if H % 2: y += (W - c) // 2
if W % 2: x += (H - c) // 2
#print(x, y, c)
if H > 1 and (W > 1):
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
    if p % 4:
      if x > 0:
        x -= 1
      elif y > 0:
        y -= 1
      else:
        print("No")
        exit(0)
else:
  for p in t:
    if p % 2:
      if c > 0:
        c -= 1
      else:
        print("No")
        exit(0)
print("Yes")