Problem
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
Input: "A man, a plan, a canal: Panama"
Output: true
Input: "race a car"
Output: false
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPalindrome(string s) {
int i=0;
int j=s.size()-1;
transform(s.begin(),s.end(),s.begin(), ::tolower);
while(i<j){
if(!((s[i]>='a'&&s[i]<='z') || (s[i]>='0'&&s[i]<='9'))){
i++;
continue;
}
if(!((s[j]>='a'&&s[i]<='z') || (s[j]>='0'&&s[j]<='9'))){
j--;
continue;
}
if(s[i]==s[j]){
i++;
j--;
continue;
}else{
return false;
}
}
return true;
}
};
网友评论