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
Valid Palindrome Problem OverviewINPUT"A man, a plan, a canal: Panama"PROCESS: Keep only alphanumeric, ignore caseCLEANED"amanaplanacanalpanama"Compare: same forwards ↔ backwards?OUTPUTtrue
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
Asked in
Meta 45 Amazon 38 Microsoft 32 Apple 28
296.8K Views
High Frequency
~15 min Avg. Time
8.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen