iq.lab
Python starts when a code cell comes near or you run one
easyTwo pointers target 15 min

Valid palindrome

A palindrome reads the same from left to right as from right to left, like "noon". You get a string s. Keep only its letters and digits, and treat uppercase and lowercase as the same letter. Return True if what is left is a palindrome, and False if it is not.

Spaces, punctuation and other symbols do not count, so "No, on" is a palindrome: its letters, in lowercase, are noon. Digits do count, and a digit never matches a letter. A string with no letters or digits at all is a palindrome too, because nothing is left to disagree.

Example 1
Inputs = "Top spot"OutputTrue

Keeping letters and digits in lowercase leaves topspot, which reads the same both ways.

Example 2
Inputs = "Top spots"OutputFalse

topspots backwards is stopspot. The first and last letters already differ: t and s.

Example 3
Inputs = ", ."OutputTrue

No letters or digits are left, and an empty string reads the same both ways.

Constraints
  • 0 ≤ len(s) ≤ 2 × 105

  • s holds printable ASCII characters: letters, digits, spaces, punctuation and symbols such as $ or @.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.