https://leetcode.com/problems/valid-palindrome/
问题描述
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
Solution
class Solution {
public:
bool isPalindrome(string s) {
if(s.empty())
return true;
int left = 0;
int right = s.size() - 1;
while (left < right)
{
while(!isalnum(s[left]) && (left < right))
left++;
while(!isalnum(s[right]) && (left < right))
right--;
if(std::toupper(s[left]) != std::toupper(s[right]))
return false;
left++;
right--;
}
return true;
}
};