1. Valid Palindrome
Difficulty: easy · String, Two Pointers
Problem
A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Given a string `s`, print `true` if it is a palindrome, or `false` otherwise.
Input
A single line containing the string `s`. It may contain spaces and punctuation.
Output
Print `true` or `false`.
Example 1
Input: A man, a plan, a canal: Panama Output: true
Explanation: Keeping letters and digits in lowercase gives "amanaplanacanalpanama", which is the same reversed.
Example 2
Input: race a car Output: false
Explanation: Keeping letters and digits in lowercase gives "raceacar", which is not the same reversed.
Example 3
Input: Output: true
Explanation: Nothing is left after removing non-alphanumeric characters; an empty string reads the same both ways.
Example 4
Input: Was it a car or a cat I saw? Output: true
Explanation: Keeping letters and digits in lowercase gives "wasitacaroracatisaw", which is the same reversed.
Example 5
Input: 0P Output: false
Explanation: Keeping letters and digits in lowercase gives "0p", which is not the same reversed.
Example 6
Input: No 'x' in Nixon Output: true
Explanation: Keeping letters and digits in lowercase gives "noxinnixon", which is the same reversed.
Example 7
Input: ab_a Output: true
Explanation: Keeping letters and digits in lowercase gives "aba", which is the same reversed.
Example 8
Input: Madam, in Eden, I'm Adam. Output: true
Explanation: Keeping letters and digits in lowercase gives "madaminedenimadam", which is the same reversed.
Example 9
Input: hello Output: false
Explanation: Keeping letters and digits in lowercase gives "hello", which is not the same reversed.
Example 10
Input: 12321 Output: true
Explanation: Keeping letters and digits in lowercase gives "12321", which is the same reversed.
Constraints
- 1 <= s.length <= 2 * 10^5 - `s` consists of printable ASCII characters.
Solutions are judged against 10 sample and 5 hidden tests. Sign in to solve it · All problems