Python Algorithms: Check if String is Palindrome

This is a classic problem that is used for beginner programmers. To proceed we need to break the problem down and assess what its need to move forward.

First what is a palindrome?? A palindrome is a word, sequence or phrase that reads the same forward and backwards. A common example of this is ‘racecar’. To solve this problem we need to know how to reverse a string and check if something is equal to something else.

The Most Pythonic Solution

def isPalindrome(str):
return str == str[::-1]

The Big O Notation of this code is O(n). This is due to us going through the length of the first string str and the reverse of the string str[::-1] . Both of these operations are O(n) and O(n). The way we have written our code will return a boolean, since it is checking if the string is equal to the reverse of the string or not.

Rejoice we now have another simple problem under our belt.