DSA problem
Valid Palindrome
A phrase is a palindrome if, after converting all uppercase letters into lowercase and removing all non-alphanumeric characters, it reads the same forward and backward.
Alphanumeric characters include letters and digits. Given a string s, return true if it is a palindrome, or false otherwise.
For example, "A man, a plan, a canal: Panama" reduces to "amanaplanacanalpanama" and is a valid palindrome, while "race a car" reduces to "raceacar" which is not.
Examples
Example 1
Input:
s = "A man, a plan, a canal: Panama"Output:
trueExplanation: After lowercasing and removing non-alphanumeric characters, s becomes "amanaplanacanalpanama", which reads the same both ways.
Example 2
Input:
s = "race a car"Output:
falseExplanation: After cleaning, s becomes "raceacar", which is not a palindrome.
Example 3
Input:
s = " "Output:
trueExplanation: After removing non-alphanumeric characters, s is the empty string, and an empty string is considered a palindrome.
Constraints
- 1 <= s.length <= 2 * 10^5
- s consists only of printable ASCII characters.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.