Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Valid Palindrome

easytwo-pointersstring

Problem

Given a string s, decide whether it reads the same forwards and backwards once you ignore every character that is not a letter or digit and treat uppercase and lowercase letters as equal. An empty string, or one that contains no alphanumeric characters at all, counts as a palindrome.

  • s is at most 30,000 characters long; the empty string is a valid input.
  • Every character is printable ASCII (letters, digits, space, punctuation).

Examples

  • s = "Step on no pets!" → True. Dropping the space and the ! and lowercasing leaves steponnopets, which is symmetric.
  • s = "Top spot? Not." → False. The cleaned string is topspotnot; the outer t pair matches, then o matches o, then p is compared with n and the scan fails.
  • s = "..." → True. Nothing survives the filter, so there is nothing to mismatch.

Hints

  1. What is the first pair of characters you would compare to prove or disprove symmetry, and what pair comes next?
  2. You do not need to build the cleaned string; you can skip over irrelevant characters as you go.
  3. Two pointers, one from each end, each advancing past non-alphanumeric characters before comparing case-folded values.

Last reviewed 8 September 2026