String Processing in Python: Is Palindrome

preview_player
Показать описание
In this video, we will be considering how to test whether a string is a palindrome in Python. We will be doing this using a linear amount of time and a constant amount of space.

We will cover how to solve this problem algorithmically, and then code up a solution to this question in Python.

This video is part of a series on string processing and, specifically, on how these problems tend to show up in the context of a technical interview:

This video is also part of an "Algorithm" series. For more algorithm tutorials:

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

I don't think the while loops in the while loop are necessary, they can just be conditionals, with the i<j condition removed

AAAAAAAAAAAAAAAAAAAAHH
Автор

Wonderful tutorial. This solved my problems on palindrome.

Alan-qbqt
Автор

s = ''.join([i for i in s if i.isalpha()]).lower()
will work to clean up the string. .replace(' ', '') is not needed. .isalpha() filters out the space.

sankaranarayanansubbayya
Автор

i was little bit confused from last 2 hours

but now i have got what am was understand in this solution

My doubt was
why we take j = len(s) -1 instead of j = len(s)

but afeter so many searching i got the answer, that in a string index number starts from 0

anyway your explanation is so well
you are the best for DSA

mightyprogrammer
Автор

Thanks for the videos! Very nice examples and clear explanations. Keep up the good work! Subscribed. 👍

zvapa
Автор

For the solution using the join() method. I don't think the argument needs to be within a list. Also, isn't the replace() method pointless as white-space isn't alphabetical?

redpred
Автор

Super helpful video. I was wondering why you added the` i < j` condition in the 2 inner while loops (one for s[i] and one for s[j]) that checks if the char is alphanumeric? Since the outer loop already accounts for `i < j` condition... is there a corner case I'm missing? Thank you!

elisad
Автор

sir i am little bit confuse hope you will get me out of it....why we first increment the j and then decrement it later

shivamrawat
Автор

Is stack a better data structure for this problem?

connorhennen
Автор

should the j be decremented inside the while loop ? (increment will shift to right and not to left) ....I saw your edit at the end of the you

chittybabu
Автор

couldn't you just use
txt = input("enter a palindrome: ")
txt = ''.join([i for i in txt if i.isalpha()]).replace(" ", "").lower()

if txt == txt[::-1]:
print(True)

McMurphyMillions
Автор

Hello. thanks for the video. Can you explain the time complexity for your second solution? My guess is O(n^2) but I am not sure since my initial loop only goes half the time

nwaiwumunachimso
Автор

Aren’t you looping over the string several times, by which you are increasing the time complexity?

rajivm
Автор

the variable s isn't a palindrome, cat spelled backwards is tac..

Kingofqueers