Example 1
- Input
- s = "A man, a plan, a canal: Panama"
- Output
- true
Cleaning leaves "amanaplanacanalpanama", which reads identically in both directions.
Given a string s, decide whether it reads the same forward and backward after converting all letters to lowercase and deleting every non-alphanumeric character. An empty result after cleaning counts as a valid palindrome.
A phrase is a palindrome when, after converting all uppercase letters to lowercase and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters are letters and digits.
Given a string s, return true if it is a palindrome under these rules, and false otherwise.
1 <= s.length <= 2 * 10^5s consists of printable ASCII characters.Cleaning leaves "amanaplanacanalpanama", which reads identically in both directions.
Cleaning leaves "raceacar", whose mirror "racaecar" differs, so it is not a palindrome.
Removing the space leaves the empty string, which reads the same forward and backward.
Cleaning leaves 0p. Digits are retained, and the digit 0 differs from the letter p, so the result is false.
The exclamation mark is ignored and uppercase A becomes a, leaving aa, which is a palindrome.
Ignore punctuation and spaces, then compare matching positions from opposite ends. For Example 1, the first meaningful character A matches the last a after lowercasing; the next left space is skipped before m matches m. Digits remain meaningful: 0 does not match the letter p. Because the input is printable ASCII, meaningful characters are exactly A-Z, a-z, and 0-9.
Reveal Reverse and Compare: intuition, complexity, and code
" .,; ", which cleans to the empty string and returns true."Aa", which matches only when both sides share one case."0P", where 0 and p correctly fail the comparison.left < right inside the skip loops, which overruns on strings like ",."."A" and "a" wrongly mismatch.When mirror positions of a sequence must agree, inward pointers test each pair in place with constant extra space.