class Solution {
public:
bool isPalindrome(string s) {
int left = 0;
int right = s.size() - 1;
while (left <= right) {
if (!isLetter(s[left])) {
++left;
continue;
}
if (!isLetter(s[right])) {
--right;
continue;
}
if (toLower(s[left]) != toLower(s[right])) return false;
++left;
--right;
}
return true;
}
bool isLetter(char c) {
return ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9');
}
char toLower(char c) {
if ('A' <= c && c <= 'Z') return c + 'a' - 'A';
return c;
}
};
No comments:
Post a Comment