-3

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))
4
  • 1
    Instead of taking a single I/P at once and then splitting it with endline char, try taking input at every line and trim it for every testcase to remove possible whitespaces.
    – Zircoz
    Jun 26, 2020 at 4:31
  • 1
    I just checked the problem and weirdly enough most of the top submissions are in c or go.
    – Zircoz
    Jun 26, 2020 at 6:45
  • how is abccab considered a lapindrome
    – Joe Ferndz
    Dec 6, 2020 at 4:24
  • figured out that abccab should result in YES for lapindrome as the characters a,b,c have the same frequency on both the halves
    – Joe Ferndz
    Dec 6, 2020 at 4:46

1 Answer 1

0

LAPIN solution

T = int(input())
lines = []
for i in range(T): lines.append(input())
for i in range(T):
    word = lines[i]
    ln = len(word)
    lap_left  = sorted(word[:ln//2])
    lap_right = sorted(word[ln//2:] if (ln%2 == 0) else word[ln//2 + 1:])
    if (lap_left == lap_right):
        lapindrome = 'YES'
    else:
        lapindrome = 'NO'
    print (lapindrome)

Example of output:

5
alphabet
xyzzxy
rotor
abccab
gaga
NO
YES
YES
YES
YES

earlier response

Do you want to try to do this in fewer lines? Here's how I got to it.

while (word := input('Enter your string :')) != "quit":
    ln = len(word)
    lap_left  = sorted(word[:ln//2])
    lap_right = sorted(word[ln//2:] if (ln%2 == 0) else word[ln//2 + 1:])
    if (lap_left == lap_right):
        lapindrome = 'YES'
    else:
        lapindrome = 'NO'

    print (word, lapindrome)

The output of this is :

Enter your string :gaga
gaga YES
Enter your string :abccab
abccab YES
Enter your string :rotor
rotor YES
Enter your string :xyzxy
xyzxy YES
Enter your string :alphabet
alphabet NO
Enter your string :quit
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.