Is Palindrome

In this lesson, you will learn how to determine if a string is a palindrome or not.

In this lesson, we will be considering how to test whether a string is a palindrome in Python. At first, we’ll come up with a concise solution that takes extra space, but we’ll eventually code a solution that takes a linear amount of time and a constant amount of space.

A palindrome is a word, number, phrase, or any other sequence of characters that reads the same forward as it does backward.

Here are some examples of a palindrome:

Implementation

Let’s get started with the implementations.

Solution 1

Press + to interact
s = "Was it a cat I saw?"
# Solution uses extra space proportional to size of string "s"
s = ''.join([i for i in s if i.isalnum()]).replace(" ", "").lower()
print(s == s[::-1])

Let’s discuss the above one-line solution in parts. So the [i for i in s if i.isalnum()] statement returns ...