Practice · Two pointers · 1 of 5

Palindrome, Ignoring Punctuation

You are given a string s. Keep only its letters and digits, treat upper and lower case as the same, and return true if what is left reads the same forwards and backwards. Otherwise return false.

Spaces, punctuation and every other character are ignored. A string with no letters or digits at all counts as a palindrome.

Example 1

Input
s = "Was it a car, or a cat I saw?"
Output
true
Keeping letters only gives wasitacaroracatisaw, which reverses to itself.

Example 2

Input
s = "Race a car"
Output
false
raceacar backwards is racaecar.

Example 3

Input
s = "0P"
Output
false
Digits count too: 0p backwards is p0.

Constraints

  • s holds 1 to 2 × 10⁵ characters.
  • Every character is printable ASCII: letters, digits, spaces and punctuation.