Practice

Valid Palindrome

Module 5 · Strings

Problem

A phrase is a palindrome if, after converting to lowercase and removing all non-alphanumeric characters, it reads the same forward and backward. Given s, return whether it is a palindrome under the cleaned reading.

Goal

Decide true/false for the cleaned reading — not “how to clean” yet. The follow-up asks for O(1) auxiliary space (no cleaned copy).

Examples

Example 1

Input"A man, a plan, a canal: Panama"Outputtrue

Explanation. amanaplanacanalpanama

Example 2

Input"race a car"Outputfalse

Explanation. raceacar

Example 3

Input" "Outputtrue

Explanation. empty after cleaning

Constraints

1 ≤ n ≤ 2·10⁵ · printable ASCII · prefer O(1) auxiliary space

Attempt it first

Tip

The easy version (clean the string, compare with its reverse) is worth 60 seconds. The real target is the follow-up: no cleaned copy at all.