All problems

Valid Palindrome

Easy · two pointers

Given a string s, return True if it reads the same forwards and backwards, and False otherwise.

Only letters and digits count. Ignore every other character (spaces, punctuation, symbols), and treat uppercase and lowercase letters as the same. For example, "A man, a plan, a canal: Panama" becomes "amanaplanacanalpanama" once you drop the extras, and that is a palindrome.

If nothing is left after ignoring characters (for instance s = " "), the empty text reads the same both ways, so return True.

Examples

Example 1

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

Output: True

Keeping only letters and digits in lowercase gives "amanaplanacanalpanama", which is the same reversed.

Example 2

Input: s = "race a car"

Output: False

It becomes "raceacar", which reversed is "racaecar".

Example 3

Input: s = " "

Output: True

No letters or digits remain. The empty text is a palindrome.

Constraints