Challenge: Palindrome Sentence
Completed
Read a sentence. Ignoring spaces, punctuation and capital letters, is it a palindrome? Use two pointers that skip anything that isn't a letter or digit. Print yes or no.
Input
One line of text.
Output
yes or no.
Example
Input
A man, a plan, a canal: Panama
Output
yes
Editor
Python
Hints
ch.isalnum() is True for letters and digits.
Compare with .lower() so A and a match.
If a side points at a space or comma, just move that pointer.
Solution explanation
The pointers skip junk characters and compare letters, in one pass: O(n) time, O(1) space.
Show solution
s = input()
left, right = 0, len(s) - 1
ok = True
while left < right:
if not s[left].isalnum():
left += 1
elif not s[right].isalnum():
right -= 1
elif s[left].lower() != s[right].lower():
ok = False
break
else:
left += 1
right -= 1
print("yes" if ok else "no") Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module