DEV Community

Cover image for Data Structure and Algorithms: Palindrome String.
Abhishek Jain
Abhishek Jain

Posted on

3 2

Data Structure and Algorithms: Palindrome String.

Hey there, today we are gonna check if a string is Palindrome or not. Now, before doing that we need to understand what a palindrome is...

Basically a palindrome is defined as a string that is written the same forward and backwards. For example -: abcdbca, if you read this from backwards it will the same.

So, how can we implement this algorithm in python?
Well, it's just a few lines of code in python...

def isPalindrome(string):
    reversed_string = ""
    for i in reversed(range(len(string))):
        reversed_string += string[i]
    return string == reversed_string
Enter fullscreen mode Exit fullscreen mode

In the above code, we defined a function and it takes a parameter of string. In the second line, we define a variable reversed_string and assign it to an empty string. In the third line of code, we start a for loop with a range of length of the string in the reversed order, Because we want the reversed string of the input string. In the fourth line of code, we assign the input order of string in reversed order to the reversed_string variable. In the fifth line of code, we check if input string is equal to the reversed_string if it then returns True, else returns False.

Hey, it's my first article. I hope you liked this article. I will be back with more amazing content until then enjoy.

bye

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay