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

Solution (Python):
H, W = map(int, raw_input().split(' '))

counts = {}
for h in xrange(H):
  for c in raw_input():
    counts[c] = counts.get(c, 0) + 1

odd = two = 0
for count in counts.itervalues():
  if count % 2 == 1:
    odd += 1
  elif count % 4 != 0:
    two += 1

if (H * W) % 2 == 1:
  
  ans = odd <= 1 and two <= (H-1)/2 + (W-1)/2
elif H % 2 == 1:
  ans = (odd == 0 and two <= W/2)
elif W % 2 == 1:
  
  ans = (odd == 0 and two <= H/2)
else:
  
  ans = (odd == 0 and two == 0)

print ['No', 'Yes'][ans]

Solution (Python):
import string

[H,W]=map(int, raw_input().split())
lw=string.ascii_lowercase
A=[0 for i in range(26)]
for i in range(H):
    st=raw_input()
    for c in st:
        A[lw.index(c)]+=1

K=(W/2)*(H/2)
L=(W*(H%2)+H*(W%2)-2*(H%2)*(W%2))/2
M=(H%2)*(W%2)

f=0
ans=["No","Yes"]
P=sum([a/4 for a in A])

if P>=K:
    P-=K
    while(K>0):
        for i in range(26):
            if A[i]>=4:
                A[i]-=4
                K-=1
                if K==0: break
        
    Q=sum([a/2 for a in A])
    
    if Q>=L:
        Q-=L
        L=0
        R=Q*2+sum([a%2 for a in A])
        
        if R>=M:
            f=1
print ans[f]

Solution (Python):
H, W = [int(x) for x in input().split(" ")]

d = {}

for h in range(H):
    row = input()
    for a in list(row):
        if a in d:
            d[a] += 1
        else:
            d[a] = 1


res = False

if H % 2 == 0 and W % 2 == 0:
    for v in d.values():
        if v % 4 != 0:
            res = True
            break
    res = not res

elif H % 2 == 1 and W % 2 == 0:
    cnt4 = (W // 2) * ((H - 1) // 2)
    cnt2 = (W // 2)
    temp4 = 0
    temp2 = 0
    f = True
    for v in d.values():
        if v % 2 != 0:
            f = False
            break

        temp4 += v // 4
        temp2 += 1 if v % 4 != 0 else 0
    
    if f and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True
elif H % 2 == 0 and W % 2 == 1:
    cnt4 = (H // 2) * ((W - 1) // 2)
    cnt2 = (H // 2)
    temp4 = 0
    temp2 = 0
    f = True
    for v in d.values():
        if v % 2 != 0:
            f = False
            break

        temp4 += v // 4
        temp2 += 1 if v % 4 != 0 else 0
    
    if f and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True
else:
    cnt4 = ((H - 1) // 2) * ((W - 1) // 2)
    cnt2 = ((H - 1) // 2) + ((W - 1) // 2)
    temp4 = 0
    temp2 = 0
    temp1 = 0

    for v in d.values():
        temp4 += v // 4
        temp2 += (v % 4) // 2
        temp1 += (v % 2)
    
    if temp1 == 1 and cnt2 >= temp2 and (cnt2 - temp2) % 2 == 0:
        res = True

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

Solution (Python):



hw = raw_input()
h,w = hw.split()
h = int(h)
w = int(w)

al = [chr(i) for i in range(97, 97+26)]

a = ""
for i in range(h):
    s = raw_input()
    a += s





c = []

for x in al:
    count = a.count(x)
    c.append(count)



status1 = 0
status2 = 0
for x in c:
    if x % 4 == 0:
        pass
    elif x % 2 == 0:
        status1 += 1
    else:
        status2 += 1

ans = 0
if h % 2 == 0 and w % 2 == 0:
    if status1 > 0 or status2 > 0:
        ans = 1
elif h % 2 == 0:
    if status2 > 0:
        ans = 1
    elif status1 > h / 2:
        ans = 1
elif w % 2 == 0:
    if status2 > 0:
        ans = 1
    elif status1 > w / 2:
        ans = 1
else:
    if status2 > 1:
        ans = 1
    elif status1 > h / 2 + w / 2:
        ans = 1

if ans == 0:
    print "Yes"
else:
    print "No"

Solution (Python):

from collections import defaultdict

def II(): return int(input())
def ILI(): return list(map(int, input().split()))


def read():
    H, W = ILI()
    count = defaultdict(int)
    for __ in range(H):
        sen = str(input())
        for i in range(W):
            count[sen[i]] += 1
    return H, W, count


def solve(H, W, count):
    odd = 0
    num_2 = 0
    num_4 = 0
    for val in count.values():
        div_2, mod_2 = divmod(val, 2)
        odd += mod_2
        div_2 *= 2
        div_4, mod_4 = divmod(div_2, 4)
        num_4 += div_4 * 4
        num_2 += mod_4
    ans = 0
    if H % 2 == 0:
        if W % 2 == 0:
            if odd == 0:
                if num_4 == H * W:
                    ans = "Yes"
                else:
                    ans = "No"
            else:
                ans = "No"
        else:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > H:
                    ans = "No"
                else:
                    ans = "Yes"
    else:
        if W % 2 == 0:
            if odd != 0:
                ans = "No"
            else:
                if num_2 > W:
                    ans = "No"
                else:
                    ans = "Yes"
        else:
            if odd != 1:
                ans = "No"
            else:
                if num_2 > (W - 1) + (H - 1):
                    ans = "No"
                else:
                    ans = "Yes"
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
H,W=map(int,input().split())
L=[0]*26
for i in range(H):
  A=str(input())
  for j in range(W):
    L[ord(A[j])-97]+=1

fours0=0
fours1=0
fours2=0
fours3=0
for i in range(26):
  if L[i]%4==0:
    fours0+=1
  if L[i]%4==1:
    fours1+=1
  if L[i]%4==2:
    fours2+=1
  if L[i]%4==3:
    fours3+=1

if W%2==0 and H%2==0:
  if fours1+fours2+fours3==0: print("Yes")
  else: print("No")
elif W%2==1 and H%2==1:
  if fours1+fours3<=1 and fours1+fours3+2*fours2<=W+H-1:
    print("Yes")
  else: print("No")
elif W%2==1: 
  if fours1+fours3==0 and fours2*2<=H:
    print("Yes")
  else: print("No")
else: 
  if fours1+fours3==0 and fours2*2<=W:
    print("Yes")
  else: print("No")

Solution (Python):

import heapq
import math
import sys

tmp = input()
H, W = [int(a) for a in tmp.split()]
is_evens = [a % 2 == 0 for a in [H, W]]
H2, W2 = [(a+1) // 2 for a in [H, W]]

chars = []
for i in range(H):
    tmp = input()
    chars = chars + [a for a in tmp]
char_counts = {}
for char in chars:
    if char not in char_counts:
        char_counts[char] = 0
    char_counts[char] += 1

valids = {'4': 0, '2': 0, '1': 0}
if is_evens[0] and is_evens[1]:
    valids['4'] = H2 * W2
elif is_evens[0] and not is_evens[1]:
    valids['4'] = (W2 - 1) * H2
    valids['2'] = H2
elif is_evens[1] and not is_evens[0]:
    valids['4'] = (H2 - 1) * W2
    valids['2'] = W2
else:
    valids['4'] = (H2 - 1) * (W2 - 1)
    valids['2'] = (H2 - 1) + (W2 - 1)
    valids['1'] = 1
nows = {'4': 0, '2': 0, '1': 0}
for c, num_ in char_counts.items():
    num = num_
    if num % 2 == 1:
        nows['1'] += 1
        num -= 1
        if nows['1'] > valids['1']:
            print('No')
            sys.exit()
    if num % 4 == 2:
        nows['2'] += 1
        num -= 2
        if nows['2'] > valids['2']:
            print('No')
            sys.exit()
    nows['4'] += (num // 4)
if nows['4'] > valids['4']:
    amari = nows['4'] - valids['4']
    nows['2'] += amari * 2
    if nows['2'] > valids['2']:
        print('No')
        sys.exit()
print('Yes')

Solution (Python):
from collections import Counter
import string
H,W = list(map(int,input().split()))


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

c= Counter("".join(a))


if H==1 or W==1:
    allow1 =H %2 + W%2 -1
    allow2 = (H+W-1)//2
else:
    allow1 = 1 if H%2 and W%2 else 0
    allow2 = H%2*W//2 + W%2*H//2

for s in string.ascii_lowercase:
    count = c[s]
    if count%4==0:
        continue

    elif count%4==2:
        allow2 -=1

    elif count%4 ==1:
        allow1 -=1
    elif count%4==3:
        allow1 -=1
        allow2 -=1

if allow1>=0 and allow2 >=0:
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import Counter


def solve(h, w, a):
    c = Counter(a)
    m1, m2 = 0, 0
    for v in c.values():
        if v & 3 == 0:
            continue
        elif v & 1 == 0:
            m2 += 1
        else:
            m1 += 1
            if (v - 1) & 3:
                m2 += 1

    if h & 1:
        if w & 1:
            if m1 > 1:
                return 'No'
            if m2 > h // 2 + w // 2:
                return 'No'
            return 'Yes'
        if m1 > 0:
            return 'No'
        if m2 > w // 2:
            return 'No'
        return 'Yes'
    if m1 > 0:
        return 'No'
    if w & 1:
        if m2 > h // 2:
            return 'No'
        return 'Yes'
    if m2 > 0:
        return 'No'
    return 'Yes'


h, w = map(int, input().split())
a = ''.join(input().strip() for _ in range(h))
print(solve(h, w, a))

Solution (Python):
import sys
import socket
from collections import Counter

if socket.gethostname() == 'N551J':
    sys.stdin = open('c1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve():
    h, w = read_int_list()
    c = Counter()
    for i in range(h):
        c += Counter(read_str())
    values = sorted(c.values(), reverse=True)
    m = [4] * ((h // 2) * (w // 2))
    if h % 2 == 1:
        m = m + [2] * (w // 2)
    if w % 2 == 1:
        m = m + [2] * (h // 2)
    if h % 2 == 1 and w % 2 == 1:
        m = m + [1]
    for v in m:
        if values[0] < v:
            return 'No'
        else:
            values[0] -= v
            if values[0] == 0:
                values.pop(0)
            else:
                values.sort(reverse=True)
    return 'Yes'


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):


import collections

def main():
    H, W = map(int, input().split())
    cnt = collections.Counter()
    for i in range(H):
        for c in input().strip():
            cnt[c] += 1
    cnts = list(cnt.values())
    l = len(cnts)

    if H % 2 and W % 2:
        for i in range(l):
            if cnts[i] % 2 != 0:
                cnts[i] -= 1
                break
    d = 0
    if H % 2:
        d += (W // 2) * 2
    if W % 2:
        d += (H // 2) * 2
    i = 0
    while d > 0 and i < l:
        if cnts[i] % 4 != 0:
            cnts[i] -= 2
            d -= 2
        i += 1

    for k in cnts:
        if k % 4 != 0:
            return "No"
    return "Yes"


print(main())

Solution (Python):
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]

cnt = {}
for ai in a:
    for c in ai:
        if c in cnt:
            cnt[c] += 1
        else:
            cnt[c] = 1
c1 = 0
c2 = 0
c4 = 0
for k, v in cnt.items():
    c4 += (v // 4) * 4
    c2 += ((v % 4) // 2) * 2
    c1 += v % 2

ans = "No"
if h % 2 == 0:
    if w % 2 == 0:
        if c4 == h * w:
            ans = "Yes"
    else:
        if c2 <= h and c1 == 0:
            ans = "Yes"
else:
    if w % 2 == 0:
        if c2 <= w and c1 == 0:
            ans = "Yes"
    else:
        if c2 <= h + w - 1 and c1 == 1:
            ans = "Yes"
print(ans)

Solution (Python):
from collections import Counter
H, W = map(int, input().split())
cnt4, cnt2, cnt1 = 0, 0, 0
for v in Counter(''.join(input() for _ in range(H))).values():
    if v % 4 == 0:
        continue
    elif v % 2 == 0:
        cnt2 += 1
    else:
        cnt1 += 1
        if (v - 1) % 4 != 0:
            cnt2 += 1
odd = (H % 2 == 1) + (W % 2 == 1)
if odd == 2:
    if cnt1 <= 1 and cnt2 <= (H + W - 2) // 2:
        print("Yes")
    else:
        print("No")
elif odd == 1 and H % 2 == 1:
    if cnt1 == 0 and cnt2 <= W // 2:
        print("Yes")
    else:
        print("No")
elif odd == 1 and W % 2 == 1:
    if cnt1 == 0 and cnt2 <= H // 2:
        print("Yes")
    else:
        print("No")
else:
    if cnt1 == 0 and cnt2 == 0:
        print("Yes")
    else:
        print("No")

Solution (Python):
h, w = map(int, input().split())
C = {}
for i in range(h):
    for c in input():
        C[c] = C.get(c, 0) + 1
if h == 1 or w == 1:
    odd = 0
    for c in C:
        if C[c] % 2 == 1:
            odd += 1
    if h % 2 == 1 and w % 2 == 1:
        if odd == 1:
            print('Yes')
        else:
            print('No')
    else:
        if odd == 0:
            print('Yes')
        else:
            print('No')
else:
    
    four = 0
    odd = 0
    for c in C:
        if C[c] % 2 == 1:
            odd += 1
        if C[c] >= 4:
            four += C[c]//4
    needf = (h//2*2) * (w//2*2) // 4
    
    if h % 2 == 1 and w % 2 == 1:
        if four >= needf and odd == 1:
            print('Yes')
        else:
            print('No')
    else:
        if four >= needf and odd == 0:
            print('Yes')
        else:
            print('No')

Solution (Python):

from collections import Counter

def check():
    h, w = map(int, input().split()) 
    c = Counter()
    for i in range(h):
        c.update(input())
    cc = {}
    cc[4] = (h >> 1) * (w >> 1)
    cc[2] = (h & 1) * (w >> 1) + (w & 1) * (h >> 1)
    cc[1] = (h & 1) * (w & 1)
    tt = list(c.values())
    for v in [4, 2, 1]:
        for i in range(len(tt)):
            k = min(cc[v], tt[i] // v)
            tt[i] -= k * v
            cc[v] -= k
    return sum(tt) == 0


print(["No", "Yes"][check()])

Solution (Python):
import collections
h, w = map(int, input().split())
counter = collections.Counter()
for i in range(h):
    counter += collections.Counter(input())

cnt = [0, 0, 0, 0]
for k, v in counter.items():
    cnt[v % 4] += 1
if h % 2 == 0 and w % 2 == 0:
    if cnt[1] == 0 and cnt[2] == 0 and cnt[3] == 0:
        print('Yes')
    else:
        print('No')
elif h % 2 == 1 and w % 2 == 1:
    if (cnt[1] == 1 and cnt[2] <= (h - 1)//2 + (w - 1)//2 and cnt[3] == 0) or (cnt[1] == 0 and cnt[2] <= (h - 1)//2 + (w - 1)//2 - 1 and cnt[3] == 1):
        print('Yes')
    else:
        print('No')
elif h % 2 == 1:
    if cnt[1] == 0 and cnt[3] == 0 and cnt[2] <= w // 2:
        print('Yes')
    else:
        print('No')
else:
    if cnt[1] == 0 and cnt[3] == 0 and cnt[2] <= h // 2:
        print('Yes')
    else:
        print('No')

Solution (Python):
Y,X = map(int, raw_input().split())
nc = [0]*256
for y in range(Y):
	s = raw_input()
	for c in s:
		nc[ord(c)] += 1
n1 = 0
n2 = 0
for i in range(256):
	if nc[i]%4 != 0:
		if nc[i]%2 == 0:
			n2 += nc[i]%4
		else:
			n2 += nc[i]%4/2*2
			n1 += nc[i]%2

m1 = 0
m2 = 0
if X%2 and Y%2:
	m1 = 1
	m2 = X+Y-1
else:
	if X%2:
		m2 += Y
	if Y%2:
		m2 += X


if m1 < n1 or m2 < n2:
	print "No"
else:
	print "Yes"

Solution (Python):
n, m = map(int, raw_input().split())
s = ''
for i in range(n):
	s += raw_input()
c = [s.count(i) % 4 for i in 'abcdefghijklmnopqrstuvwxyz']
d1 = 0
d2 = 0
if n % 2 == 0 and m % 2 == 0:
	pass
if n % 2 == 0 and m % 2 == 1:
	d2 = n / 2
if n % 2 == 1 and m % 2 == 0:
	d2 = m / 2
if n % 2 == 1 and m % 2 == 1:
	d2 = n / 2 + m / 2
	d1 = 1

for i in c:
	while i >= 2 and d2 > 0:
		i -= 2
		d2 -= 1
	while i >= 1 and d1 > 0:
		i -= 1
		d1 -= 1
	if i > 0:
		print 'No'
		exit()

print 'Yes'