Program to Check if a String is Lapindrome or Not

Python Program to Check if a String is Lapindrome or Not

In the previous article, we have discussed Python Program to Calculate BMI
Lapindrome:

When strings are divided into halves, if the partitioned strings have the same frequency (i.e. character count) on both partitions, those strings are referred to as lapindromes. If there are an odd number of characters, ignore the middle character.

Example:

Given String = pqrsprq

When the given string is divided into two halves we get pqr and prq (ignore middle character). Both these strings have the same characters. Hence the given String is Lapindrome.

Examples:

Example 1:

Input:

Given String = pqrsprq

Output:

Yes,the above Given string is a lapindrome

Example 2:

Input:

Given String = btechgeeks

Output:

No, the above given string is not a lapindrome

Program to Check if a String is lapindrome or Not

Below are the ways to check if the given  String is lapindrome or Not

Method #1: Using Slicing Operator (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Initialize two empty stings(S1, S2).
  • If the length of the Given String is even, then divide it into two equal halves using the slicing operator and append it to the above two declared empty strings.
  • Else if the length of the Given String is odd, then divide it into two equal halves using the slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
  • Convert the split strings S1, S2 into two lists respectively using the built-in list() function and store them in another variable.
  • Sort the above two lists in ascending order using the sort() function and store them in another variable.
  • Convert the above two lists into two different strings respectively and store them in two variables.
  • If both the given split strings are equal, print “Lapindrome ” using the if conditional statement.
  • print “Not Lapindrome”, if both the given split strings are not equal.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng = "pqrsprq"
# Initialize two empty stings
fst_strng, secnd_strng = '', ''
# If the length of the Given String is even ,then divide it into two equal halves
# using slicing operator and append it to the above two declared empty strings.
if(len(gvn_strng) % 2 == 0):
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2:]
# Else if the length of the Given String is odd ,then divide it into two equal halves
# using slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
else:
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2+1:]
# Convert the split strings S1, S2 into two lists respectively using built-in list()
# function and store them in another variables.
lst_1 = list(fst_strng)
lst_2 = list(secnd_strng)
# Sort the above two lists in ascending order and store them in another variables .
lst_1.sort()
lst_2.sort()
# Convert the above two lists into two different strings respectively and
# store them in another variables .
fst_strng = str(lst_1)
secnd_strng = str(lst_2)
# If both the given split strings are equal ,print "Lapindrome " using if condition.
if(fst_strng == secnd_strng):
    print('Yes,the above Given string is a lapindrome')
# print "Not Lapindrome", if both the given split strings  are not equal .
else:
    print('No, the above given string is not a lapindrome')

Output:

Yes,the above Given string is a lapindrome

Method #2: Using Slicing Operator (User Input)

Approach:

  • Give the string as User input using the input() function and store it in a variable.
  • Initialize two empty stings(S1, S2).
  • If the length of the Given String is even, then divide it into two equal halves using the slicing operator and append it to the above two declared empty strings.
  • Else if the length of the Given String is odd, then divide it into two equal halves using the slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
  • Convert the split strings S1, S2 into two lists respectively using the built-in list() function and store them in another variable.
  • Sort the above two lists in ascending order using the sort() function and store them in another variable.
  • Convert the above two lists into two different strings respectively and store them in two variables.
  • If both the given split strings are equal, print “Lapindrome ” using the if condition.
  • print “Not Lapindrome”, if both the given split strings are not equal.
  • The exit of the program.

Below is the implementation:

# Give the string as User input and store it in a variable.
gvn_strng = input("Enter Some Random String = ")
# Initialize two empty stings
fst_strng, secnd_strng = '', ''
# If the length of the Given String is even ,then divide it into two equal halves
# using slicing operator and append it to the above two declared empty strings.
if(len(gvn_strng) % 2 == 0):
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2:]
# Else if the length of the Given String is odd ,then divide it into two equal halves
# using slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
else:
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2+1:]
# Convert the split strings S1, S2 into two lists respectively using built-in list()
# function and store them in another variables.
lst_1 = list(fst_strng)
lst_2 = list(secnd_strng)
# Sort the above two lists in ascending order and store them in another variables .
lst_1.sort()
lst_2.sort()
# Convert the above two lists into two different strings respectively and
# store them in another variables .
fst_strng = str(lst_1)
secnd_strng = str(lst_2)
# If both the given split strings are equal ,print "Lapindrome " using if condition.
if(fst_strng == secnd_strng):
    print('Yes, the above Given string is a lapindrome')
# print "Not Lapindrome", if both the given split strings  are not equal .
else:
    print('No, the above given string is not a lapindrome')

Output:

Enter Some Random String = btechgeeks
No, the above given string is not a lapindrome

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.