1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: bool isPalindrome(string s) { int left = 0, right = s.size() - 1; while(left < right) { while(left < right && !isValid(s[left])) ++left; while(left < right && !isValid(s[right])) --right; if(left >= right) break; if(tolower(s[left++]) != tolower(s[right--])) return false; } return true; } private: inline bool isValid(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z'); } };
|