Palindrome Program: Python Implementation

Introduction

Have you ever thought of the word “MALAYALAM” and why its opposite is the same, and what do we call a word? Don't worry, dear, when Board Infinity is here.

In this article, we will discuss how we can implement palindrome in python. If a word’s opposite is equal to the original word, then we can say, i.e., a palindrome. Now let us understand the logic behind this and how we can know which word is palindrome or not. Let us discuss the methods to solve this problem.

Methods for Finding Palindrome

We will discuss different methods for finding whether a word that is palindrome or not. Let us understand what those methods are.

Bruteforce Method

In this method, we will find the reverse of the string, and then we will check if the reverse of the string and the original are the same or not. Here is the implementation of it.

Code

# Palindrome in Python

# Finding reverse of the string
def funReverse(s):
    return s == s[::-1]

orginalString = "inifini"
reverseString = funReverse(orginalString)

if reverseString:
    print("Yes",orginalString,"is palindrome")
else:
    print("No")

Output

Yes infini is palindrome

write your code here: Coding Playground

This method's time and space complexity is O(n) and O(1). Let us understand the iterative method to find palindrome in Python.

Iterative Method

To use this method, you must run a loop from starting to length/2 and compare each character in the string to the one before it. If there are any character mismatches, the string won't be a palindrome.

Code

# Palindrome in Python

# Finding reverse of the string
def funReverse(orginalString):

    # Run loop from 0 to length/2
    for i in rangeY(0, int(len(orginalString)/2)):
        if orginalString[i] != orginalString[len(orginalString)-i-1]:
            return False
    return True

orginalString = "inifini"
reverseString = funReverse(orginalString)

if (reverseString):
    print("Yes",orginalString,"is palindrome")
else:
    print("No")  

Output

Yes infini is palindrome

write your code here: Coding Playground

This method's time and space complexity is O(n) and O(1).

Recursive Method

In this method, you need to compare the first and the last element of the string and give the rest of the substring a recursive call to itself.

Code

# Palindrome in Python

# Finding reverse of the string
def funReverse(orginalString):

    # Change string to lower case
    orginalString = orginalString.lower()
    # length of originalString
    l = len(orginalString)

    # If length < 2
    if l < 2:
        return True

    # If originalString[0] and originalString[l-1] are equal
    elif orginalString[0] == orginalString[l - 1]:
        return funReverse(orginalString[1: l - 1])

    else:
        return False


orginalString = "inifini"
reverseString = funReverse(orginalString)

if reverseString:
    print("Yes",orginalString,"is palindrome")

else:
    print("No")    

Output

Yes infini is palindrome

write your code here: Coding Playground

This method's time and space complexity is O(n) and O(n).

Conclusion

In this article, we have discussed palindrome in Python. We discussed how we could find out whether a word is a palindrome or not. We discussed three methods to find palindrome in Python.