Valid Palindrome
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, return true if it is a palindrome, or false otherwise.
Examples
s = "A man, a plan, a canal: Panama"trues = "race a car"falses = " "trueConstraints
1 <= s.length <= 2 * 10^5s consists only of printable ASCII characters
š” Hints
Hint 1
Use two pointers starting from both ends of the string.
Hint 2
Skip non-alphanumeric characters and compare lowercase versions.
Algorithm Complexity
Official asymptotic Big-O runtime and auxiliary memory analysis
Two pointers from both ends. Skip non-alphanumeric characters, then compare lowercase characters. If any mismatch, return false.
Two pointers move toward each other from the ends, skipping non-alphanumeric characters and comparing at most n characters.
Directly inspects characters in-place using two pointer indices without allocating a cleaned string copy.
š Real-World Use Cases
Checking if usernames or display names have palindromic patterns, used in content moderation to detect spam patterns.
Identifying palindromic sequences in DNA strands, which form hairpin structures important in molecular biology.
š Dry Run
Initialize left=0 (ā 'r') and right=7 (ā '!'). Input: "racecar!"