Skip to content
AI360Xpert
Beta
Difficulty: EasyTwo Pointers

Valid Palindrome

Problem in Plain English

Given a string s, decide whether it reads the same forward and backward after converting all letters to lowercase and deleting every non-alphanumeric character. An empty result after cleaning counts as a valid palindrome.

Problem Statement

A phrase is a palindrome when, after converting all uppercase letters to lowercase and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters are letters and digits.

Given a string s, return true if it is a palindrome under these rules, and false otherwise.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists of printable ASCII characters.

Examples

Example 1

Input
s = "A man, a plan, a canal: Panama"
Output
true

Cleaning leaves "amanaplanacanalpanama", which reads identically in both directions.

Example 2

Input
s = "race a car"
Output
false

Cleaning leaves "raceacar", whose mirror "racaecar" differs, so it is not a palindrome.

Example 3

Input
s = " "
Output
true

Removing the space leaves the empty string, which reads the same forward and backward.

Example 4

Input
s = "0P"
Output
false

Cleaning leaves 0p. Digits are retained, and the digit 0 differs from the letter p, so the result is false.

Example 5

Input
s = "Aa!"
Output
true

The exclamation mark is ignored and uppercase A becomes a, leaving aa, which is a palindrome.

Intuition

Ignore punctuation and spaces, then compare matching positions from opposite ends. For Example 1, the first meaningful character A matches the last a after lowercasing; the next left space is skipped before m matches m. Digits remain meaningful: 0 does not match the letter p. Because the input is printable ASCII, meaningful characters are exactly A-Z, a-z, and 0-9.

Approaches

Reverse and Compare

Solution Details

Reveal Reverse and Compare: intuition, complexity, and code

Hints

Hint 1
Only letters and digits matter, and casing is ignored, so what should each comparison actually test?
Hint 2
Characters at mirror positions must agree, which suggests checking from both ends toward the middle.
Hint 3
Punctuation can be stepped over during the scan instead of removed beforehand.

Edge Cases

  • Only spaces or punctuation such as " .,; ", which cleans to the empty string and returns true.
  • Mixed casing such as "Aa", which matches only when both sides share one case.
  • Digits mixed with letters such as "0P", where 0 and p correctly fail the comparison.

Common Mistakes and Interview Tips

  • Skipping non-alphanumeric characters without rechecking left < right inside the skip loops, which overruns on strings like ",.".
  • Comparing raw characters without lowercasing, so "A" and "a" wrongly mismatch.
  • Building the cleaned string but forgetting the reversal comparison needs the same case folding.
  • Treating only letters as meaningful drops digits and wrongly accepts 0P.
  • Interview tip: state the printable-ASCII assumption. General Unicode can involve different letter categories and case mappings across languages and needs a separate contract.

Key Takeaway

When mirror positions of a sequence must agree, inward pointers test each pair in place with constant extra space.