Monday, June 17, 2013

Codechef June 2013 Lapindromes

Accepted!
# 1. split string into two sections along the middle
# 2. use a associative array to store frequency of letters in each part
# 3. compare the 2 associative arrays
# 4. win!
import sys
T = int(sys.stdin.readline().strip())
for i in range(T):
p = sys.stdin.readline().strip()
if (len(p) % 2 == 0):
# even
part1 = p[0:(len(p)/2)]
part2 = p[len(p)/2:]
else:
# odd
part1 = p[0:int(len(p)/2)]
part2 = p[int(len(p)/2)+1:]
a = {}
b = {}
for c in part1:
if (c in a):
a[c]+=1
else:
a[c] = 1
for c in part2:
if (c in b):
b[c]+=1
else:
b[c]=1
palid = True
for key in a.keys():
#print(key, ":", a[key])
if key not in b or a[key] != b[key]:
palid = False
for key in b.keys():
#print(key, ":", a[key])
if key not in a or a[key] != b[key]:
palid = False
if palid:
print ("YES")
else:
print("NO")
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment