Palindrome through the noise
A phrase counts as a palindrome here if it reads the same both ways after ignoring case and every character other than letters and digits.
is_clean_palindrome("No lemon, no melon")
# True
is_clean_palindrome("ab")
# FalseTwo pointers
You could build a filtered string and compare it with its reverse, but that uses extra memory. Two indexes can do it in place:
leftstarts at the beginning andrightat the end.- Move each inward past characters that do not count.
str.isalnumtells you which those are. - Compare the two characters with
lower(). A mismatch ends the search. - Stop when the indexes meet.
Text with nothing to compare, such as "!!" or an empty string, counts as a palindrome.
Your task
Write is_clean_palindrome(text) using two pointers, returning True if the letters and digits of text read the same in both directions, ignoring case.
The explanation opens here once every check passes.