EasyTwo PointersString

Valid Palindrome

LeetCode
1 approach, code in all languages

You are given a string that may contain letters, digits, spaces, and punctuation. The task is to decide whether the string reads the same forward and backward once you ignore everything that is not a letter or a digit, and once you treat uppercase and lowercase letters as equal.

Return true when the cleaned-up version of the string is a palindrome, and false otherwise. An empty string (after filtering) counts as a palindrome.

Example 1

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

Output: true

After removing punctuation and spaces and lowercasing, the text becomes "amanaplanacanalpanama", which mirrors itself.

Example 2

Input: s = "race a car"

Output: false

The filtered text "raceacar" differs when read backward, so it is not a palindrome.

Constraints

  • 1 ≤ s.length ≤ 2 * 10^5
  • s consists only of printable ASCII characters.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now