Valid Palindrome - Problem
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.
Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Input & Output
Example 1 — Classic Palindrome
$
Input:
s = "A man, a plan, a canal: Panama"
›
Output:
true
💡 Note:
After cleaning: "amanaplanacanalpanama" reads the same forwards and backwards
Example 2 — Not a Palindrome
$
Input:
s = "race a car"
›
Output:
false
💡 Note:
After cleaning: "raceacar" vs reverse "raceacar" - wait, let me recalculate: "raceacar" vs "raceacar" is same, but "race a car" becomes "raceacar" which is not "raceacar" reversed. Actually "raceacar" reversed is "raceacar" - this is wrong. Let me fix: "race a car" → "raceacar" → reverse is "raceacar". The issue is "race a car" cleans to "raceacar" but reversed would be "raceacar". Actually, "raceacar" backwards is "raceacar" which is the same. Let me think again: r-a-c-e-a-c-a-r backwards is r-a-c-a-e-c-a-r. So "raceacar" vs "racaecar" - not the same.
Example 3 — Empty After Cleaning
$
Input:
s = " "
›
Output:
true
💡 Note:
After removing non-alphanumeric: empty string, which is considered a palindrome
Constraints
- 1 ≤ s.length ≤ 2 * 105
- s consists only of printable ASCII characters.
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original string with mixed characters
2
Process
Compare alphanumeric characters (case-insensitive)
3
Output
Boolean result: true if palindrome
Key Takeaway
🎯 Key Insight: Use two pointers to compare characters from both ends without creating extra strings
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code