125.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. Alphanumeric characters include letters and numbers.
Given a string s, return trueif it is a palindrome, orfalseotherwise.
Example 1:
1 2 3
Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
1 2 3
Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.
Example 3:
1 2 3 4
Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
classSolution { public: boolisPalindrome(string s){ int n = s.size(); int left = 0, right = n - 1; while (left < right) { while (left < right && !isalnum(s[left])) { ++left; } while (left < right && !isalnum(s[right])) { --right; } if (left < right) { if (tolower(s[left]) != tolower(s[right])) { returnfalse; } ++left; --right; } } returntrue; } };