Valid Palindrome
Check whether a string reads the same both ways once you drop non-alphanumeric characters and ignore case.
The problem
You get a string s that can contain letters, digits, spaces and punctuation. Ignore every character that is not a letter or a digit, and treat uppercase and lowercase letters as the same. Return true if what is left reads the same forwards and backwards, and false otherwise.
Input s = "Top spot!"
Output true
Keeping only letters and digits, in lowercase, gives "topspot", which reads the same both ways.
Input s = "Level 5"
Output false
The cleaned string is "level5", and backwards it is "5level"; digits count too.
How to spot the pattern
- You compare the first character with the last, the second with the second-last, and so on.
- Some characters must be skipped, and two pointers can skip them in place instead of building a cleaned copy.
The idea
A palindrome mirrors around its middle, so compare characters from both ends moving inward. Put one pointer at the start and one at the end, move each past characters that are not letters or digits, then compare the two characters in lowercase. One mismatch means false; if the pointers meet without a mismatch, the string is a palindrome.
Brute force: Build a cleaned lowercase copy and compare it with its reverse, which is also O(n) time but needs O(n) extra space.
Step by step
- 1Set
left = 0andright = len(s) - 1. - 2While
left < right, moveleftforward past characters that are not letters or digits, and moverightbackward the same way. - 3Compare
s[left]ands[right]in lowercase, and returnfalseif they differ. - 4Otherwise move both pointers one step inward and repeat.
- 5When the pointers meet, return
true.
See the pattern move
The two pointers pattern on a small example. Step through it, or press play.
Code
class Solution:
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
# skip anything that is not a letter or a digit
while left < right and not s[left].isalnum():
left += 1
while left < right and not s[right].isalnum():
right -= 1
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return TrueComplexity
Time
O(n)
Space
O(1)
Each pointer only moves inward, so every character is looked at once; no cleaned copy of the string is made.
Watch out for
- Leaving out
left < rightin the skipping loops, which lets a pointer run off the end of a string made only of punctuation, like ".,". - Skipping digits as if they were punctuation: in "0P" the 0 and the P must be compared, and they differ.
- Comparing without lowercasing both sides, so "A" and "a" look different.
Follow-up questions
What if you may delete at most one character?
Walk inward the same way. At the first mismatch, check whether the rest is a palindrome after skipping the left character or after skipping the right one; either one working means true. That is Valid Palindrome II, still O(n).
Why not just compare the string with its reverse?
It works and is quick to write, but it builds a cleaned copy, which costs O(n) extra memory. The two-pointer version needs only two indices.
In Hinglish
Dono end se chalo, faltu characters skip karo, aur lowercase mein compare karo; ek bhi mismatch mila toh false.