Skip to main content

Valid Palindrome

EasyStringsTwo pointers303 tests

A phrase is a palindrome if, after converting all uppercase letters to lowercase and removing every character that is not a letter or a digit, it reads the same forwards and backwards.

Given a string s, return true if it is a palindrome.

Example 1

text

After cleaning, s becomes "amanaplanacanalpanama".

Example 2

text

Cleaned, this is "raceacar", which is not a palindrome.

An empty string counts as a palindrome.

Examples

Example 1

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

Cleaned it reads "amanaplanacanalpanama".

Example 2

Input:
"race a car"
Output:
false

Cleaned it reads "raceacar".

Example 3

Input:
" "
Output:
true

Removing non-alphanumeric characters leaves an empty string, which is a palindrome.

Constraints

  • 0 <= s.length <= 2000
  • s consists of printable ASCII characters.

Run checks your code against the 3 examples. Submit checks it against all 303 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.