Problem:


Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes



Sample input:
3 4
aabb
aabb
aacc


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
h, w = map(int, raw_input().split())
d = {}
for i in range(h):
    t = raw_input()
    for j in range(w):
        if d.has_key(t[j]):
            d[t[j]] += 1
        else:
            d[t[j]] = 1

two = 0
one = 0
four = 0
if w < 2:
    two = h // 2
    one = h % 2
elif h < 2:
    two = w //2
    one = h % 2
elif h%2 == 0:
    if w%2 == 0:
        four = h*w/4
    else:
        four = h//2 + (w-2)//2
        two = h//2
else:
    if w%2 == 0:
        four = w//2 + (h-2)//2
        two = w//2
    else:
        four = w//2 + (h-2)//2
        one = 1
        two = w//2 + h//2

def calc(dic, num, n):
    for key, value in dic.items():
        if n==0:
            return True, dic
        if value >= num:
            dic[key] -= num
            n -= 1
    if n:
        return False, dic
    else:
        return True, dic

result, d = calc(d, 4, four)
if result:
    result, d = calc(d, 2, two)
if result:
    result, d = calc(d, 1, one)

if result:
    print 'Yes'
else:
    print 'No'

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
H,W = map(int, input().split())

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    kisuu = 0
    kisugusu = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1
        if a % 2 == 1:
            kisuu += 1
        if a % 2 == 0:
            tmp = a / 2
            while(tmp != 1):
                if tmp % 2 == 1:
                    kisugusu += 1
                    break   
                tmp /= 2             

    if one > 1 or ((H * W) % 2 == 1 and (one == 0 or kisugusu == 0)):
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            if kisuu > 1:
                print('No')
            else:
                if H % 2 == 0 and W % 2 == 0 and kisugusu > 0:
                    print('No')
                else:
                    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())

target4 = int(H / 2) * int(W / 2)
if H % 2 == 0:
    if W % 2 == 0:
        target2 = 0
    else:
        target2 = int(H / 2)
else:
    if W % 2 == 0:
        target2 = int(W / 2)
    else:
        target2 = max(int(W / 2), int(H / 2))
target1 = 1 if (H % 2 == 1 and W % 2 == 1) else 0

a = [input() for _ in range(H)]

flag = False

count1 = 0
count2 = 0
count4 = 0
for i in range(26):
    count = sum([a[h].count(chr(97 + i)) for h in range(H)])
    count4 += int(count / 4)
    count2 += int((count - int(count / 4) * 4) / 2)
    count1 += count % 2

if target1 != count1:
    print('No')
elif target2 < count2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')
                
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1) or (flag1 == 0 and flag2 == 3 and flag3 == 1) or (flag1 == 0 and flag2 == 1 and flag3 == 1):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')
                
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1) or (flag1 == 0 and flag2 == 3 and flag3 == 1):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())

target4 = int(H / 2) * int(W / 2)
if H % 2 == 0:
    if W % 2 == 0:
        target2 = 0
    else:
        target2 = int(H / 2)
else:
    if W % 2 == 0:
        target2 = int(W / 2)
    else:
        target2 = max(int(W / 2), int(H / 2))
target1 = 1 if (H % 2 == 1 and W % 2 == 1) else 0

a = [input() for _ in range(H)]

flag = False

count1 = 0
count2 = 0
count4 = 0
for i in range(26):
    count = sum([a[h].count(chr(97 + i)) for h in range(H)])
    count4 += int(count / 4)
    count2 += int((count - int(count / 4) * 4) / 2)
    count1 += count % 2

if target1 < count1:
    print('No')
elif target2 < count2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')
                
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1) or (flag1 == 0 and flag2 == 3 and flag3 == 1):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

Submission status: Wrong Answer

Solution code:
h,w = [int(d) for d in input().split()]
a=[]
for i in range(h):
    l=[d for d in input()]
    a.append(l)


found=False
cfound=False
for i in range(h):
    row = {}
    cnt=0
    for j in range(w):
        if a[i][j] in row:
            del row[a[i][j]]
        else:
            row[a[i][j]]=1
    for k in row:
        cnt+=row[k]
    if cnt==0 or cnt==1:
        found=True
    else:
        found=False
        break



for i in range(w):
    col = {}
    cnt1=0
    for j in range(h):
        if a[j][i] in col:
            del col[a[j][i]]
        else:
            col[a[j][i]] = 1
    for k in col:
        cnt1+=col[k]
    if cnt1==0 or cnt1==1:
        cfound=True
    else:
        cfound=False
        break

if found and cfound:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
H,W = map(int, input().split())

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    kisuu = 0
    kisugusu = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1
        if a % 2 == 1:
            kisuu += 1
        if a % 2 == 0:
            tmp = a / 2
            while(tmp != 1):
                if tmp % 2 == 1:
                    kisugusu += 1
                    break   
                tmp /= 2             

    if one > 1 or ((H * W) % 2 == 1 and one == 0):
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            if kisuu > 1:
                print('No')
            else:
                if H % 2 == 0 and W % 2 == 0 and kisugusu > 0:
                    print('No')
                else:
                    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import pdb
from collections import Counter
h, w = [int(i) for i in input().split()]
array = [[i for i in input()] for _ in range(h)]
words = ''.join([i for seq in array for i in seq])
# print(words)

# print(Counter(words))
# print(dict(Counter(words)))
word_freq = dict(Counter(words))

def check_2d_palindrome(h, w, word_freq):
    flag = True
    # vocab size check
    ch = h
    cw = w
    if h % 2 != 0:
        ch = h // 2 + 1
    else:
        ch = h // 2

    if w % 2 != 0:
        cw = w // 2 + 1
    else:
        cw = w //2

    # pdb.set_trace()
    if len(word_freq) > ch * cw:
        flag = False
        return flag

    if (h * w % 2) == 0:
        # even
        for freq in word_freq.values():
            if freq % 2 != 0:
                flag = False
                break
    else:
        # odd
        odd_count = 0
        for freq in word_freq.values():
            if freq % 2 != 0:
                odd_count += 1
            if odd_count > 1:
                flag = False
                break
    return flag


ret = check_2d_palindrome(h, w, word_freq)
if ret:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')
                
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

Submission status: Wrong Answer

Solution code:

from collections import Counter
h, w = [int(i) for i in input().split()]
array = [[i for i in input()] for _ in range(h)]
words = ''.join([i for seq in array for i in seq])
# print(words)

# print(Counter(words))
# print(dict(Counter(words)))
word_freq = dict(Counter(words))

def check_2d_palindrome(h, w, word_freq):
    flag = True
    # vocab size check
    ch = h
    cw = w
    if h % 2 != 0:
        ch = h // 2 + 1
    else:
        ch = h // 2

    if w % 2 != 0:
        cw = w // 2 + 1
    else:
        cw = w //2

    # pdb.set_trace()
    if len(word_freq) > ch * cw:
        flag = False
        return flag

    if (h * w % 2) == 0:
        # even
        for freq in word_freq.values():
            if freq % 2 != 0:
                flag = False
    else:
        # odd
        odd_count = 0
        for freq in word_freq.values():
            if freq % 2 != 0:
                odd_count += 1
            if odd_count > 2:
                flag = False
                break
    return flag


ret = check_2d_palindrome(h, w, word_freq)
if ret:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
H,W = map(int, input().split())

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    kisuu = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1
        if a % 2 == 1:
            kisuu += 1

    if one > 1 or ((H * W) % 2 == 1 and one == 0):
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            if kisuu > 1:
                print('No')
            else:
                print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            print('No')
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1):
            print('Yes')
        else:
            print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            li_uniq = list(set(li))
            if len(li_uniq) == 1:
                print('Yes')
            else:
                print('No')

Submission status: Wrong Answer

Solution code:
import itertools
import sys

h, w = map(int, input().split())

a = [input() for i in range(h)]
words = list(set(itertools.chain(*a)))
at = list(itertools.chain(*a))

mods = []
for word in words:
    for i in range(at.count(word) // 4):
        mods.append(4)
    mod = at.count(word) % 4
    if mod:
        mods.append(mod)

mods = list(sorted(mods))
def count_down(l, n):
    for i in range(len(l)):
        if l[i] - n >= 0:
            l[i] -= n
            if not l[i]:
                del l[i]
            return True
    return False

if h % 2 and w % 2:
    flg = False
    for i in range(w // 2 + h // 2):
        flg = count_down(mods, 2)
    if flg and count_down(mods, 1) and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

elif h % 2:
    flg = False
    for i in range(w // 2):
        flg = count_down(mods, 2)
    if flg and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

elif w % 2:
    flg = False
    for i in range(h // 2):
        flg = count_down(mods, 2)
    if flg and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

else:
    if mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

print("No")

Submission status: Wrong Answer

Solution code:
h,w = [int(d) for d in input().split()]
a=[]
for i in range(h):
    l=[d for d in input()]
    a.append(l)


found=False
cfound=False
for i in range(h):
    row = {}
    cnt=0
    rcnt=0
    for j in range(w):
        if a[i][j] in row:
            row[a[i][j]]-=1
        else:
            row[a[i][j]]=1
    for k in row:
        cnt+=row[k]
        rcnt+=1
    if rcnt!=1:
        if cnt==0 or cnt==1:
            found=True
        else:
            found=False
            break
    else:
        found=True



for i in range(w):
    col = {}
    cnt1=0
    clcnt=0
    for j in range(h):
        if a[j][i] in col:
            col[a[j][i]] -= 1
        else:
            col[a[j][i]] = 1
    for k in col:
        cnt1+=col[k]
    if clcnt!=1:
        if cnt==0 or cnt==1:
            cfound=True
        else:
            cfound=False
            break
    else:
        cfound=True

if found and cfound:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#! -*- coding:utf-8 -*-
import collections
H,W = map(int,input().split())

arr = [[0]*W]*H

for i in range(H):
    arr[i] = list(input())
    
if H == 1 and W == 1:
    print('Yes')
elif H == 1 or W == 1:
    stock = []
    count = 0
    for i in range(H):
        for j in range(W):
            if arr[i][j] in stock:
                count += 1
                stock.remove(arr[i][j])
            else:
                stock.append(arr[i][j])

    if H == 1 and W%2 == 0:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif H == 1 and W%2 == 1:
        if count == W//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 0:
        if count == H//2:
            print('Yes')
        else:
            print('No')
    elif W == 1 and H%2 == 1:
        if count == H//2:
            print('Yes')
        else:
            print('No')
            
else:
    li = [ y for x in arr for y in x]
    count_dict = collections.Counter(li)
    if H%2 == 0 and W%2 == 0:
        flag = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                flag = 1

        if flag == 0:
            print('Yes')
        else:
            print('No')
    elif H%2 == 1 and W%2 == 1:
        flag1 = 0
        flag2 = 0
        flag3 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 2 == 0:
                    flag2 += 1
                elif v % 2 == 1:
                    flag3 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2,flag3)
        if (flag1 == 0 and flag2 == 2 and flag3 == 1) or (flag1 == 0 and flag2 == 0 and flag3 == 1):
            print('Yes')
        else:
            print('No')

    else:
        flag1 = 0
        flag2 = 0
        for k,v in count_dict.items():
            if v%4 != 0:
                if v % 4 == 2:
                    flag2 += 1
                else:
                    flag1 = 1
        #print(flag1,flag2)
        if H%2 == 1:
            x = W//2
        elif W%2 == 1:
            x = H//2
        if (flag1 == 0 and flag2 == x):
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
H,W = map(int, input().split())

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1

    if one > 1 or ((H * W) % 2 == 1 and one == 0):
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
hoge = [int(x) for x in input().split()]
moji = ''
char = {}
for i in range(hoge[0]):
    moji += input()
for m in moji:
    if m in char:
        char[m] += 1
    else:
        char.update({m:1})
if hoge[0] % 2 == 1 and hoge[1] % 2 == 1:
    one = 1
    two = (hoge[0]-1)/2 + (hoge[1]-1)/2
    four = (hoge[0]-1)/2 * (hoge[1]-1)/2
elif hoge[0] % 2 == 1 and hoge[1] % 2 == 0:
    one = 0
    two = hoge[1]/2
    four = (hoge[0]-1)/2 * hoge[1]/2
elif hoge[0] % 2 == 0 and hoge[1] % 2 == 1:
    one = 0
    two = hoge[0]/2
    four = (hoge[1]-1)/2 * hoge[0]/2
else:
    one = 0
    two = 0
    four = hoge[1]/2 * hoge[0]/2

foo = 0
for c in char:
    while(char[c] > 0):
        if char[c] >= 4:
            char[c] -= 4
            four -= 1
        elif char[c] >= 2:
            char[c] -= 2
            two -= 1
        else:
            char[c] -= 1
            one -= 1
        if four < 0 or two < 0 or one < 0:
            foo = 1
            break
    if foo == 1:
        break

if foo == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
H,W = map(int, input().split())

a = [
    input()
    for _ in range(H)
]

tmp = []
for aa in a:
    for c in aa:
        tmp.append(c)


cc = Counter(tmp)

if (int(math.ceil(H/2.0)) + int(math.ceil(W/2.0)) - 1 - len(cc.keys())) >= 0:
    one = 0
    two = 0
    for _, a in cc.items():
        if a == 1:
            one += 1
        if a == 2:
            two += 1

    if one > 1:
        print('No')
    else:
        if H % 2 == 0 and W % 2 == 0 and two > 0:
            print('No')
        else:
            print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import itertools
import sys

h, w = map(int, input().split())

a = [input() for i in range(h)]
words = list(set(itertools.chain(*a)))
at = list(itertools.chain(*a))

mods = []
for word in words:
    mod = at.count(word) % 4
    if mod:
        mods.append(mod)
    else:
        mods.append(4)

mods = list(sorted(mods))
def count_down(l, n):
    for i in range(len(l)):
        if l[i] - n >= 0:
            l[i] -= n
            if not l[i]:
                del l[i]
            return True
    return False

if h % 2 and w % 2:
    flg = False
    for i in range(w // 2 + h // 2):
        flg = count_down(mods, 2)
    if flg and count_down(mods, 1) and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

elif h % 2:
    flg = False
    for i in range(w // 2):
        flg = count_down(mods, 2)
    if flg and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

elif w % 2:
    flg = False
    for i in range(h // 2):
        flg = count_down(mods, 2)
    if flg and mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

else:
    if mods.count(4) == len(mods):
        print("Yes")
        sys.exit()

print("No")