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
Output
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
Output
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
Output
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.