DSA Trainer
← All problems

Valid Palindrome

Two PointersEasy

Step 1 of 8 · Understand

1. Understand the problem

A phrase is a palindrome if, after removing everything that isn't a letter or digit and ignoring case, it reads the same forward and backward. Given a string, return whether it's a palindrome.

Input

A string `s` that may contain letters, digits, spaces, and punctuation.

Output

A boolean — `true` if the cleaned string is the same forwards and backwards, `false` otherwise.

Examples

input → s = "A man, a plan, a canal: Panama"
output → true
After stripping non-alphanumeric chars and lowercasing: "amanaplanacanalpanama" — a palindrome.
input → s = "race a car"
output → false
Cleaned: "raceacar" — not the same forwards and backwards.
input → s = " "
output → true
Empty after cleaning — an empty string is a palindrome.

Write your solution above and hit Run tests. Results and tips appear here.