Leetcode 125 Valid Palindrome
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
return true;
for (int i = 0;i < s.size();i ++){
if (!isalpha(s[i]) && !isdigit(s[i])){
s.erase(i, 1);
i--;
}
}
transform(s.begin(), s.end(), s.begin(), ::tolower);
string newstr = s;
reverse(s.begin(), s.end());
if (newstr == s)
return true;
else
return false;
}
};
class Solution {
public:
bool isPalindrome(string s) {
int begin = 0;
int end = s.size() - 1;
while (begin < end){
if (!isalnum(s[begin]))
begin++;
else if (!isalnum(s[end]))
end--;
else if ((s[begin] + 32 - 'a') % 32 != (s[end] + 32 - 'a') % 32)
return false;
else{
begin++;
end--;
}
}
return true;
}
};