#125

Valid Palindrome

pupil · 485 · lc easy +27 · verified · 52.9% accepted · 11,498 likes · top 43%

play →

Description

A string is a palindrome if, after lowercasing all letters and removing every non-alphanumeric character, it reads the same forwards and backwards.

Given a string s, return true if it qualifies as a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

Code

1
2
3