I'm trying to submit my code for LAPIN problem in CodeChef. But it keeps telling me my submission is wrong despite it compiled, ran, and had the same exact output expected. I also tested it with random input and the code works just fine. The only different is CodeChef use an array instead of dictionary. But I wasn't sure if that is the issue.
Here is the problem: Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match. Your task is simple. Given a string, you need to tell if it is a lapindrome.
This is the code that I submitted:
import sys
T=int(sys.stdin.readline())
S=sys.stdin.read()
array_string = S.split('\n',T-1)
def count_characters(random_string):
some_dict={}
for char in random_string:
if char in some_dict.keys():
some_dict[char] = some_dict[char]+1
else:
some_dict[char] = 1
return some_dict
def is_lapindrome(random_string):
left_side=random_string[:int(len(random_string)/2)]
right_side=random_string[-int(len(random_string)/2):]
left_storage=count_characters(left_side)
right_storage=count_characters(right_side)
for key in left_storage.keys():
if key in right_storage.keys():
if left_storage[key] == right_storage[key]:
lapindrome="YES"
else:
lapindrome="NO"
break
else:
lapindrome="NO"
break
return lapindrome
for each_string in array_string:
print(is_lapindrome(each_string))
abccab
considered a lapindromeabccab
should result in YES for lapindrome as the charactersa,b,c
have the same frequency on both the halves